Java gets the method of method parameter name information in code _java

Source: Internet
Author: User

Objective

We all know that with the use of JAVA8, a new object parameter is added to the corresponding method signature, which is used to represent the specific parameter information, through which the getname can get the corresponding parameter name. That is, as written in code, such as username, in the foreground, That is, you do not need to write annotations such as @parameter ("username") class, and you can directly map by name.

As shown in the following code reference:

public class T {
 private interface T2 {
  void Method (string username, string password);
 }
 
 public static void Main (string[] args) throws Exception {
  System.out.println t.class.getmethod ("main", string[). Class). GetParameters () [0].getname ());
  System.out.println (T2.class.getMethod ("method", String.class, String.class). GetParameters () [0].getname ()];
  System.out.println (T2.class.getMethod ("method", String.class, String.class). GetParameters () [1].getname ());
 }

Press Java8 before, also can use some means to get the parameter name information, but the way is different. ParameterMethodNameResolver This can work in the previous versions of Spring MVC, as well. But you need a special compilation. That's what works. LocalVariableTable MethodParameters Chinese is compiled into a local variable table and a method parameter table.

Localvariabletable local Variable table

As described in the JVM specification, the local variable table exists in the code attribute, and the Code property is a property of the MethodInfo. It can be understood that when a method has a method body, the corresponding code attribute appears, and in the code attribute, in addition to the specific execution code, There will be other information as well. such as linenumbertable (used to describe where each line of code is located).

The local variable table is part of the debugging information in the method, so the information is not generated in the class file by default. You need to turn on the-G or-g:vars switch. Fortunately, these switches are turned on by default for the IDE or MAVEN compilation. In the IDE, you can set the (Generate debugging Info for idea) to control (default tick). In Maven, control whether output is by using Debug or DebugLevel in the plug-in maven-compiler-plugin (the default is true).

The local variable table is JAVAP, as follows:

Non-static method
  localvariabletable:
  Start Length Slot Name Signature
   0  1 0 this  LT;
   0  1  1 count J
   0  1  3 name ljava/lang/string;
 
static method
  localvariabletable:
  Start Length Slot Name Signature
   0  0 args [ljava/lang/string ;

The local variable table not only holds the parameter information, it also holds temporary variables that may be used throughout the body of the method, such as the declared int I, and so on. And as shown above, the representation method and the Non-static method, in the first place there is the difference between the this variable. Therefore, you can read the number of parameters ( method.getParameterCount ), Then, according to the method signature, read the parameter information of the specified number in the local variable table.

Note that in the above illustration, if the parameter is long or double, and the slot occupies 2, the type information of the parameter needs to be considered when the parameter information is obtained through slot.

interface method Because there is no code attribute, so there is no local variable table, get an interface of the method definition, through the local variable table can not get the corresponding parameter name

Methodparameters Method Parameter Table

The method parameter table was introduced after 1.8, so only the class file generated using JDK8 compiles the information. Unlike a local variable table, it belongs to the MethodInfo property, that is, it is the same level as the Code property. This property is available for both the interface method and the normal method. Therefore, even the interface method can get the corresponding parameter information.

By default, this information is not in class. You need to use special compilation parameters-parameters to build, and this information is not generated by default in the IDE and maven. In idea, you need to add this compilation parameter to the Java additional line parameters. In maven, you also need to compilerArgs add this parameter to the Maven-compiler-plugin parameter.

After Javap, the method parameter table is shown as follows:

Non-static method
 methodparameters:
  name       Flags
  count
  name
 
//static method
 Methodparameters:
  name       Flags
  args

As you can see, no matter whether it is static or not, only the information used to describe the parameter is present in the parameter table. The following flags parameter is used for special scenarios, such as final parameters for method rewriting, and so on.

Some of the tools you can use

In addition to using the native APIs and the Spring Toolkit, there are other tools that can get the parameter name information. In the spring system, the interface used to describe the parameter name is ParameterNameDiscoverer . It allows you to get the appropriate parameter name information. In addition, com.thoughtworks.paranamer:paranamer The Paranamer in this toolkit can also handle the corresponding information. However, the JDK8 methodparameters support is not very high, users can expand it to achieve their goals.

Summarize

The above is for you to summarize the Java to obtain code method parameter name information method, hope to learn or use Java to bring certain help, if there is doubt you can message exchange.

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.