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.

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.

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.

Yup. Brief and correct. – CPerkins Jul 25 at 13:44 See my answer.

Briefly, if you compile with "-g", the information should be present. – Stephen C Jul 26 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 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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions