我想使用Java Reflection API来使用java.lang.reflect.Parameter.getName()
获取名称方法参数。
如果我指定javac
标志,我可以使用Sun的-parameters
编译器编译代码,如下所示:
$> javac
Usage: javac <options> <source files>
where possible options include:
...
-parameters Generate metadata for reflection on method parameters
...
但是,我在eclipse中找不到任何匹配选项,会将这些名称添加到已编译的字节码中。
因此,在Eclipse中,参数名称被arg0
,arg1
替换为......
以下说明了问题,所述输出来自Eclipse中的debug / run:
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()));
}
}
答案 0 :(得分:1)
我不确定早期版本,但Eclipse Luna(4.4)在Java->Compiler->Store information about method parameters
中有这个选项。它仅适用于java 1.8类文件版本。
您可以在项目首选项中对其进行设置,以将此附加元数据限制为所选项目。