2012-10-11 18 views
6

Próbuję napisać procesor adnotacji przy użyciu java. Ten procesor adnotacji musi zidentyfikować opisywane zagnieżdżone klasy w ramach opisanej klasy, jak widać poniżej. Najpierw przetworzę przypisane klasy, a następnie przetworzę ich wewnętrzne adnotacje. Jest to wykonywane w czasie kompilacji i nie będę posiadał żadnej wiedzy na temat przetwarzanej klasy. Możliwe jest posiadanie wielu klas zagnieżdżonych w Foo. Jak przetwarzać adnotacje wszystkich zagnieżdżonych klas.APT Jak przetwarzać adnotacje zagnieżdżonych opisów klas

@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 }  
    } 
} 

Jak mogę uzyskać dostęp do pod-klasy Bar, to adnotacja „MyAnnotation” i jego „MyMethodAnnotation” podczas przetwarzania? Poniższy kod wyświetla jedynie informacje o klasie Foo. Jak mogę przetworzyć informacje o barze?

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

Staram dostępu element na zagnieżdżonych klasy kreskowego przy dla (element E: env.getElementsAnnotatedWith (EmfInfo.class)) { ale to tylko zwraca zewnętrzną najbardziej class Foo. – sholmes

Odpowiedz

0

Myślę, że to zależy od tego jak te adnotacja odnoszą się do siebie nawzajem.

Można po prostu zadeklarować wszelkie adnotacje w @SupportedAnnotationTypes i kilka bloków w procesie-metody jak:

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

Inaczej może być w stanie używać element.getEnclosedElements() i element.getEnclosingElement() aby osiągnąć to, co chcesz.

+0

To działało idealnie. Właściwie to miałem problem z moim źródłem, który powodował mi błędy. Dzięki temu rozwiązaniu powyższe rozwiązanie przetworzyło adnotacje klasy zagnieżdżonej. – sholmes

-1

Musisz kilka metod z Class i Method to zrobić, specjalnie w celu uzyskania klasy zadeklarowane w Foo adnotacje na temat tych klas, metod zadeklarowanych w tych klasach, a adnotacje na tych metod . Oto krótki przykład:

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

To może być wnikliwe zapoznanie się z dokumentacją na temat refleksji w Javie: http://docs.oracle.com/javase/tutorial/reflect/index.html

+3

Mówisz o refleksyjnym API. OP zapytał o procesor adnotacji. To nie jest odbicie. To jest inne API, które jest rodzajem rozszerzenia do kompilatora java. – AlexR

+0

Używam narzędzia Annotation Processing do przedłużenia kompilatora w Javie6. Nie znam zagnieżdżonych nazw klas podczas przetwarzania plików przy użyciu narzędzia Annotation Processing Tool w czasie kompilacji. – sholmes

Powiązane problemy