Issue
I have a web application in which I want to retrieve method's parameter names with reflection java.lang.reflect.Method.getParameters
. Howerver parameter names can be retrieved only if Java files were compiled with -parameters
flag.
You can obtain the names of the formal parameters of any method or constructor with the method java.lang.reflect.Executable.getParameters. (The classes Method and Constructor extend the class Executable and therefore inherit the method Executable.getParameters.) However, .class files do not store formal parameter names by default. This is because many tools that produce and consume class files may not expect the larger static and dynamic footprint of .class files that contain parameter names. In particular, these tools would have to handle larger .class files, and the Java Virtual Machine (JVM) would use more memory. In addition, some parameter names, such as secret or password, may expose information about security-sensitive methods.
To store formal parameter names in a particular .class file, and thus enable the Reflection API to retrieve formal parameter names, compile the source file with the -parameters option to the javac compiler.
Source: https://docs.oracle.com/javase/tutorial/reflect/member/methodparameterreflection.html
How to instruct Eclipse to compile my web application with -parameters
so I could later start Tomcat server in Eclipse with my code compiled with -parameters
?
Solution
Eclipse has its own Java compiler and the equivalent of the -parameters
flag can be set in Project > Properties: Java Compiler by ticking the last checkbox: Store information about method parameters (usable via reflection) (which is not enabled by default and requires Java 8 or higher).
Answered By - howlger