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中的參數名稱取代arg0
,arg1
,...
下面舉例說明問題,所述輸出是從調試/運行在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()));
}
}
N.B.這僅適用於javac 1.8及更高版本。 – jwa