如何根据最终局部变量设置匿名内部类中的条件断点?
-
12-10-2019 - |
题
假设我有以下类,并希望在标记的位置设置有条件的断点。这不会在Eclipse中起作用,并给出错误“条件断点具有编译错误。原因:ARG无法解析为变量”。
我找到了一些相关信息 这里, ,但是即使我将条件更改为“ Val $ arg == null”(Val $ arg是在调试器的变量视图中显示的变量名称),Eclipse也会给我相同的错误。
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);
}
};
}
}
解决方案
您可以尝试将参数作为本地类中的字段。
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);
}
};
}
其他提示
我只能提供一个丑陋的解决方法:
if (arg == null) {
int foo = 0; // add breakpoint here
}
System.out.println(arg);
不隶属于 StackOverflow