Re: Getting the local variables of a method
Matthew Wilson <mj.wilson.uk <at> googlemail.com>
2009-11-03 09:32:07 GMT
2009/11/2 MUTHURAMAN SWAMINADHAN <muthu437 <at> yahoo.com>:
> I have been trying to get the local variables in a method but I don't see anything returned. I am using the
following snippet.
>
> LocalVariable lv[];
> LocalVariableTable lvt;
> lvt=mt.getLocalVariableTable(); //mt is a method object
> lv=lvt.getLocalVariableTable();
>
> I don't get anything back in the lv array. Can anyone advise if this is the correct approach or if there is a
better approach?
The local variable table is an optional table in the class file.
Unless you compile with the debug option (which, by default, includes
the local variables, line numbers, and location of the original
source), it will be missing. Try "javac -g" to compile.
See the Java VM specification for more details:
http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#5956
You can also use javap to have a look at the local variable table (to
see if it is there in the file):
javap -verbose MyClass
I hope that helps.
Kind regards,
Matthew
(Continue reading)