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

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.

Leave a Comment