jwa
jwa

Reputation: 3281

Eclipse Compiler Equivalent of -parameters

I would like to use the Java Reflection API to obtain the names method parameters, using java.lang.reflect.Parameter.getName().

I can achieve this for code that has been compiled using Sun's javac compiler, if I specify the -parameters flag, as seen here:

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

However, I cannot find any matching option in eclipse that will add these names to the compiled byte-code.

Therefore, in Eclipse the parameter names are substituted with arg0, arg1, ...

The following illustrates the problem, stated output is from debug/run in 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()));
    }
}

Upvotes: 0

Views: 106

Answers (1)

Michał Grzejszczak
Michał Grzejszczak

Reputation: 2617

I'm not sure about earlier versions, but Eclipse Luna (4.4) has that option in Java->Compiler->Store information about method parameters. It is only available for java 1.8 class file versions.

You can set it in project preferences to limit this additional metadata to selected project(s).

Upvotes: 1

Related Questions