Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a custom AbstractProcessor and integrating with Eclipse
    primarykey
    data
    text
    <p>I'm trying to create a new annotation with which I'll do some runtime wiring, but, for a number of reasons, I'd like to verify at compile time that my wiring will be successful with some rudimentary checks.</p> <p>Suppose I create a new annotation:</p> <pre><code>@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface CustomAnnotation{ } </code></pre> <p>Now I want to do some kind of validation at compile time, like check the field that <code>CustomAnnotation</code> annotates is of a particular type: <code>ParticularType</code>. I'm working in Java 6, so I created an <code>AbstractProcessor</code>:</p> <pre><code>@SupportedAnnotationTypes("com.example.CustomAnnotation") public class CompileTimeAnnotationProcessor extends AbstractProcessor { @Override public boolean process(Set&lt;? extends TypeElement&gt; annotations, RoundEnvironment roundEnv) { Set&lt;? extends Element&gt; elements = roundEnv.getElementsAnnotatedWith(CustomAnnotation.class); for(Element e : elements){ if(!e.getClass().equals(ParticularType.class)){ processingEnv.getMessager().printMessage(Kind.ERROR, "@CustomAnnotation annotated fields must be of type ParticularType"); } } return true; } } </code></pre> <p>Then, based on some instructions I found, I created a folder <code>META-INF/services</code> and created a file <code>javax.annotation.processing.Processor</code> with contents:</p> <pre><code> com.example.CompileTimeAnnotationProcessor </code></pre> <p>Then, I exported the project as a jar.</p> <p>In another project, I built a simple test class:</p> <pre><code>public class TestClass { @CustomAnnotation private String bar; // not `ParticularType` } </code></pre> <p>I configured the Eclipse project properties as follows:</p> <ul> <li>Set Java Compiler -> Annotation Processing: "Enable annotation processing" and "Enable processing in editor"</li> <li>Set Java Compiler -> Annotation Processing -> Factory Path to include my exported jar and checked under advanced that my fully qualified class shows up.</li> </ul> <p>I clicked "apply" and Eclipse prompts to rebuild the project, I hit okay -- but no error is thrown, despite having the annotation processor.</p> <p>Where did I go wrong?</p> <hr> <p>I ran this using <code>javac</code> as</p> <pre><code>javac -classpath "..\bin;path\to\tools.jar" -processorpath ..\bin -processor com.example.CompileTimeAnnotationProcessor com\test\TestClass.java </code></pre> <p>with output</p> <blockquote> <p>@CustomAnnotation annotated fields must be of type ParticularType</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload