How do you use code fragment mode in the Intellij debugger?

intellij-idea

Solution

Given

public class CodeFragment {
    public static void main(String[] args) {
        List<Foo> list = new ArrayList<Foo>();
        list.add(new Foo("555"));
        list.add(new Foo("777"));
        list.add(new Foo("999"));
        list.add(new Foo("bill"));

        System.out.println();
    }

    public static class Foo {
        String s;

        public Foo(String s) {
            this.s = s;
        }
    }
}

If we set a break point on the println, we can put the following into the code fragment

Foo resultFoo = null;
Iterator<Foo> it = list.iterator();
while (it.hasNext()) {
    Foo foo = it.next();
    if (foo.s.equals("777")) {
        resultFoo = foo;
    }
}
resultFoo = resultFoo;

This demonstrates declaration, assignment, a loop, and an if.

Note that the foreach loop is not supported, in older versions of intellij!

Also note the assignment at the end. As far as I can tell, the result that gets displayed is the result of the last statement. Without that last statement, this code would display 'false' - ie the result of the last call to it.next.

Problem

In the debugger, you can press alt-f8 and evaluate an expression. There's also a code fragment mode. The documentation in IntelliJ only says: Code Fragment Mode for evaluating short code portions introducing them in the Statements to evaluate text field. Supported constructs are declarations, assignments, loops and if/else. I can't find any examples on the web of how to use it and can't figure out myself. Can you give examples of how to use the supported constructs?

Original source