Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get information about the local variables using Java reflection?

I need to know the type of the local variables. I am using Java reflection, using which I could not get it. Can you please let me know how to know the type/name of the local variables.

Can I get information about the local variables using Java reflection?

like image 773
Easwaramoorthy K Avatar asked Jul 25 '11 13:07

Easwaramoorthy K


1 Answers

Assuming that you are talking about a method or constructor's local variables, you cannot find out about them using reflection. You have to either

  • use a bytecode library such as BCEL or ASM, or
  • use one of the remote debugger APIs.

The latter will allow you to access the values of the local variables, but only while the JVM is suspended by the debug agent.

Both of these approaches rely on the classes in question being compiled with debug information. Specifically, the classes need to be compiled with "local variable debugging information"; e.g. using javac -g .... The "vars" debug information is not included by default.

like image 94
Stephen C Avatar answered Oct 12 '22 21:10

Stephen C