How to iterate over all Java bean properties

Below is my bean structure. Employee.java is the parent bean. I would like to iterate over all properties up to Zip.java and manipulate the values.

I tried to repeat this using reflection, but getDeclaredFields () will only give fields to the top level object. How to iterate deeper objects.

Can anyone let me know how to do this in java.

Employee.java

private String id;
private String name;
private int age;
private Address addr;
private Contact cont;

Address.java

private String addr1;
private String addr2;
private String city;
private Zip zip;

Contact.java

private String phone;
private String email;

Zip.java

private String zipCd;
private String zipExt;
+4
source share
5 answers

getDeclaredFields ()

for (Field field : yourObject.getClass().getDeclaredFields()) {
//do stuff
}
+1
source

! JPA Hibernate , JAXB JSON/XML/ ..

, , , :

package myOwnPackage;
import java.lang.reflect.Field;


class Address {
    private String addr1;
    private String addr2;
    private String city;
    private Zip zip;
}
class Contact {
    private String phone;
    private String email;
}
class Employee {
    private String id;
    private String name;
    private int age;
    private Address addr;
    private Contact cont;

    public void setAddr(Address addr) {
        this.addr = addr;
    }
}

class Zip {
    private String zipCd;
    private String zipExt;
}

public class Main {

    public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {

        Employee employee = new Employee();
        employee.setAddr(new Address());

        printFields("", employee);
    }

    private static void printFields(String prefix, Object container) throws IllegalAccessException {

        Class<? extends Object> class1 = null; 
        Package package1 = null;

        if (container != null)
            class1 = container.getClass();

        if (class1 != null)
            package1 = class1.getPackage();

        if (package1 == null || !"myOwnPackage".equals(package1.getName())) {
            System.out.println(container);
            return;
        }

        for (Field field : class1.getDeclaredFields()) {
            System.out.print(prefix+field.getName()+": ");

            // make private fields accessible
            field.setAccessible(true);

            Object value = field.get(container);
            printFields(prefix+"  ", value);
        }
    }
}

:

  • ,
+1

java-, -

for (Field f : Employee.class.getClass().getDeclaredFields()) {

  } 
0

POJO arraylist POJO

Public class ModelPojo{

private ArrayList<Employee > employeeList;
private ArrayList<Address> employeeList;
private ArrayList<Zip> employeeList;
private ArrayList<Contact> employeeList;
class Employee {}
class Address{}
class Contact{}
class Zip{}

}

.

public class Logic {
someMethod{
ModelPojo.Employee employee ;
        ArrayList<Employee > empList = new ArrayList<Employee >();
        for(int i=0;i<empList .size();i++){
            employee = new ModelPojo(). new Employee ();
            employee .set***("");
            employee .set***("");


            empList .add(product);
        }
}
}

0

I did this, but I know, and I strongly feel that this is considered poor programming. I did it because someone forced me, though! (The best solution, I think, would be to write more code ... this was the fastest).

public String getNestedDeclaredField(Object obj, String fieldName) {
    if (null == fieldName) {
        return null;
    }
    String[] fieldNames = fieldName.split("\\.");
    Field field = null;
    Class<? extends Object> requestClass = obj.getClass();

    for (String s : fieldNames) {
        try
        {
            field = getSuperClassField(requestClass,
                    requestClass.getSimpleName(), s);
            field.setAccessible(true);
            obj = field.get(obj);

            if (null == obj) {
               return null;
            }
            requestClass = obj.getClass();
        }

        catch (Exception e) {
            log.error("Error while retrieving field {} from {}", s,
                    requestClass.toString(), e);
            return "";
        }
    }

    return obj.toString();
}

public Field getSuperClassField(Class<? extends Object> clazz,
        String clazzName, String fieldName) throws NoSuchFieldException {
    Field field = null;

    try{
        field = clazz.getDeclaredField(fieldName);
    }

    catch (NoSuchFieldException e) {
        clazz = clazz.getSuperclass();

        if (StringUtils.equals(clazz.getSimpleName(), "Object")) {
            log.error("Field {} doesn't seem to appear in class {}",
                    fieldName, clazzName);
            throw new NoSuchFieldException();
        }
        field = getSuperClassField(clazz, clazzName, fieldName);
    }

    catch (Exception e) {
        log.error("Error while retrieving field {} from {}", fieldName,
                clazz.toString(), e);
    }

    return field;
}

Where fieldName (for zipCd): addr.zip.zipCd

It will retrieve the String zipCd value from the Employee object.

0
source

All Articles