How to get Method Parameter names in Java 8 using reflection?

后端 未结 4 1160
终归单人心
终归单人心 2020-11-27 03:55

Java 8 has the ability to acquire method parameter names using Reflection API.

  1. How can I get these method parameter names?

  2. As per my knowled

4条回答
  •  佛祖请我去吃肉
    2020-11-27 04:28

    Thanks Andreas, but finally i got the complete solution from oracle Tutorials on Method Parameters

    It says,

    You can obtain the names of the formal parameters of any method or constructor with the method java.lang.reflect.Executable.getParameters. (The classes Method and Constructor extend the class Executable and therefore inherit the method Executable.getParameters.) However, .class files do not store formal parameter names by default. This is because many tools that produce and consume class files may not expect the larger static and dynamic footprint of .class files that contain parameter names. In particular, these tools would have to handle larger .class files, and the Java Virtual Machine (JVM) would use more memory. In addition, some parameter names, such as secret or password, may expose information about security-sensitive methods.

    To store formal parameter names in a particular .class file, and thus enable the Reflection API to retrieve formal parameter names, compile the source file with the -parameters option to the javac compiler.

    How to Compile

    Remember to compile the with the -parameters compiler option

    Expected Output(For complete example visit the link mentioned above)

    java MethodParameterSpy ExampleMethods

    This command prints the following:

    Number of constructors: 1
    
    Constructor #1
    public ExampleMethods()
    
    Number of declared constructors: 1
    
    Declared constructor #1
    public ExampleMethods()
    
    Number of methods: 4
    
    Method #1
    public boolean ExampleMethods.simpleMethod(java.lang.String,int)
                 Return type: boolean
         Generic return type: boolean
             Parameter class: class java.lang.String
              Parameter name: stringParam
                   Modifiers: 0
                Is implicit?: false
            Is name present?: true
               Is synthetic?: false
             Parameter class: int
              Parameter name: intParam
                   Modifiers: 0
                Is implicit?: false
            Is name present?: true
               Is synthetic?: false
    
    Method #2
    public int ExampleMethods.varArgsMethod(java.lang.String...)
                 Return type: int
         Generic return type: int
             Parameter class: class [Ljava.lang.String;
              Parameter name: manyStrings
                   Modifiers: 0
                Is implicit?: false
            Is name present?: true
               Is synthetic?: false
    
    Method #3
    public boolean ExampleMethods.methodWithList(java.util.List)
                 Return type: boolean
         Generic return type: boolean
             Parameter class: interface java.util.List
              Parameter name: listParam
                   Modifiers: 0
                Is implicit?: false
            Is name present?: true
               Is synthetic?: false
    
    Method #4
    public  void ExampleMethods.genericMethod(T[],java.util.Collection)
                 Return type: void
         Generic return type: void
             Parameter class: class [Ljava.lang.Object;
              Parameter name: a
                   Modifiers: 0
                Is implicit?: false
            Is name present?: true
               Is synthetic?: false
             Parameter class: interface java.util.Collection
              Parameter name: c
                   Modifiers: 0
                Is implicit?: false
            Is name present?: true
               Is synthetic?: false
    

提交回复
热议问题