2012-10-11 2 views
6

Java를 사용하여 주석 프로세서를 작성하려고합니다. 이 주석 프로세서는 아래와 같이 주석이 달린 클래스에서 주석이있는 중첩 클래스를 식별해야합니다. 주석이 달린 클래스를 먼저 처리 한 다음 내부 주석을 처리합니다. 이것은 컴파일 시간에 수행되며 처리중인 클래스에 대한 기존 지식이 없습니다. Foo 내에서 여러 중첩 클래스를 가질 수 있습니다. 이러한 모든 중첩 클래스의 주석을 어떻게 처리합니까?APT 중첩 된 주석이 달린 클래스의 주석을 처리하는 방법

@MyAnnotation(value="Something important") 
public class Foo 
{ 
    private Integer A; 

    @MyMethodAnnotation(value="Something Else") 
    public Integer getA() { return this.A; } 

    @MyAnnotation(value="Something really important") 
    private class Bar 
    { 
     private Integer B; 

     @MyMethodAnnotation(value="Something Else that is very Important") 
     public Integer getB() { return this.B }  
    } 
} 

어떻게 처리하는 동안, 그것은 주석 'MyAnnotation'의 그 'MyMethodAnnotation'중첩 된 바 클래스에 대한 액세스 권한을 얻을 수 있나요? 다음 코드는 Foo 클래스에 대한 정보 만 출력합니다. Bar 정보를 어떻게 처리합니까?

for (Element element : env.getElementsAnnotatedWith(MyAnnotation.class)) { 
    if (element.getKind().equals(ElementKind.CLASS)) 
    { 
     System.out.println(element.getKind().name() + " " + element.getSimpleName()); 
     processInnerClassElement(element); 
    } 
    else 
    { 
     System.out.println(element.getKind().name() + " " + element.getSimpleName()); 
    }  
} 

... 


private void processInnerClassElement(Element element) 
{ 
    for (Element e : element.getEnclosedElements()) 
    { 
     if (e.getKind().equals(ElementKind.CLASS)) 
     { 
      System.out.println(e.getKind().name() + " " + e.getSimpleName()); 
      processInnerClassElement(e); 
     } 
     else 
     { 
      System.out.println(e.getKind().name() + " " + e.getSimpleName() ); 
     } 
    } 
} 
+0

나는 (요소 E : env.getElementsAnnotatedWith (EmfInfo.class))에 대한 를 사용하여 중첩 된 클래스 모음의 요소에 접근하려고 { 하지만이 바깥 쪽 가장 Foo 클래스를 반환합니다. – sholmes

답변

0

내가이 주석이 서로 어떻게 관계를 맺고 있는지를 따라 추측 여기에 빠른 예입니다. 그렇지 않으면 당신은 당신이 원하는 것을 달성하기 위해 element.getEnclosedElements()element.getEnclosingElement()을 사용할 수 있습니다

for (Element element : roundEnv.getElementsAnnotatedWith(MyAnnotation.class)) { 
    MyAnnotation myAnnotation = element.getAnnotation(MyAnnotation.class); 
    if (myAnnotation != null) { 
     doSomething(myAnnotation, element); 
    } 
} 

for (Element element : roundEnv.getElementsAnnotatedWith(MyMethodAnnotation.class)) { 
    MyMethodAnnotation myMethodAnnotation = element.getAnnotation(MyMethodAnnotation.class); 
    if (myMethodAnnotation != null) { 
     doSomething(myMethodAnnotation, element); 
    } 
} 

:

당신은 단순히 같은 프로세스 방식으로 여러 블록을 @SupportedAnnotationTypes의 모든 주석을 선언하고있을 수 있습니다.

+0

이것은 완벽하게 작동했습니다. 사실 내 원본에 문제가있어서 오류가 발생했습니다. 위에서 수정 된 솔루션은 중첩 클래스의 주석을 처리했습니다. – sholmes

-1

당신은 특별히 그 방법에 Foo에서 선언 된 클래스, 그 클래스에 대한 주석, 이러한 클래스에 선언 된 메소드, 주석을 얻기 위해,이 작업을 수행 ClassMethod에서 몇 가지 방법이 필요합니다 .

public static void main(String... args) { 
    for (Class<?> declaredClass : Foo.class.getDeclaredClasses()) { 
     MyAnnotation myAnnotation = declaredClass.getAnnotation(MyAnnotation.class); 
     // Process value of class annotation here 
     for (Method method : declaredClass.getDeclaredMethods()) { 
      MyMethodAnnotation myMethodAnnotation = method.getAnnotation(MyMethodAnnotation.class); 
      // Process value of method annotation here 
     } 
    } 
} 

자바의 반사에 대한 문서를 읽어 통찰력 수 있습니다 : http://docs.oracle.com/javase/tutorial/reflect/index.html

+3

리플렉션 API에 대해 이야기하고 있습니다. OP는 주석 처리기에 대해 질문했습니다. 그것은 성찰이 아닙니다. Java 컴파일러의 확장 기능 중 하나 인 API입니다. – AlexR

+0

Java6에서 컴파일러의 확장 인 Annotation Processing Tool을 사용하고 있습니다. 컴파일 시간에 주석 처리 도구를 사용하여 파일을 처리하는 동안 중첩 된 클래스 이름을 알지 못합니다. – sholmes