4

假设我有以下类,并希望在标记的位置为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

你可以试着将参数作为一个字段的本地类。

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

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

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

条件断点*杀*性能,因此解决方法是不是*,在我的眼睛... *丑 – 2011-01-19 09:41:00