[JSR308] Other questions related to Annotation processing

Artemus Harper subanark at gmail.com
Thu Dec 18 13:28:19 EST 2008


You need to ask the AnnotatedTypeFactory for a AnnotatedTypeMirror for  
the tree node. E.g.
AnnotatedTypeMirror type = atypeFactory.getAnnotatedType(node);

 From AnnotatedTypeMirror you can use directSuperTypes() to get the  
super types, or use any other method to get annotation information.

Not every tree has an assoicated Element. In particular any tree node  
within a method does not have an Element. However, you can use  
getUnderlyingType() in AnnotatedTypeMirror to get the TypeMirror. From  
there if the type is a DeclaredType (check with getKind()) you can  
cast to DeclaredType and get its Element. Pass the Element back into  
the AnnotatedTypeFactory to get the annotations.

Note: doing this will give you the element of the declared type, not  
the element of the declaration. E.g.

@A public class ClsA { }
public class ClsB {
    private @B ClsA fieldA= new ClsA();
    public void m() {
        fieldA.toString();
    }
}

If you use the above technique to get the Element, you will get  
annotation A, not annotation B. The AnnotatedTypeMirror will (by  
default) annotate all uses of fieldA with @B. In general you should  
not ask Element for annotations, as this ignores any implicit  
annotations that might be added, and those that can only be added in  
JSR-308 (e.g. class A extends @Anno B).
On Dec 18, 2008, at 9:21 AM, Chester Chen wrote:

> 1.  How do I traverse over an ancestors or referenced classes ?
>
>     Once I found the Tree node in the AST, how to I find the class,  
> parent class and their annoation information ?
>
> 2.How I can get Element from a Tree Node ?
>
>
> Thanks
>
> Chester
>
> _______________________________________________
> JSR308 mailing list
> JSR308 at lists.csail.mit.edu
> https://lists.csail.mit.edu/mailman/listinfo/jsr308

-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://lists.csail.mit.edu/pipermail/jsr308/attachments/20081218/15cabb3a/attachment.htm 


More information about the JSR308 mailing list