Two processing questions

Alex Buckley alex.buckley at oracle.com
Mon Dec 16 12:03:24 PST 2013


Mohan, please subscribe to type-annotations-dev to post.

On 12/16/2013 2:47 AM, Mohan Radhakrishnan wrote:
> Hi,
>                I have these two questions.
>
> 1. How do I get the annotations and the targets from within a method ?

There is no access to annotations in a method body from the Language 
Model API (or the Core Reflection API).

> 2. How do I get the annotation applied on the bound ?

Once getBounds() gives you back a list of TypeMirror objects, you don't 
need to turn those mirrors back into elements. Elements are 
declarations, and the declaration of class Integer has no annotations. 
Instead, think of a TypeMirror as a box around the use of the type. In 
Test1, the particular use of "Integer" as a type parameter bound is 
annotated, so the 'typeMirror' variable has everything you need. Just 
call getAnnotation(TypeUse.class) on it. And note that the TypeMirror 
interface extends the AnnotatedConstruct interface, so you can get 
repeated annotations via getAnnotationsByType(Class<T>).

There were some bugs around annotations on type parameter declarations 
in b113-b116, but they should all be resolved in b120.

Alex

> ------------------------Source class--------------------
>
> @TypeUse class Test1<@TypeParameter @TestTypeParameter T extends
> @TypeUse Integer> {
>
>          /**
>           * A type annotation is permitted in front of a constructor
> declaration, where declaration annotations are already
>           permitted.
>           */
>          @TypeUse <@TypeParameter @TestTypeParameter S> Test1(String s){
>
>          }
>      }
>
>
> -----------------Test Processor---------------------
>
>      private void processClassTypeParameters(Element e) {
>          for (TypeParameterElement typeParameterElement : ((TypeElement)
> e).getTypeParameters()) {
>              //System.out.println( "Annotation [" +
> typeParameterElement.getAnnotationMirrors() + "] [" +
> typeParameterElement  + "]");
>              for (TypeMirror typeMirror :
> typeParameterElement.getBounds()) {
>                  TypeElement typeElement = (TypeElement)
> ((DeclaredType)typeMirror).asElement();
>                  System.out.println( "Annotation [" +
> typeElement.getAnnotationMirrors() + "] [" + typeMirror  + "]");
>              }
>          }
>      }
>
>
> Thanks,
> Mohan


More information about the type-annotations-dev mailing list