Using Java Reflection, is it possible to get the name of a local variable? For example, if I have this:
Foo b = new Foo();
Foo a = new Foo();
Foo r = new Foo();
is it possible to implement a method that can find the names of those variables, like so:
public void baz(Foo... foos)
{
for (Foo foo: foos) {
// Print the name of each foo - b, a, and r
System.out.println(***);
}
}
EDIT: This question is different from Is there a way in Java to find the name of the variable that was passed to a function? in that it more purely asks the question about whether one can use reflection to determine the name of a local variable, whereas the other question (including the accepted answer) is more focused on testing values of variables.
You can do like this:
It is not possible at all. Variable names aren't communicated within Java (and might also be removed due to compiler optimizations).
EDIT (related to comments):
If you step back from the idea of having to use it as function parameters, here's an alternative (which I wouldn't use - see below):
There will be issues if
a == b, a == r, or b == r
or there are other fields which have the same references.EDIT now unnecessary since question got clarified
(Edit: two previous answers removed, one for answering the question as it stood before edits and one for being, if not absolutely wrong, at least close to it.)
If you compile with debug information on (
javac -g
), the names of local variables are kept in the .class file. For example, take this simple class:After compiling with
javac -g:vars TestLocalVarNames.java
, the names of local variables are now in the .class file.javap
's-l
flag ("Print line number and local variable tables") can show them.javap -l -c TestLocalVarNames
shows:The VM spec explains what we're seeing here:
§4.7.9 The
LocalVariableTable
Attribute:The
LocalVariableTable
stores the names and types of the variables in each slot, so it is possible to match them up with the bytecode. This is how debuggers can do "Evaluate expression".As erickson said, though, there's no way to access this table through normal reflection. If you're still determined to do this, I believe the Java Platform Debugger Architecture (JPDA) will help (but I've never used it myself).
As of Java 8, some local variable name information is available through reflection. See the "Update" section below.
Complete information is often stored in class files. One compile-time optimization is to remove it, saving space (and providing some obsfuscation). However, when it is is present, each method has a local variable table attribute that lists the type and name of local variables, and the range of instructions where they are in scope.
Perhaps a byte-code engineering library like ASM would allow you to inspect this information at runtime. The only reasonable place I can think of for needing this information is in a development tool, and so byte-code engineering is likely to be useful for other purposes too.
Update: Limited support for this was added to Java 8. Parameter (a special class of local variable) names are now available via reflection. Among other purposes, this can help to replace
@ParameterName
annotations used by dependency injection containers.see this example :