With Java 8 reflection API added method to get parameters name of the method. For that, you need to make some compile-time configuration as pass the compiler flag: javac -parameters.
In this example, you will difference in reflection API output with and without named parameter configuration.
Example: Named Parameter Reflection API
import java.lang.reflect.Method; import java.lang.reflect.Parameter; public class NamedParameterExample { public static void main(String[] args) throws Exception { for(Method method :NamedParameterExample.class.getDeclaredMethods()) { System.out.println("\nMethod :"+method.getName()+" Parameters are :"); for (final Parameter parameter : method.getParameters()) { System.out.println("Parameter: " + parameter.getName()); } } } public static void printValues() { //Some statement here } public static int addOperation(int A, int b) { return A+b; } public static int substractionOperation(int param1 , int param2) { return param1-param2; } }
Output: Without Configuration for the named parameter
Method :main Parameters are :
Parameter: arg0
Method :printValues Parameters are :
Method :addOperation Parameters are :
Parameter: arg0
Parameter: arg1
Method :substractionOperation Parameters are :
Parameter: arg0
Parameter: arg1
Output: With Configuration for the named parameter
Method :main Parameters are :
Parameter: args
Method :printValues Parameters are :
Method :addOperation Parameters are :
Parameter: A
Parameter: b
Method :substractionOperation Parameters are :
Parameter: param1
Parameter: param2
Eclipse Configuration for Named Parameter
Go to Window Tab -> Preferences -> Compiler -> Select Checkbox for “Store Information about method parameters (usable via reflection)”.
As shown in the below screen.
Console Configuration for Named Parameter
On time on compile with javac pass additional configuration as javac -parameters “class name”.
Maven Configuration for Named Parameter
On section of the maven-compiler-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerArgument>-parameters</compilerArgument>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
You must log in to post a comment.