Before Java8, when the code is compiled into a class file, the type of the method parameter is fixed, but the method name is lost and the method name becomes arg0, arg1 ... Now, the parameter names can be left in the class file at the beginning of Java8, which brings a great traversal to the reflection. When you need to use the reflection mechanism to get the method parameters like MyBatis, you don't need to use annotations like these before @Para
.
Functional Testing
Code reference from, click to enter
Writing test Classes
import Java.lang.reflect.Method;import Java.lang.reflect.Parameter; Public classGetruntimeparametername { Public void CreateUser(String name,intAgeintVersion) {} Public Static void Main(string[] args)throwsException { for(Method m:getruntimeparametername.class.GetMethods()) {System. out.println("--------------------"); System. out.println("Method:"+ M.GetName()); System. out.println("return:"+ M.Getreturntype().GetName()); for(Parameter p:m.GetParameters()) {System. out.println("parameter:"+ P.GetType().GetName() +", "+ P.GetName()); } } }}
- Test
Because the. class file is too large or uses more memory to avoid the. classes file because it retains the parameter name, it also avoids some parameters (Secrect/password) from leaking security information, and the JVM does not retain the parameter name even when 1.8 defaults.
So here we are. You can test the compilation of reserved parameter names and do not preserve parameter names.
Parameter names are not preserved
Compile command:
javac GetRuntimeParameterName.java
Output Result:
--------------------method: createUserreturn: voidparameter:java.lang.String, arg0parameter:int, arg1parameter:int, arg2
Preserve parameter names
Compile command:
javac -parameters GetRuntimeParameterName.java
Output Result:
--------------------method: createUserreturn: voidparameter:java.lang.String, nameparameter:int, ageparameter:int, version
IDE and MAVEN Open-parameters approach in eclipse
Preferences->java->Compiler
Select the Store information about method parameters
option below.
This compiles the parameter name into the class file when compiling the Java file with Eclipse.
The method of opening in idea
File->Settings->Build,Execution,Deployment->Java Compiler
Additional command line parameters
added in the options below -parameters
.
Ways to open Maven
Add parameter configuration in Pom.xml's compile plugin <arg>-parameters</arg>
.
<PLUGIN> <GROUPID> org.apache.maven.plugins</GROUPID> <ARTIFACTID> maven-compiler-plugin</ARTIFACTID> <VERSION> 3.3</VERSION> < Configuration> <SOURCE> 1.8</SOURCE> <TARGET> 1.8</TARGET> <compilerargs <ARG> -parameters</ARG> </COMPILERARGS> </CONFIGURATION> </plugin
Also attached is the case of a test using the Maven install command to compile the class file:
When building a jar package using Maven install:
- If the Target/classes class file for the source files are not modified, will not be recompiled, directly packaged under the classes class file;
- If the Target/classes class file is modified for the source file, all class files will be recompiled, and then the class file under the classes can be packaged;
Java8 get parameter names and Idea/eclipse/maven configuration