Actually there is a way. We can get the local variable signature by iterating method.getLocalVariableTable().getLocalVariableTable()
. Once we have the signature, there is a Utility class called org.apache.bcel.classfile.Utility
and there is a conversion method Utility.signatureToString(variableSignature)
.
Javadoc of Utility class - https://commons.apache.org/proper/commonsbcel/apidocs/org/apache/bcel/classfile/Utility.html
Posting the example code segment
This is the example java file which the class file will be used for analyzing
public class ExampleClassFile {
public void testClass(int input){
int intVal= 0;
String stringVal= "randomText";
boolean booleanVal= false;
int []intArray = new int[2];
}
}
This is the bcel code for analyzing the above .class file
JavaClass javaClass = Repository.lookupClass("ExampleClassFile");
for(Method method: javaClass.getMethods()){
for(LocalVariable localVariable: method.getLocalVariableTable().getLocalVariableTable()){
System.out.println(Utility.signatureToString(localVariable.getSignature()) + " " + localVariable.getName());
}
}
These are the output results
- int input
- int intVal
- String stringVal
- boolean booleanVal
- int[] intArray