0

I am using Apache's PropertUtils to get values of bean by passing string parameter. In this particular case I have List of Object and I want to read specific property of an object inside the list, same code to explain

 List<AuditModelDTO> auditModelDTOs = new ArrayList<>();

    AuditModelDTO amd1 = new AuditModelDTO();
    amd1.setEntityId("e1");
    amd1.setParamResponse(false);

    AuditModelDTO amd2 = new AuditModelDTO();
    amd2.setEntityId("e2");
    amd2.setParamResponse(true);

    auditModelDTOs.add(amd1);
    auditModelDTOs.add(amd2);

    Object requiredObjectProperty = null;

    try {
        requiredObjectProperty = PropertyUtils.getProperty(auditModelDTOs,"get().entityId");
        IndexedProperty(auditModelDTOs,"get(0).entityId",1);
    } catch (Exception e) {
        log.error("Caller does not have access to the property accessor method. Exception thrown is {}", e);
        throw new AuditException(AuditError.ILLEGAL_ACCESS_FOR_PROPERTY_ACCESSOR, e);
    } 

I want to read entityId of all the object inside the list. Any help?

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
Pratik
  • 952
  • 2
  • 16
  • 31

3 Answers3

2

java8 streams

List<String> entityIds = auditModelDTOs.streams().map(p-> (String) PropertyUtils.getProperty(p, "entityId")).collect(Collectors.toList());
kuhajeyan
  • 10,727
  • 10
  • 46
  • 71
  • 1
    Thanks a lot, I started doing something similar but your java 8 solution just made it simpler. – Pratik Aug 09 '16 at 06:42
0

You can use this method to get data of property base on object and property name

public static String getPropertyValue(Object object, String propertyName) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException
    {
        String strValue = "";
        Class< ? > c = object.getClass();
        Field f = c.getDeclaredField(propertyName);
        f.setAccessible(true);
        Object value = f.get(object);
        if (value != null)
        {
            strValue = value.toString();
        }
        return strValue;
    }
BIZ
  • 113
  • 6
0

The full feature like that :

public static void main(String[] args)
    {
    List<AuditModelDTO> auditModelDTOs = new ArrayList<>();

    AuditModelDTO amd1 = new AuditModelDTO();
    amd1.setEntityId("e1");
    amd1.setParamResponse(false);

    AuditModelDTO amd2 = new AuditModelDTO();
    amd2.setEntityId("e2");
    amd2.setParamResponse(true);

    auditModelDTOs.add(amd1);
    auditModelDTOs.add(amd2);

    System.out.println("EntityId list : " + getEntityId(auditModelDTOs));

}

// GetEntityIdList
public static List<String> getEntityIdList(List<Object> auditModelDTOs)
    throws SecurityException,
        IllegalArgumentException,
        NoSuchFieldException,
        IllegalAccessException
{
    List<String> entityIdList = new ArrayList<String>();
    String propertyName = "entityId";
    for (Object object : auditModelDTOs)
    {
        if (object != null)
        {
            entityIdList.add(getPropertyValue(object, propertyName));
        }
    }
    return entityIdList;
}

 // getPropertyValue
public static String getPropertyValue(Object object, String propertyName)
    throws NoSuchFieldException,
        SecurityException,
        IllegalArgumentException,
        IllegalAccessException
{
    String strValue = "";
    Class< ? > c = object.getClass();
    Field f = c.getDeclaredField(propertyName);
    f.setAccessible(true);
    Object value = f.get(object);
    if (value != null)
    {
        strValue = value.toString();
    }
    return strValue;
}
BIZ
  • 113
  • 6