[JSR308] Other questions related to Annotation processing

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


If you use getUnderlyingType() you will end up with the definition of  
the class, and get annotation A. If you simply look at the annotations  
of fieldA (from AnnotatedTypeMirror) you will get annotation B. I  
would have to look at the implementation, but you might get annotation  
A for fieldA if its declaration was not annotated with any  
annotations. As usual try it and see what you get.
On Dec 18, 2008, at 12:30 PM, Chester Chen wrote:

> Thanks for the further clarification. It is helpful.
>
> But I am still confused by your original statement:
>
> >"....you will get annotation A, not annotation B. The  
> AnnotatedTypeMirror will (by default) annotate all uses of fieldA
> > with @B."
>
>
> If I going through the AST tree node,
>
>  1) I visit class ClsA, as for the Annotation, I got @A
>  2) I visit class ClsB, visit the field A, ask field's annotation, I  
> got @B
>  3) I visit method ClsB.m, found method Select identified fieldA.
>
>    if "the AnnotatedTypeFactory will copy the annotation from
> fieldA's declaration to its use", then identifier fieldA will has  
> annotation of @B.
>
>    If that's the case, do you mean by ""....you will get annotation  
> A, not annotation B." then ?
>
> Thanks
>
> Chester
>
>
>
>
>
>
>
>
> -----Original Message-----
> From: Artemus Harper [mailto:subanark at gmail.com]
> Sent: Thu 12/18/2008 12:05 PM
> To: Chester Chen
> Cc: JSR 308 (Annotations) mailing list
> Subject: Re: [JSR308] Other questions related to Annotation processing
>
> 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/d7a06745/attachment-0001.htm 


More information about the JSR308 mailing list