2011-08-08 3 views
3

BeanUtils.describe처럼 작동하지만 .class에서 작동하는 것이 아닌 객체를 찾고 있습니까? 아무도 도와주지? 현재 나는 아래와 같이 기본 getHeaders 메서드를 사용하여 객체 클래스 목록을 작성하고 있습니다.자바에서 .class의 모든 속성을 얻는 방법?

public class SimpleList<E> { 
    protected final Class<E> clazz; 

    SimpleList(Class<E> clazz) { 
     this.clazz = clazz; 
    } 

    public String[] getHeaders() { 
     Map props = BeanUtils.describe(clazz); // replace this with something 
     return (String[]) props.keySet().toArray(); 
    } 
} 
+1

참고하면 ** **'E.class'을 사용할 수없고 "해결 방법은"이것에도 없다 * 당신은에 액세스 할 수 *하지 않는 한' Class '이 어떤 식 으로든 당신에게 전달되었습니다. –

+0

@ Joachim 좋은 지적 –

+0

@Sean, "왜이 태그가 붙은 겁니까?" ;-) –

답변

10

Introspector API 사용

PropertyDescriptor[] propertyDescriptors = 
    Introspector.getBeanInfo(beanClass).getPropertyDescriptors(); 
List<String> propertyNames = new ArrayList<String>(propertyDescriptors.length); 
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { 
    propertyNames.add(propertyDescriptor.getName()); 
} 
+0

투표까지, 감사합니다 – marioosh

4

당신은 거의 PropertyUtils.getPropertyDescriptors()을 원합니다. PropertyDescriptor 오브젝트의 배열을 리턴하며 그 중 이름을 추출해야합니다.

+0

고맙습니다 :) – marioosh

+0

@Sean의 답은 BeanUtils 라이브러리가 필요없이 작동하며 호출은 * 조금 * 더 길다는 것에 유의하십시오. –

+0

또 다시 당신이 맞아요 :) – marioosh

관련 문제