Preface
Everyone knows that with the use of java8, a new object Parameter has been added to the corresponding method signature to represent specific parameter information. The corresponding parameter name can be obtained through its getName. That is, like written in the code, such as username, when passing parameters in the foreground, there is no need to write annotations such as @Parameter("username") class, but can directly map by name.
The following code reference is shown:
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()); }} Before java8, you can also get parameter name information through some means, but the methods are different. For example, ParameterMethodNameResolver in spring mvc can also work normally in previous versions. However, it only requires special compilation. What works here are LocalVariableTable and MethodParameters , which are compiled into local variable tables and method parameter tables in Chinese.
LocalVariableTable Local Variable Table
According to the jvm specification, the local variable table exists in the Code attribute, and the Code attribute is also an attribute of methodInfo. It can be understood that when a method has a method body, the corresponding Code attribute will appear. In the code attribute, in addition to the specific code execution, there will be other information. For example, LineNumberTable (used to describe the location of each line of code).
The local variable table is part of the debugging information in the method, so by default this information will not be generated in the class file. The -g or -g:vars switches need to be turned on. Fortunately, for ide or maven compilation, these switches are turned on by default. In ide, you can control it by setting (generate debugging info for idea) (default check). In maven, whether the output is controlled by debugging or debugLevel in the plug-in maven-compiler-plugin (the default value is true).
The local variable table is after 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 101 0 args [Ljava/lang/String;
The local variable table not only saves parameter information, but also stores temporary variables that may be used in the entire method body, such as the declared int i, etc. And as shown above, the method and non-static method have a difference between this variable in the first position. Therefore, you can read the number of parameters ( method.getParameterCount ), and then read the parameter information of the specified number in the local variable table according to the method signature.
It should be noted that in the above figure, if the parameter is long or double, its slot occupies 2 places. When obtaining parameter information through slot, the parameter type information needs to be considered.
Since the interface method has no code attribute, there is no local variable table. If you get the method definition of an interface, you cannot get the corresponding parameter name through the local variable table.
MethodParameters method parameter table
The method parameter table was introduced after 1.8, so this information is only available in class files compiled with jdk8. Unlike the local variable table, it belongs to the MethodInfo attribute, that is, it is at the same level as the Code attribute. Whether it is an interface method or an ordinary method, it has this attribute. Therefore, even an interface method can obtain the corresponding parameter information.
By default, this information is not available in the class. A special compilation parameter -parameters is required to generate, and this information is not generated by default in idea and maven. In idea, this compilation parameter needs to be added in java additional line parameters. In maven, this parameter needs to be added in the compilerArgs parameter of maven-compiler-plugin.
The method parameter table is shown in the following form after Javap:
//Non-static method MethodParameters: Name Flags count name //static method MethodParameters: Name Flags args
It can be seen that whether it is static or not, only information used to describe the parameters will appear in the parameter table. The following Flags parameter is used in some special scenarios, such as final parameter for method rewriting, etc.
Some tools available
In addition to using native APIs and spring toolkits, there are other tools that can get parameter name information. In the spring system, the interface used to describe parameter names is ParameterNameDiscoverer . Through it, the corresponding parameter name information can be obtained. In addition, the Paranamer in com.thoughtworks.paranamer:paranamer toolkit can also process corresponding information. However, the support for jdk8 methodParameters is not very high, and users can achieve their own goals by extending it.
Summarize
The above is the method of obtaining the parameter name information of the Java code summarized by you. I hope it will help you learn or use Java. If you have any questions, you can leave a message to communicate.