2012-10-11 98 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

我想這取決於這些註釋是如何相互關聯。

你可以簡單地宣佈所有註釋中@SupportedAnnotationTypes並有幾個塊,如工藝方法:

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

否則您可能能夠使用element.getEnclosedElements()element.getEnclosingElement()達到你想要的東西。

+0

這工作完美。我的來源中有一個問題,它導致了我的錯誤。通過上述解決方案,上面的解決方案處理了嵌套類的註釋。 – sholmes

-1

你需要從ClassMethod一些方法來做到這一點,特別是讓那些方法Foo聲明的類,在這些類的註解,在這些類中聲明的方法和註解。下面是一個簡單的例子:

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

這可能是有見地的,通過關於反射在Java中的文檔閱讀:http://docs.oracle.com/javase/tutorial/reflect/index.html

+3

您正在談論反射API。 OP詢問了註釋處理器。這不是反射。它是對java編譯器的一種擴展。 – AlexR

+0

我正在使用註釋處理工具作爲Java6編譯器的擴展。在編譯時使用Annotation Processing Tool處理文件時,我不知道嵌套的類名。 – sholmes