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