How to set a conditional breakpoint in an anonymous inner class depending on the final local variable?

Suppose I have the following class and you want to set a conditional breakpoint to arg == null at the marked position. This will not work in eclipse and will give the error "conditional breakpoint has compilation error (s). Reason: arg could not 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 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); } }; } } 
+4
source share
2 answers

You can try to place the argument as a field in a 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); } }; } 
+2
source

I can only offer an ugly workaround:

 if (arg == null) { int foo = 0; // add breakpoint here } System.out.println(arg); 
+4
source

Source: https://habr.com/ru/post/1336111/


All Articles