[JSR308] Other questions related to Annotation processing

Artemus Harper subanark at gmail.com
Thu Dec 18 15:05:25 EST 2008


No, I do mean B, not A. There is an annotation on the field, and one  
on the definition of the type of the field.
The tree:
private @B ClsA fieldA = new ClsA();
is a type of VariableTree, it has the annotation of @B

The tree:
fieldA.toString();
is a statement tree, that contains a MethodInvocationTree. The method  
select part of this tree is an IdentifierTree:
fieldA
This identifier doesn't and can't have any annotation declared on it.  
However, the AnnotatedTypeFactory will copy the annotation from  
fieldA's declaration to its use. If you want to change how annotations  
are inferred, then you should subclass the AnnotatedTypeFactory and  
supply your own implementation of the annotateImplicit methods. E.g.  
you could mark a variable use as being @Validated if validate() was  
called on an object.
On Dec 18, 2008, at 11:51 AM, Chester Chen wrote:

> Artemus,
>
>    Thanks for the detailed reply.
>
>    I did not follow your last point :
>
>     >"....you will get annotation A, not annotation B. The  
> AnnotatedTypeMirror will (by default) annotate all uses of fieldA  
> with @B."
>
>    do you mean to say
>
>     ....you will get annotation A, not annotation B. The  
> AnnotatedTypeMirror will (by default) annotate all uses of fieldA  
> with @A.
>
>   Any way, I will tries this out.
>
> Thanks
>
> Chester
> -----Original Message-----
> From: Artemus Harper [mailto:subanark at gmail.com]
> Sent: Thursday, December 18, 2008 10:28 AM
> To: Chester Chen
> Cc: JSR 308 (Annotations) mailing list
> Subject: Re: [JSR308] Other questions related to Annotation processing
>
> 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/76b69c3c/attachment.htm 


More information about the JSR308 mailing list