8217381: Incovenient errors reported when annotation processor generates source file and errors in the same round
authorjlahoda
Fri, 15 Feb 2019 12:09:53 +0100
changeset 53773 454d54d8af1c
parent 53772 0eca4e6a0037
child 53774 622c26f0673f
8217381: Incovenient errors reported when annotation processor generates source file and errors in the same round Summary: When an annotation processor reports and error, defer reporting recoverable errors from the erroneous round to the last round, to avoid reporting errors that were resolved in the erroneous round. Reviewed-by: jjg
src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTrees.java
src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java
src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacMessager.java
src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java
src/jdk.compiler/share/classes/com/sun/tools/javac/util/JCDiagnostic.java
src/jdk.compiler/share/classes/com/sun/tools/javac/util/Log.java
test/langtools/tools/javac/6304921/TestLog.java
test/langtools/tools/javac/processing/6994946/SemanticErrorTest.2.out
test/langtools/tools/javac/processing/6994946/TestProcessor.java
test/langtools/tools/javac/processing/GenerateAndError.java
test/langtools/tools/javac/processing/GenerateAndError.out
test/langtools/tools/javac/processing/GenerateAndErrorTest.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTrees.java	Fri Feb 15 05:32:36 2019 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTrees.java	Fri Feb 15 12:09:53 2019 +0100
@@ -1152,7 +1152,7 @@
         try {
             switch (kind) {
             case ERROR:
-                log.error(DiagnosticFlag.MULTIPLE, pos, Errors.ProcMessager(msg.toString()));
+                log.error(DiagnosticFlag.API, pos, Errors.ProcMessager(msg.toString()));
                 break;
 
             case WARNING:
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java	Fri Feb 15 05:32:36 2019 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java	Fri Feb 15 12:09:53 2019 +0100
@@ -1014,7 +1014,11 @@
      * Parses a list of files.
      */
    public List<JCCompilationUnit> parseFiles(Iterable<JavaFileObject> fileObjects) {
-       if (shouldStop(CompileState.PARSE))
+       return parseFiles(fileObjects, false);
+   }
+
+   public List<JCCompilationUnit> parseFiles(Iterable<JavaFileObject> fileObjects, boolean force) {
+       if (!force && shouldStop(CompileState.PARSE))
            return List.nil();
 
         //parse all files
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacMessager.java	Fri Feb 15 05:32:36 2019 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacMessager.java	Fri Feb 15 12:09:53 2019 +0100
@@ -34,6 +34,7 @@
 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
 import com.sun.tools.javac.tree.JCTree;
 import com.sun.tools.javac.tree.JCTree.*;
+import java.util.Set;
 import javax.lang.model.element.*;
 import javax.tools.JavaFileObject;
 import javax.tools.Diagnostic;
@@ -117,7 +118,7 @@
             switch (kind) {
             case ERROR:
                 errorCount++;
-                log.error(DiagnosticFlag.MULTIPLE, pos, Errors.ProcMessager(msg.toString()));
+                log.error(DiagnosticFlag.API, pos, Errors.ProcMessager(msg.toString()));
                 break;
 
             case WARNING:
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java	Fri Feb 15 05:32:36 2019 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java	Fri Feb 15 12:09:53 2019 +0100
@@ -35,6 +35,7 @@
 import java.nio.file.Path;
 import java.util.*;
 import java.util.Map.Entry;
+import java.util.function.Predicate;
 import java.util.regex.*;
 import java.util.stream.Collectors;
 
@@ -83,6 +84,7 @@
 import com.sun.tools.javac.util.DefinedBy.Api;
 import com.sun.tools.javac.util.Iterators;
 import com.sun.tools.javac.util.JCDiagnostic;
+import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
 import com.sun.tools.javac.util.JavacMessages;
 import com.sun.tools.javac.util.List;
 import com.sun.tools.javac.util.Log;
@@ -1066,7 +1068,9 @@
             prev.newRound();
             this.genClassFiles = prev.genClassFiles;
 
-            List<JCCompilationUnit> parsedFiles = compiler.parseFiles(newSourceFiles);
+            //parse the generated files even despite errors reported so far, to eliminate
+            //recoverable errors related to the type declared in the generated files:
+            List<JCCompilationUnit> parsedFiles = compiler.parseFiles(newSourceFiles, true);
             roots = prev.roots.appendList(parsedFiles);
 
             // Check for errors after parsing
@@ -1233,15 +1237,17 @@
         }
 
         void showDiagnostics(boolean showAll) {
-            Set<JCDiagnostic.Kind> kinds = EnumSet.allOf(JCDiagnostic.Kind.class);
-            if (!showAll) {
-                // suppress errors, which are all presumed to be transient resolve errors
-                kinds.remove(JCDiagnostic.Kind.ERROR);
-            }
-            deferredDiagnosticHandler.reportDeferredDiagnostics(kinds);
+            deferredDiagnosticHandler.reportDeferredDiagnostics(showAll ? ACCEPT_ALL
+                                                                        : ACCEPT_NON_RECOVERABLE);
             log.popDiagnosticHandler(deferredDiagnosticHandler);
             compiler.setDeferredDiagnosticHandler(null);
         }
+        //where:
+            private final Predicate<JCDiagnostic> ACCEPT_NON_RECOVERABLE =
+                    d -> d.getKind() != JCDiagnostic.Kind.ERROR ||
+                         !d.isFlagSet(DiagnosticFlag.RECOVERABLE) ||
+                         d.isFlagSet(DiagnosticFlag.API);
+            private final Predicate<JCDiagnostic> ACCEPT_ALL = d -> true;
 
         /** Print info about this round. */
         private void printRoundInfo(boolean lastRound) {
@@ -1335,7 +1341,7 @@
             errorStatus = round.unrecoverableError();
             moreToDo = moreToDo();
 
-            round.showDiagnostics(errorStatus || showResolveErrors);
+            round.showDiagnostics(showResolveErrors);
 
             // Set up next round.
             // Copy mutable collections returned from filer.
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/JCDiagnostic.java	Fri Feb 15 05:32:36 2019 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/JCDiagnostic.java	Fri Feb 15 12:09:53 2019 +0100
@@ -429,9 +429,9 @@
         RECOVERABLE,
         NON_DEFERRABLE,
         COMPRESSED,
-        /** Print multiple errors for same source locations.
+        /** Flag for diagnostics that were reported through API methods.
          */
-        MULTIPLE,
+        API,
         /** Flag for not-supported-in-source-X errors.
          */
         SOURCE_LEVEL;
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Log.java	Fri Feb 15 05:32:36 2019 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Log.java	Fri Feb 15 12:09:53 2019 +0100
@@ -33,6 +33,7 @@
 import java.util.Map;
 import java.util.Queue;
 import java.util.Set;
+import java.util.function.Predicate;
 
 import javax.tools.DiagnosticListener;
 import javax.tools.JavaFileObject;
@@ -158,14 +159,14 @@
 
         /** Report all deferred diagnostics. */
         public void reportDeferredDiagnostics() {
-            reportDeferredDiagnostics(EnumSet.allOf(JCDiagnostic.Kind.class));
+            reportDeferredDiagnostics(d -> true);
         }
 
         /** Report selected deferred diagnostics. */
-        public void reportDeferredDiagnostics(Set<JCDiagnostic.Kind> kinds) {
+        public void reportDeferredDiagnostics(Predicate<JCDiagnostic> accepter) {
             JCDiagnostic d;
             while ((d = deferred.poll()) != null) {
-                if (kinds.contains(d.getKind()))
+                if (accepter.test(d))
                     prev.report(d);
             }
             deferred = null; // prevent accidental ongoing use
@@ -713,7 +714,7 @@
 
             case ERROR:
                 if (nerrors < MaxErrors &&
-                    (diagnostic.isFlagSet(DiagnosticFlag.MULTIPLE) ||
+                    (diagnostic.isFlagSet(DiagnosticFlag.API) ||
                      shouldReport(diagnostic))) {
                     writeDiagnostic(diagnostic);
                     nerrors++;
--- a/test/langtools/tools/javac/6304921/TestLog.java	Fri Feb 15 05:32:36 2019 -0500
+++ b/test/langtools/tools/javac/6304921/TestLog.java	Fri Feb 15 12:09:53 2019 +0100
@@ -76,7 +76,7 @@
         Set<DiagnosticFlag> defaultErrorFlags =
                 (Set<DiagnosticFlag>) defaultErrorFlagsField.get(diagnosticFactory);
 
-        defaultErrorFlags.add(DiagnosticFlag.MULTIPLE);
+        defaultErrorFlags.add(DiagnosticFlag.API);
 
         JavacFileManager.preRegister(context);
         ParserFactory pfac = ParserFactory.instance(context);
--- a/test/langtools/tools/javac/processing/6994946/SemanticErrorTest.2.out	Fri Feb 15 05:32:36 2019 -0500
+++ b/test/langtools/tools/javac/processing/6994946/SemanticErrorTest.2.out	Fri Feb 15 12:09:53 2019 +0100
@@ -1,3 +1,4 @@
+- compiler.err.proc.messager: Deliberate Error
+SemanticErrorTest.java:13:1: compiler.err.proc.messager: Deliberate Error on Trees
 SemanticErrorTest.java:13:46: compiler.err.repeated.interface
-- compiler.err.proc.messager: Deliberate Error
-2 errors
+3 errors
--- a/test/langtools/tools/javac/processing/6994946/TestProcessor.java	Fri Feb 15 05:32:36 2019 -0500
+++ b/test/langtools/tools/javac/processing/6994946/TestProcessor.java	Fri Feb 15 12:09:53 2019 +0100
@@ -27,13 +27,21 @@
 import javax.lang.model.element.*;
 import static javax.tools.Diagnostic.Kind.*;
 
+import com.sun.source.util.TreePath;
+import com.sun.source.util.Trees;
+
 public class TestProcessor extends JavacTestingAbstractProcessor {
    private int round = 0;
 
    public boolean process(Set<? extends TypeElement> annotations,
                   RoundEnvironment roundEnv) {
-        if (++round == 1)
+        if (++round == 1) {
             messager.printMessage(ERROR, "Deliberate Error");
+            Trees trees = Trees.instance(processingEnv);
+            TreePath elPath = trees.getPath(roundEnv.getRootElements().iterator().next());
+            trees.printMessage(ERROR, "Deliberate Error on Trees",
+                               elPath.getLeaf(), elPath.getCompilationUnit());
+        }
         return false;
    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/processing/GenerateAndError.java	Fri Feb 15 12:09:53 2019 +0100
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8217381
+ * @summary Check error are convenient when AP generates a source file and
+ *          an error in the same round
+ * @library /tools/javac/lib
+ * @modules jdk.compiler
+ * @build JavacTestingAbstractProcessor GenerateAndError
+ * @compile/fail/ref=GenerateAndError.out -XDrawDiagnostics -processor GenerateAndError GenerateAndErrorTest.java
+ */
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.*;
+
+import javax.annotation.processing.*;
+import javax.lang.model.element.*;
+import javax.tools.Diagnostic.Kind;
+
+public class GenerateAndError extends JavacTestingAbstractProcessor {
+    int round = 0;
+    @Override
+    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
+        if (round++ == 0) {
+            try (Writer w = processingEnv.getFiler().createSourceFile("Extra").openWriter()) {
+                w.write("public class Extra {}");
+            } catch (IOException ex) {
+                throw new IllegalStateException(ex);
+            }
+            processingEnv.getMessager().printMessage(Kind.ERROR, "error");
+        }
+        return false;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/processing/GenerateAndError.out	Fri Feb 15 12:09:53 2019 +0100
@@ -0,0 +1,3 @@
+- compiler.err.proc.messager: error
+GenerateAndErrorTest.java:2:60: compiler.err.cant.resolve: kindname.class, ExtraExtra, , 
+2 errors
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/processing/GenerateAndErrorTest.java	Fri Feb 15 12:09:53 2019 +0100
@@ -0,0 +1,2 @@
+/* /nodynamiccopyright/ */
+public class GenerateAndErrorTest extends Extra implements ExtraExtra {}