Java8 get parameter names and Idea/eclipse/maven configuration

Source: Internet
Author: User

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

  1. 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()); }        }    }}
  2. 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->CompilerSelect 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 CompilerAdditional command line parametersadded 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:

  1. If the Target/classes class file for the source files are not modified, will not be recompiled, directly packaged under the classes class file;
  2. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.