Goal :
set the value for a class fields as well the extended class fields
Example :
public class CreateRequisitionRO extends AbstractPortfolioSpecificRO {....}
i am able to set value for the CreateRequisitionRO
but not able to set value for AbstractPortfolioSpecificRO
which is being extended.
This is how i set the value :
public Object creatObjectWithDefaultValue(String className) throws IllegalArgumentException, IllegalAccessException, InstantiationException {
Class<?> objectClass = null;
Object clsObject =null;
try {
objectClass = Class.forName(className);
clsObject = objectClass.newInstance();
Field[] fields = objectClass.getDeclaredFields();
for(Field f:fields){
if(!f.isAccessible()){
f.setAccessible(true);
Class<?> type = f.getType();
if(! Modifier.isFinal(f.getModifiers()) && type.equals(Integer.class)){
f.set(clsObject, DefaultParamValuesEnum.INTEGER.getDefaultInt());
} else if( !Modifier.isFinal(f.getModifiers()) && type.equals(java.math.BigDecimal.class)){
f.set(clsObject, DefaultParamValuesEnum.BIGDECIMAL.getDdefaultBigDecimal());
} else if(! Modifier.isFinal(f.getModifiers()) && type.equals(org.joda.time.LocalDate.class)){
f.set(clsObject,DefaultParamValuesEnum.DATE.getDefaultDate());
} else if(! Modifier.isFinal(f.getModifiers()) && type.equals(boolean.class)){
f.set(clsObject, DefaultParamValuesEnum.BOOLEAN.getDefaultBoolean());
} else if(! Modifier.isFinal(f.getModifiers()) && type.equals(java.lang.String.class)){
f.set(clsObject, DefaultParamValuesEnum.STRING.getDefaultString());
}
else if(! Modifier.isFinal(f.getModifiers()) && type.equals(long.class)){
f.set(clsObject,DefaultParamValuesEnum.LONGVALUE.getDefaultLong());
}
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return clsObject;
}
This is how i call the above method :
classObject.creatJSONObject("com.hexgen.ro.request.CreateRequisitionRO");
The above way sets value for all the fields available in CreateRequisitionRO
not fields which are available in extended class.
How to set it?
4 Answers 4
You can use clazz.getSuperclass()
to retrieve the supertype of CreateRequisitionRO
. Once you have a handle on the supertype AbstractPortfolioSpecificRO
you can iterate its fields. I would recommend storing all of these fields in a List<Field>
. Iterate through this list and set the fields based upon your conditional logic.
I have provided a quick example of how to perform this. In my example, Child
extends Parent
. The recursive getFields
method returns a List<Field>
that contains all of the fields for Child
. I then iterate through the List<Field>
setting the value for each one. Notice that I must switch the accessibility of private fields.
Child.Java
public class Child extends Parent {
public String a;
public String b;
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
Child child = new Child();
List<Field> fields = getFields(child.getClass());
for(Field f: fields){
f.setAccessible(true);
f.set(child, "Test");
}
for(Field fd: fields){
System.out.println(fd.get(child));
fd.setAccessible(false);
}
}
public static List<Field> getFields(Class<?> clazz){
List<Field> list = new ArrayList<Field>();
list.addAll(Arrays.asList(clazz.getDeclaredFields()));
if(clazz.getSuperclass() != null){
list.addAll(getFields(clazz.getSuperclass()));
}
return list;
}
}
Parent.java
public class Parent {
private String x;
public String y;
protected String z;
}
-
Thanks for the nice explanation +1. will do this and mark this as answer as this will help others too.Java Questions– Java Questions04/30/2013 09:17:03Commented Apr 30, 2013 at 9:17
-
@Anto Glad I could help. If you have any further questions let me know.Kevin Bowersox– Kevin Bowersox04/30/2013 09:18:14Commented Apr 30, 2013 at 9:18
-
Kevin i get only field name like the following
userId
when i usegetSuperclass().
but i need to get likeprivate java.lang.String com.hexgen.ro.request.CreateRequisitionRO.custodianN
is it possibleJava Questions– Java Questions04/30/2013 09:51:40Commented Apr 30, 2013 at 9:51 -
getDeclaredFields
should pull the private fields also.Kevin Bowersox– Kevin Bowersox04/30/2013 09:57:50Commented Apr 30, 2013 at 9:57 -
let us continue this discussion in chatJava Questions– Java Questions04/30/2013 10:00:11Commented Apr 30, 2013 at 10:00
You can get super class of your class using Class<SuperClass> superClazz = clazz.getSuperclass();
Once got, get the Field[]
using superClazz.getFields()
and use set
method of Field
Use getSuperclass()
method to obtain the class object of superclass. and then in the similar way get the fields of superclass.
obj.getClass().getSuperclass().getDeclaredFields();
you can use above function for achieving same ..
also check Access to private inherited fields via reflection in Java
if(! Modifier.isFinal(f.getModifiers()))
instead of doing the same check over and over again