سؤال

suppose I have the following class and want to set a conditional breakpoint on arg==null at the marked location. 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); 
            }
        };
    }
}
هل كانت مفيدة؟

المحلول

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); 
        }
    };
}

نصائح أخرى

I can only offer an ugly workaround:

if (arg == null) {
     int foo = 0; // add breakpoint here
}
System.out.println(arg);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top