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
  • 292,901
  • 67
  • 465
  • 588
Java Questions
  • 7,813
  • 41
  • 118
  • 176
  • 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 – Sean Patrick Floyd Apr 30 '13 at 09:29

4 Answers4

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

sanbhat
  • 17,522
  • 6
  • 48
  • 64
1

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

Vineet Singla
  • 1,609
  • 2
  • 20
  • 34
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;

}
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
0
obj.getClass().getSuperclass().getDeclaredFields();

you can use above function for achieving same ..

also check Access to private inherited fields via reflection in Java

Community
  • 1
  • 1
ManMohan Vyas
  • 4,004
  • 4
  • 27
  • 40