Hello,
I am new to BCEL and I am writing a program to get the local variables
of each method in a class. When I run this code for few classes I get
the LocalVariableTable but for most of the classes I get a null value
for the LocalVariableTable. When I look at the source code (java file) I
see lots of local variables in the methods. Also, the
Code.getMaxLocals() for each method show a number other than zero. I am
confused as to why the variable table is null. Can someone please shed
some light ?
public void process(String classname)
{
JavaClass javaClass = Repository.lookupClass(classname);
if ( javaClass != null )
{
Method[] methods = javaClass.getMethods();
System.out.println("No. of methods: " + methods.length
);
for ( int i=0; i < methods.length; i++ )
{
System.out.println("\n\n================================" );
System.out.println("Method: " +
methods[i].getName() );
System.out.println("================================" );
Code methodCode = methods[i].getCode();
LocalVariableTable localVariableTable =
methodCode.getLocalVariableTable();
System.out.println("MaxLocalVars: " +
methods[i].getCode().getMaxLocals() );
Attribute[] methodCodeAttr =
methodCode.getAttributes();
if ( methodCodeAttr != null )
{
for ( int k=0; k < methodCodeAttr.length;
k++ )
{
System.out.println("methodCodeAttr["
+ k + "] : " +
methodCodeAttr[k] );
}
}
else
{
System.out.println("CodeAttributes is
NULL");
}
if ( localVariableTable != null )
{
for (int j=0; j <
localVariableTable.getTableLength(); j++ )
{
LocalVariable variable =
localVariableTable.getLocalVariable(j);
String signature =
variable.getSignature();
System.out.print(convertSignature(signature) + " ");
System.out.println(variable.getName() );
}
}
else
{
System.out.println("localVariableTable.length : " + localVariableTable
);
System.out.println("No local variables in
this method" );
}
}
}
else
{
System.out.println("Could not find the class '" +
classname + "' in CLASSPATH" );
}
}
Thanks,
Kannan