如何根据最终的局部变量在匿名内部类中设置条件断点?

时间:2011-01-19 09:26:53

标签: java eclipse conditional-breakpoint anonymous-inner-class

假设我有以下类,并希望在标记位置的arg == null上设置条件断点。这在eclipse中不起作用,并给出错误“条件断点有编译错误。原因:arg无法解析为变量”。

我发现了一些相关信息here,但即使我将条件更改为“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); 
            }
        };
    }
}

2 个答案:

答案 0 :(得分:4)

我只能提供一个丑陋的解决方法:

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

答案 1 :(得分:2)

您可以尝试将参数作为字段放在本地类中。

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); 
        }
    };
}
相关问题