This discussion is archived
2 Replies Latest reply: Apr 9, 2012 8:37 PM by 801313 RSS

Having trouble getting my annotation to process

801313 Explorer
Currently Being Moderated
I have an annotation that I'm working with from an annotation tutorial:

package com.tjacobs.annotation
<pre>
public @interface Complexity {
     public enum Level {
VERY_SIMPLE, SIMPLE, MEDIUM, COMPLEX, VERY_COMPLEX;
}
     Level value() default Level.MEDIUM;
}
</pre>

Here is the processor I built to handle it:

<pre>
@SupportedAnnotationTypes("com.tjacobs.annotation.Complexity")
@SupportedSourceVersion(javax.lang.model.SourceVersion.RELEASE_6)
public class ComplexityProcessor extends AbstractProcessor {

public ComplexityProcessor() {
super();
}

@Override
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
     for (Element elem : roundEnv.getElementsAnnotatedWith(Complexity.class)) {
Complexity complexity = elem.getAnnotation(Complexity.class);
String message = "annotation found in " + elem.getSimpleName()
+ " with complexity " + complexity.value();
processingEnv.getMessager().printMessage(javax.tools.Diagnostic.Kind.NOTE, message);
try {
                    JavaFileObject jfo = processingEnv.getFiler().createSourceFile("Foo");
                    BufferedWriter bw = new BufferedWriter(jfo.openWriter());
                    bw.append("Bar!!!");
                    bw.close();
               } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
               }

}
return true; // no further processing of this annotation type
}
</pre>

I created a file "Processor" in META-INF/services with the following content:

com.tjacobs.annotation.processors.ComplexityProcessor

I jarred up the processor class and the META_INF/services, added it as an annotation processor in eclipse.

Then I created a test class

@Complexity
public class AnnoTest {

     /**
     * @param args
     */
     public static void main(String[] args) {
          // TODO Auto-generated method stub

     }

}

But I'm not seeing any generated output. What am I doing wrong?

Edited by: tjacobs01 on Apr 5, 2012 12:09 PM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points