2015-07-13 38 views
0

我想使用Java Reflection API來獲取名稱方法參數,使用java.lang.reflect.Parameter.getName()Eclipse編譯器等價參數

我能做到這一點對於已使用Sun公司的javac編譯器編譯的代碼,如果我指定的-parameters標誌,如下所示:

$> javac 
Usage: javac <options> <source files> 
where possible options include: 
    ... 
    -parameters    Generate metadata for reflection on method parameters 
    ... 

但是,我找不到在Eclipse中任何匹配的選項,將增加這些名稱以編譯的字節碼。

因此,Eclipse中的參數名稱取代arg0arg1,...

下面舉例說明問題,所述輸出是從調試/運行在Eclipse:

public class Example { 
    public static void main(String[] theseAreMyCliArgs) { 
     // Ouptut [java.lang.String[] arg1] 
     // Should yield [java.lang.String[] theseAreMyCliArgs] 
     System.out.println(Arrays.toString(Example.class.getMethods()[0].getParameters())); 
    } 
} 

回答

1

I」 m不知道早期版本,但Eclipse Luna(4.4)在Java->Compiler->Store information about method parameters中有該選項。它僅適用於Java 1.8類文件版本。

您可以在項目首選項中對其進行設置,以將此額外元數據限制爲選定的項目。

+0

N.B.這僅適用於javac 1.8及更高版本。 – jwa