I have an abstract class as follows. I want to get all the values of member variables.
public abstract class PARAMS {
public static final String NAME1 = "VAL1";
public static final String NAME2 = "VAL2";
public static final String NAME3 = "VAL3";
}
The values are retrieved using reflection as follows.
Field[] fields = PARAMS.class.getFields();
for (Field field : fields) {
String name = field.getName() ;
String value = (String) field.get(name);
}
This is the first time I am experimenting with reflection. Is this a correct way to achieve the goal? I would like to know what are the pitfalls in using reflection in this case.
You code iterates over both static and private fields. So you should check that you iterate over static fields only.
for (Field field : PARAMS.class.getFields()) {
if (Modifiered.isStatic(field.getModifiers())) continue;
String name = field.getName() ;
String value = (String) field.get(PARAMS.class);
}
NB: as Jon mentioned, for static field access the instance parameter is ignored. However, I prefer passing in the class instead of null since this is a better documentation of the indent.
It is however even better practice to annotate your fields with an annotation so that you only get those fields that you really want no other static fields added by other programmers (or even the Java language behind the scenes). If you do so, your code would look like
for (Field field : PARAMS.class.getFields()) {
if (!field.isAnnotationsPresent(YourAnnotation.class)) continue;
String name = field.getName() ;
String value = (String) field.get(PARAMS.class);
}
It's not quite correct - the argument to get should ideally be null for the sake of readability: the point of that argument is to give it a target for when you're retrieving instance fields.
So your code can be just:
Field[] fields = PARAMS.class.getFields();
for (Field field : fields) {
String name = field.getName() ;
String value = (String) field.get(null);
}
Now, this should work... but what are you going to do with these values? Is there any reason why you want to do this rather than creating an immutable Map<String, String> which is exposed directly?
Reflection is fine where it's necessary, but you haven't given enough information to determine whether it's actually necessary in this case.
another problem, getFields return all accessible fields (static or not) of this class and all its superclasses. Not a problem for the specific code you posted, since the only superclass is Object which has no public field.
I would at least test if the field is declared in the correct class - getDeclaringClass() - and if it has the correct return type - getType().
Using an Annotation, as Adrian suggested, is best IMHO.
Related
Here we go, suppose if i have class Name :
class Name {
String firstName, lastName;
// getters and setters, etc.
}
and then Name class's object is declared somewhere in other class :
class Other {
Name name;
// getter and setters, etc.
}
Now if i do something like :
Other o = new Other();
Field[] fields = o.getClass().getDeclaredFields();
fields[0] --> is 'name' the Object of 'Name' class
but when i say field[0].getClass() :
It gives me java.lang.reflect.Field class object and not Name class object.
How can i get original class object from a field like 'name'
Field.getType method returns a Class object that identifies the declared type for the field represented by this Field object.
This should help
Other o = new Other();
Class<?> classTemp1 = o.getClass();
Field[] allFields = classTemp1.getDeclaredFields();
Now u can query each field for name ,type etc
fields[i].getType()
Please check
http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/Field.html#getType()
getType() Returns a Type object that represents the declared type for the field represented by this Field object.
field[0].getClass()
Will return you the Type object that represents field[0] which is obiviously field[0].
Basically, you need to ask the field for the value of a specific instance, something like
Name name = (Name)fields[0].get(o);
Now. It's pretty dangrous to do a blind cast like this, I'd be probably simply assign it to a Object first and then do instanceof or maybe use Field#getName to determine the name of the field and take action from there...
nb- I'd make mention of getType, but Evgeniy Dorofeev beat me to it and I don't want take away from his answer
Based on Evgeniy's answer, this line is what you are looking for:
String actualClassName = fields[0].getType().getName();
Hi have a class[many] for which I create object dynamically during run time. now I want to set value for the fields which are private fields. How do I set them.
I have seen many examples which explain this but we need to know the field name and only than the values can be set.
for my case I have some set of default values for set of primitive and non primitive types and find the field type during run time and set the default values for them.
For example:
LoginBean loginBean = new LoginBean();
Method setUserName = loginBean.getClass().getMethod("setUserName", new Class[]{String.class});
setUserName.invoke(loginBean, "myLogin");
My case is different and i don't even know the field name but have to set the default value according to field type.
how to do this using reflection or even better in spring.
You can say yourBean.class.getFields(); which will give array of Field.
Using Field you can find its name and type, and do the desired work (setting some value, if its type is == some primitive type)
This example sets default values on several fields within a class using reflection. The fields have private access, which is toggled on and off via reflection. Field.set() is used to set the values of the field on a particular instance instead of using the setter method.
import java.lang.reflect.Field;
import java.util.Date;
public class StackExample {
private Integer field1 = 3;
private String field2 = "Something";
private Date field3;
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
StackExample se = new StackExample();
Field[] fields = se.getClass().getDeclaredFields();
for(Field f:fields){
if(!f.isAccessible()){
f.setAccessible(true);
Class<?> type = f.getType();
if(type.equals(Integer.class)){
f.set(se, 100); //Set Default value
}else if(type.equals(String.class)){
f.set(se, "Default");
}else if (type.equals(Date.class)){
f.set(se, new Date());
}
f.setAccessible(false);
}
System.out.println(f.get(se)); //print fields with reflection
}
}
}
1) By Using Spring Constructor/Setter Injection. You dont need to know the attribute name , just type will do. Like as following:
<bean id="myBean" class="myBean">
<constructor-arg type="int"><value>1</value></constructor-arg>
</bean>
It is possible to access a class attribute by its name using the following code:
MyClass.class.getDeclaredField("myAtt");
is it possible to create a function which do reverse? i.e. a function which convert an attribute to its name?
EDIT: I'm adding this edit to make this more clear:
consider the following class:
class MyClass {
Integer myAttribute;
}
I am looking for a function which accepts myAttribute (itself, or a reference to it. idk!) and returns "myAttribute" string.
Try java.lang.reflect.Field.getName();
You can use Field#getName method : -
Field[] fields = MyClass.class.getDeclaredFields();
for (Field field: fields) {
System.out.println(field.getName());
}
PS: - You should name your classes starting with uppercase letters.
UPDATE: - Ok, if you have your name of the attribute: - private String attrName;, you can get the corresponding field my using: -
Field field = MyClass.class.getDeclaredField("attrName");
// Then do this. Which is meaningless. But I don't know why you want to do this
// Your fieldName will contain "attrName" which you know already
String fieldName = field.getName();
But fetching the name from the above field doesn't make sense. I would be surprised if you were looking for this only. But you can be more clear with your question.
If I understand your question you want to get the name of the Field thats current value matches the value of a variable.
private String getAttributeName(Object myInstance, Object myValue) throws IllegalAccessException
{
Field[] fields = MyClass.class.getDeclaredFields();
for (Field each : fields)
{
if (each.get(myInstance).equals(myValue))
return each.getName();
}
return null;
}
Then you can call it,
getAttributeName(myInstance, myValue);
How will I be able to retrieve the value of a variable which has a dynamic name
For Example I have list of constants
public class Constant{
public static final String S_R = "Standard(240)";
public static final String S_W = "Standard(180)";
public static final String L_R = "Large(360)";
public static final String L_W = "Large(280)";
}
Based on database I build a variable name
String varName = "S" + "_" +"R"; // This can be S_R , S_W , L_R or L_W
String varVal = // How do i get value of S_R
Use a normal HashMap with variable names as strings against their values. Or use a EnumMap with enums as key and your value as values. AFAIK, that's the closest you can get when using Java. Sure, you can mess around with reflection but IMO the map approach is much more logical.
You can use a Map<String, String> and locate the value by its key.
Even better, you can have an enum:
public enum Foo {
S_R("Standard", 240),
S_W("Standard", 180),...;
private String type;
private String duration;
// constructor and getters
}
And then call Foo.valueOf(name)
(You can also do this via reflection - Constants.class.getField(fieldName) and then call field.get(null) (null for static). But that's not really a good approach.)
If you really must do this (and it's unlikely), you would have to use the Java "reflection" APIs.
I've created simple annotation in Java
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.FIELD)
public #interface Column {
String columnName();
}
and class
public class Table {
#Column(columnName = "id")
private int colId;
#Column(columnName = "name")
private String colName;
private int noAnnotationHere;
public Table(int colId, String colName, int noAnnotationHere) {
this.colId = colId;
this.colName = colName;
this.noAnnotationHere = noAnnotationHere;
}
}
I need to iterate over all fields, that are annotated with Column and get name and value of field and annotation. But I've got problem with getting value of each field, since all of them are of different data type.
Is there anything that would return collection of fields that have certain annotation?
I managed to do it with this code, but I don't think that reflection is good way to solve it.
Table table = new Table(1, "test", 2);
for (Field field : table.getClass().getDeclaredFields()) {
Column col;
// check if field has annotation
if ((col = field.getAnnotation(Column.class)) != null) {
String log = "colname: " + col.columnName() + "\n";
log += "field name: " + field.getName() + "\n\n";
// here i don't know how to get value of field, since all get methods
// are type specific
System.out.println(log);
}
}
Do I have to wrap every field in object, which would implement method like getValue(), or is there some better way around this? Basicly all I need is string representation of each field that is annotated.
edit: yep field.get(table) works, but only for public fields, is there any way how to do this even for private fields? Or do I have to make getter and somehow invoke it?
Every object should has toString() defined. (And you can override this for each class to get a more meaningful representation).
So you where your "// here I don't know" comment is, you could have:
Object value = field.get(table);
// gets the value of this field for the instance 'table'
log += "value: " + value + "\n";
// implicitly uses toString for you
// or will put 'null' if the object is null
Reflection is exactly the way to solve it. Finding out things about types and their members at execution time is pretty much the definition of reflection! The way you've done it looks fine to me.
To find the value of the field, use field.get(table)
Reflection is exactly the way to look at annotations. They are a form of "metadata" attached to the class or method, and Java annotations were designed to be examined that way.
Reflection is one way to process the object (probably the only way if the fields are private and don't have any kind of accessor method). You'll need to look at Field.setAccessible and perhaps Field.getType.
Another approach is to generate another class for enumerating the annotated fields using a compile-time annotation processor. This requires a com.sun API in Java 5, but support is better in the Java 6 JDK (IDEs like Eclipse may require special project configuration).