클래스가 아닌 인스턴스에서 Field 참조를 가져 오는 방법이 있습니까? 인스턴스에서 필드 가져 오기?
이
은 예입니다 : 너무 명확 없었던public class Element {
@MyAnnotation("hello")
public String label1;
@MyAnnotation("world")
public String label2;
}
public class App {
private Element elem = new Element();
public void printAnnotations() {
String elemLabel1 = elem1.label;
String elemLabel2 = elem2.label;
// cannot do elemLabel.getField().getDeclaredAnnotations();
String elemLabel1AnnotationValue = // how ?
String elemLabel2AnnotationValue = // how ?
}
}
죄송합니다,하지만 난 이미 클래스에서 필드를 가져 오는 방법을 알고 (클래스 -> 필드 -> DeclaredAnnotations)
내가 궁금한 것은 특정 인스턴스에 대해 Field를 가져 오는 방법입니다. 이 예제에서는 elemLabel1 string 인스턴스에서 Element.label1 필드를 가져올 수 있기를 원합니다.
내게는 당신이 당신의 표제와 다른 샘플을 요구하고있는 것처럼 보입니다. 주석이나 필드를 가져 오려고하십니까? –
실제로 특정 필드에 대한 주석을 얻을 수 있기 전에 실제로 필드를 가져오고 있습니다. 따라서 필자의 의견으로는 필드를 얻는 것에 관한 것입니다. – bertie
질문의 표현 방식에 따라 클래스에서 데이터를 가져 오는 방법을 이미 알고 있지만 인스턴스에서 정보를 얻으려는 것처럼 들립니다. 그렇다면'.getClass()'가 도움이 될 것입니다. –