Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the parameter names of an interface method [duplicate]

Tags:

java

spring

I need to get the parameter names of a method at runtime. Is there any way to achieve this?

I tried using LocalVariableTableParameterNameDiscoverer which is a Spring class. But it works only for classes and not the interfaces.

like image 576
Parag A Avatar asked Nov 02 '22 16:11

Parag A


1 Answers

The only way to obtain the parameter names is to compile the source code using -g:vars options. This generates a LocalVariableTable attribute into the class file. This is generally removed for optimization.

For example: (taken from here)

public class Example {
   public int plus(int a){
     int b = 1;
     return a + b;
   }
 }

This will create a LocalVariableTable as follows

LocalVariableTable:

   Start  Length  Slot  Name     Signature
   0      6       0     this     LExample;
   0      6       1     a        I
   2      4       2     b        I

The LocalVariableTable can be read using any byte code instrumentation libraries like ASM or Javassist.

like image 118
Santosh Avatar answered Nov 13 '22 22:11

Santosh