Note that there are some explanatory texts on larger screens.

plurals
  1. POCompiler API + Annotation Processing == double Diagnostic messages?
    primarykey
    data
    text
    <p> I'm combining the Java Compiler API (JSR199) with a custom annotation processor (JSR269). Some syntax errors in Java source code supplied to the compiler are resulting in diagnostic messages appearing twice. Many syntax errors still only cause a single diagnostic message. For example, unmatch curly braces only result in a single message but an import with an invalid qualified identifier result in two diagnostic messages but only when an annotation processor is in use.</p> <p>Here is some sample input that will cause the problem:</p> <pre class="lang-java prettyprint-override"><code>import javax.xml.bind.annotation; // missing ".*" public class Test { } </code></pre> <p>For an annotation processor I have the following. (I've tried returning false from process and I've also tried implementing Processor by hand instead of extending AbstractProcessor.)</p> <pre class="lang-java prettyprint-override"><code>@SupportedSourceVersion(SourceVersion.RELEASE_7) @SupportedAnnotationTypes("*") public class AnnotationProcessor extends AbstractProcessor { @Override public boolean process(Set&lt;? extends TypeElement&gt; annotations, RoundEnvironment roundEnv) { return true; } } </code></pre> <p>Here is some of the code used to invoke the compiler and add the annotation processor.</p> <pre class="lang-java prettyprint-override"><code>javax.tools.DiagnosticCollector&lt;javax.tools.JavaFileObject&gt; diagnostics = new javax.tools.DiagnosticCollector&lt;&gt;(); javax.tools.StandardJavaFileManager fileManager = javac.getStandardFileManager(diagnostics, null, null); fileManager.setLocation(javax.tools.StandardLocation.CLASS_OUTPUT, Arrays.asList(tempDir.toFile())); javax.tools.JavaCompiler.CompilationTask task = javac.getTask(out, fileManager, diagnostics, null, null, compilationUnits); List&lt;Processor&gt; processors = new ArrayList&lt;&gt;(); AnnotationProcessor ap = new AnnotationProcessor(); processors.add(ap); task.setProcessors(processors); Boolean compiled = task.call(); for (Diagnostic&lt;? extends JavaFileObject&gt; diag : diagnostics.getDiagnostics()) { out.println(diag.toString()); } </code></pre> <p>My output looks like:</p> <pre class="lang-none prettyprint-override"><code>/Test.java:1: error: cannot find symbol import javax.xml.bind.annotation; ^ symbol: class annotation location: package javax.xml.bind /Test.java:1: error: cannot find symbol import javax.xml.bind.annotation; ^ symbol: class annotation location: package javax.xml.bind </code></pre> <p>If I take out the <code>processors.add(ap);</code> line then the duplicate error message disappears. Adding multiple processors has no additional effect.</p> <p>Any idea why an annotation processor causes duplicate diagnostic messages when using the compiler API? (And only for some syntax errors at that)</p>
    singulars
    1. This table or related slice is empty.
    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.
    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