2012-07-29 3 views
2

우리는 두 개의 주석을 프로젝트에 가지고 있으며, 주석이 달린 클래스를 수집하고 두 클래스 목록을 기반으로 병합 된 출력을 만들고 싶습니다.동일한 프로세서 인스턴스로 다른 주석 처리하기

Processor 인스턴스 하나만 가능합니까? Processor 인스턴스가 모든 주석이 첨부 된 클래스와 함께 호출되었는지 어떻게 알 수 있습니까?

답변

3

프레임 워크는 Processor.process 메서드를 한 번만 (한 라운드 당) 호출하며 전달 된 RoundEnvironment 매개 변수를 통해 두 목록에 동시에 액세스 할 수 있습니다. 따라서 같은 process 메서드 호출에서 두 목록을 모두 처리 할 수 ​​있습니다.

이 목록을 할 수있는 SupportedAnnotationTypes 주석 모두 주석 :

@Override 
public boolean process(final Set<? extends TypeElement> annotations, 
     final RoundEnvironment roundEnv) { 
    System.out.println(" > ---- process method starts " + hashCode()); 
    System.out.println(" > annotations: " + annotations); 

    for (final TypeElement annotation: annotations) { 
     System.out.println(" > annotation: " + annotation.toString()); 
     final Set<? extends Element> annotateds = 
      roundEnv.getElementsAnnotatedWith(annotation); 
     for (final Element element: annotateds) { 
      System.out.println("  > class: " + element); 
     } 
    } 
    System.out.println(" > processingOver: " + roundEnv.processingOver()); 
    System.out.println(" > ---- process method ends " + hashCode()); 
    return false; 
} 

그리고 출력 :

> ---- process method starts 21314930 
    > annotations: [hu.palacsint.annotation.MyOtherAnnotation, hu.palacsint.annotation.MyAnnotation] 
    > annotation: hu.palacsint.annotation.MyOtherAnnotation 
     > class: hu.palacsint.annotation.p2.OtherClassOne 
    > annotation: hu.palacsint.annotation.MyAnnotation 
     > class: hu.palacsint.annotation.p2.ClassTwo 
     > class: hu.palacsint.annotation.p3.ClassThree 
     > class: hu.palacsint.annotation.p1.ClassOne 
    > processingOver: false 
    > ---- process method ends 21314930 
    > ---- process method starts 21314930 
    > roots: [] 
    > annotations: [] 
    > processingOver: true 
    > ---- process method ends 21314930 

그것은 모든 인쇄 여기

@SupportedAnnotationTypes({ 
    "hu.palacsint.annotation.MyAnnotation", 
    "hu.palacsint.annotation.MyOtherAnnotation" 
}) 
@SupportedSourceVersion(SourceVersion.RELEASE_6) 
public class Processor extends AbstractProcessor { ... } 

은 샘플 process 방법로 주석이 달린 수업또는 MyOtherAnnotation 주석.