22

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?

Andrew Thompson
169k41 gold badges224 silver badges438 bronze badges
asked Jul 25, 2011 at 13:36
1

5 Answers 5

21

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.

answered Jul 25, 2011 at 13:44
1
9

In a word, you can't. The names of local variables are not preserved by the compiler.

As a quick experiment, I have compiled the following class using Java 6 and default compiler options:

public class X {
 public static void main(String[] args) {
 int var = 2;
 System.out.println(var);
 }
}

A quick examination of the resulting .class file reveals that the name of the local variable (var) didn't make it there.

answered Jul 25, 2011 at 13:41
1
  • 5
    See my answer. Briefly, if you compile with "-g", the information should be present. Commented Jul 26, 2011 at 13:43
6

No it is not possible with Java Reflection. Things like local variable names are usually removed by the compiler to provide some obfuscation and to optimize space. There is a byte code library ASM which can inspect the state of things at runtime, which may be useful to you.

answered Jul 25, 2011 at 13:42
3

If my local variables you mean instance variables and class variables, here's how you would go:

String s = new String("This is a sample");
Class<String> type = s.getClass();
for ( Field f : type.getFields() ) {
 System.out.printf("Field %s is of type %s%n", f.getName(), f.getType().getName());
}

If what you mean is variables local to methods/constructors, you can not access them with reflection.

answered Jul 25, 2011 at 13:40
3

You can get access to local variable map using bytecode reverse engineering libraries like ASM. Note however that the name of local variables might not always be present in the bytecode, but the number and types will always be there.

There is no way to obtain this information via reflection. Reflection works on method level, while local variables are on code block level.

answered Jul 25, 2011 at 13:42
1
  • 1
    1 for the "local variables are on code block level" Commented Jun 11, 2018 at 13:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.