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?
-
Closely related, but for listing all local variables instead: stackoverflow.com/questions/5355101/…Ciro Santilli OurBigBook.com– Ciro Santilli OurBigBook.com03/05/2015 07:57:20Commented Mar 5, 2015 at 7:57
5 Answers 5
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.
-
Interesting (but hardly known) project here that attempts to reconstruct the local variable table in that fashion (bytecode inspection). It's on my 'check it out later' list... The gist of it seems to be just finding all the locals-accessing opcodes github.com/mathewmarcus/latte/blob/main/app/src/main/java/latte/…Computer says 'no'--SOooooo– Computer says 'no'--SOooooo07/03/2024 06:04:59Commented Jul 3, 2024 at 6:04
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.
-
5See my answer. Briefly, if you compile with "-g", the information should be present.Stephen C– Stephen C07/26/2011 13:43:04Commented Jul 26, 2011 at 13:43
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.
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.
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.
-
11 for the "local variables are on code block level"Gaurav– Gaurav06/11/2018 13:31:45Commented Jun 11, 2018 at 13:31