Get method parameter name using Javassist

I have an instance CtMethod, but I don’t know how to get parameter names (not types) from it. I tried getParameterTypes, but it seems that it only returns types.

I guess this is possible because the libraries I use have no sources, just class files, and I can see the method parameter names in the IDE.

+4
source share
2 answers

Indeed, you can get the names of the arguments, but only if the code was compiled using debugging symbols, otherwise you cannot do it.

, . 4.7.13. LocalVariableTable jvm. , JVM , , !

ctmethod

  CtMethod method = .....;
  MethodInfo methodInfo = method.getMethodInfo();
  LocalVariableAttribute table = methodInfo.getCodeAttribute().getAttribute(javassist.bytecode.LocalVariableAttribute.tag);

, table.

   int numberOfLocalVariables = table.tableLenght(); 

numberOfLocalVariables:

  • 1st: , , Length();
  • 2nd: , this.

:

|this (if non static) | arg1 | arg2 | ... | argN | var1 | ... | varN|

, , , arg2 , 3- . , :

 // remember it an array so it starts in 0, meaning if you want position 3 => use index 2
 int frameWithNameAtConstantPool = table.nameIndex(2); 
 String variableName = methodInfo.getConstPool().getUtf8Info(frameAtConstantPool)

variableName.

. , (Java) Java-. , , paranamer. .

, !

+8

, , "$ 1, $2,...", .

+3

All Articles