[JSR308] help on a simple example

Mahmood Ali mahmood at MIT.EDU
Tue Oct 28 11:33:41 EDT 2008

Greetings Florin,

Thanks for describing your problem better.  Supplying the necessary  
files helps us reproduce the behavior and understand the problem.

The compiler here is behaving as expected actually.  As you have  
observed, the compiler is not emitting a syntax error for  
'List<@def.Annotation String>'.  This indicates that your setup and  
configuration is correct.

The problem here is your reliance on  
RoundEnvironment.getElementsAnnotatedWith().  Type annotations (e.g.  
within type argument, method receivers, etc) do not annotate elements,  
but rather annotate types.  Hence, .getElementsAnnotatedWith() would  
only return elements that are annotated in pre-JSR308 manner (e.g.  
annotations on class declaration).

Try the following checker:

public class MyAnnotationProcessor extends SourceChecker {
   protected SourceVisitor<?,?>  
createSourceVisitor(CompilationUnitTree tree) {
     return new SourceVisitor<Void, Void>(this, tree) {
       public Void visitAnnotatedType(AnnotatedTypeTree node, Void p) {
           System.out.println("found: " + node);
           return super.visitAnnotatedType(node, p);

The provided checker has a tree visitor that traverse your code (i.e.  
AST nodes) looking for annotated types, so it should print 'found:  
@def.Annotation String' on your input.


More information about the JSR308 mailing list