How to set conditional breakpoint in anonymous inner class depending on final local variable?
suppose I have the following class and want to set a conditional breakpoint on arg==null at the marked location.开发者_如何学Python This won't work in eclipse and gives the error "conditional breakpoint has compilation error(s). Reason: arg cannot be resolved to a variable".
I found some related information here, but even if I change the condition to "val$arg==null" (val$arg is the variable name displayed in the debugger's variable view), eclipse gives me the same error.
public abstract class Test {
public static void main(String[] args) {
Test t1 = foo("123");
Test t2 = foo(null);
t1.bar();
t2.bar();
}
abstract void bar();
static Test foo(final String arg) {
return new Test() {
@Override
void bar() {
// I want to set a breakpoint here with the condition "arg==null"
System.out.println(arg);
}
};
}
}
I can only offer an ugly workaround:
if (arg == null) {
int foo = 0; // add breakpoint here
}
System.out.println(arg);
You could try placing the argument as a field in the local class.
static Test foo(final String arg) {
return new Test() {
private final String localArg = arg;
@Override
void bar() {
// I want to set a breakpoint here with the condition "arg==null"
System.out.println(localArg);
}
};
}
精彩评论