2

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?

Sean Patrick Floyd
300k72 gold badges480 silver badges597 bronze badges
asked Apr 30, 2013 at 8:57
1
  • 1
    you should wrap this check around the other checks: if(! Modifier.isFinal(f.getModifiers())) instead of doing the same check over and over again Commented Apr 30, 2013 at 9:29

4 Answers 4

1

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;
}
answered Apr 30, 2013 at 9:08
5
  • Thanks for the nice explanation +1. will do this and mark this as answer as this will help others too. Commented Apr 30, 2013 at 9:17
  • @Anto Glad I could help. If you have any further questions let me know. Commented Apr 30, 2013 at 9:18
  • Kevin i get only field name like the following userId when i use getSuperclass(). but i need to get like private java.lang.String com.hexgen.ro.request.CreateRequisitionRO.custodianN is it possible Commented Apr 30, 2013 at 9:51
  • getDeclaredFields should pull the private fields also. Commented Apr 30, 2013 at 9:57
  • let us continue this discussion in chat Commented Apr 30, 2013 at 10:00
1

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

answered Apr 30, 2013 at 9:01
1

Use getSuperclass() method to obtain the class object of superclass. and then in the similar way get the fields of superclass.

answered Apr 30, 2013 at 9:01
0
obj.getClass().getSuperclass().getDeclaredFields();

you can use above function for achieving same ..

also check Access to private inherited fields via reflection in Java

answered Apr 30, 2013 at 9:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.