参数的 Eclipse 编译器等效项

Eclipse Compiler Equivalent of -parameters

我想使用 Java 反射 API 获取名称方法参数,使用 java.lang.reflect.Parameter.getName().

如果我指定 -parameters 标志,我可以为使用 Sun 的 javac 编译器编译的代码实现这一点,如下所示:

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

但是,我在 eclipse 中找不到任何匹配的选项可以将这些名称添加到已编译的字节码中。

因此,在 Eclipse 中,参数名称被替换为 arg0arg1、...

下面说明了问题,说明输出来自 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()));
    }
}

我不确定早期版本,但 Eclipse Luna (4.4) 在 Java->Compiler->Store information about method parameters 中有该选项。它仅适用于 java 1.8 class 文件版本。

您可以在项目首选项中进行设置,以将此附加元数据限制为所选项目。