2016-08-09 3 views
0

저는 Apache의 PropertUtils를 사용하여 문자열 매개 변수를 전달하여 bean의 값을 가져옵니다. 이 특정한 경우에, 동일한 코드 내가 목록 내부의 모든 개체의 entityId을 읽을 수PropertyUtils를 사용하여 arraylist의 속성을 얻는 방법

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); 
    } 

설명하기 위해 나는 개체의 목록이 나는 목록 내부 객체의 특정 속성을 읽고 싶어. 도움이 되었습니까?

당신은 객체와 속성 이름에 속성베이스의 데이터를 가져 오기 위해이 방법을 사용할 수 있습니다

답변

1

java8 당신이

List<String> entityIds = auditModelDTOs.streams().map(p-> (String) PropertyUtils.getProperty(p, "entityId")).collect(Collectors.toList()); 
+1

고마워, 내가 비슷한 일을 시작하지만 귀하의 자바 8 솔루션은 그냥 간단하게 만들었습니다. – Pratik

0

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; 
    } 
+0

수 스트림 질문에 나와있는 List 예제에서 어떻게 이것을 사용할 수 있습니까? – Pratik

0

같은 전체 기능 :

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; 
} 
관련 문제