Java Reflection: How to get the name of a variable?
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.
How to get variable name? - java
You can get all field name by reflection
Class yourClass = YourClass.class
Field[] fields = yourClass.getFields();
for(Field f: fields){
f.getName();
}
or if you want mapping then go for Map
Map<String, String> propertyToValueMap
if you are trying to read method's local variable name, then it is not that simple to fetch also a signal that you are doing something wrong
Getting a variable by name using reflection in Java
You get a field
field = getClass().getDeclaredField(name);
on whatever whatever type this
is, presumably com.whatever.project.Hexagon
. But then you try to retrieve the field on an object of type com.badlogic.gdx.graphics.Color
.
System.out.println(field.get(c));
This is wrong. The javadoc states
Returns the value of the field represented by this
Field
, on the
specified object.
Color
does not have a Color
field.
What you want is probably
field.get(this)
Get all variable names in a class
Field[] fields = YourClassName.class.getFields();
returns an array of all public variables of the class.
getFields()
return the fields in the whole class-heirarcy. If you want to have the fields defined only in the class in question, and not its superclasses, use getDeclaredFields()
, and filter the public
ones with the following Modifier
approach:
Modifier.isPublic(field.getModifiers());
The YourClassName.class
literal actually represents an object of type java.lang.Class
. Check its docs for more interesting reflection methods.
The Field
class above is java.lang.reflect.Field
. You may take a look at the whole java.lang.reflect
package.
Related Topics
How to Get Utf-8 Working in Java Webapps
How to Get the Current Date and Time in Utc or Gmt in Java
How to Make a Deep Copy of an Object
How to Find a Button Source in Awt (Calculator Homework)
Socket Using in a Swing Applet
Why Is the Java Date API (Java.Util.Date, .Calendar) Such a Mess
What Is "String Args[]"? Parameter in Main Method Java
System.Currenttimemillis VS System.Nanotime
Intellij Can't Recognize Javafx 11 With Openjdk 11
How to Access Java-Classes in the Default-Package
Accept Server'S Self-Signed Ssl Certificate in Java Client
Why Are Arrays Covariant But Generics Are Invariant
Maven Shade Javafx Runtime Components Are Missing