Skip to content

Commit 44ae643

Browse files
committed
8210649: AssertionError @ jdk.compiler/com.sun.tools.javac.comp.Modules.enter(Modules.java:244)
Do not clean trees after last round of annotation processing, if the trees won't be re-entered again. Reviewed-by: vromero
1 parent 04ad75e commit 44ae643

3 files changed

Lines changed: 168 additions & 15 deletions

File tree

src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,16 +1036,12 @@ public List<JCCompilationUnit> parseFiles(Iterable<JavaFileObject> fileObjects,
10361036
return trees.toList();
10371037
}
10381038

1039-
/**
1040-
* Enter the symbols found in a list of parse trees if the compilation
1041-
* is expected to proceed beyond anno processing into attr.
1042-
* As a side-effect, this puts elements on the "todo" list.
1043-
* Also stores a list of all top level classes in rootClasses.
1044-
*/
1045-
public List<JCCompilationUnit> enterTreesIfNeeded(List<JCCompilationUnit> roots) {
1046-
if (shouldStop(CompileState.ATTR))
1047-
return List.nil();
1048-
return enterTrees(initModules(roots));
1039+
/**
1040+
* Returns true iff the compilation will continue after annotation processing
1041+
* is done.
1042+
*/
1043+
public boolean continueAfterProcessAnnotations() {
1044+
return !shouldStop(CompileState.ATTR);
10491045
}
10501046

10511047
public List<JCCompilationUnit> initModules(List<JCCompilationUnit> roots) {

src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,21 +1429,25 @@ public boolean doProcessing(List<JCCompilationUnit> roots,
14291429

14301430
errorStatus = errorStatus || (compiler.errorCount() > 0);
14311431

1432-
round.finalCompiler();
14331432

14341433
if (newSourceFiles.size() > 0)
14351434
roots = roots.appendList(compiler.parseFiles(newSourceFiles));
14361435

14371436
errorStatus = errorStatus || (compiler.errorCount() > 0);
14381437

1439-
// Free resources
1440-
this.close();
1441-
14421438
if (errorStatus && compiler.errorCount() == 0) {
14431439
compiler.log.nerrors++;
14441440
}
14451441

1446-
compiler.enterTreesIfNeeded(roots);
1442+
if (compiler.continueAfterProcessAnnotations()) {
1443+
round.finalCompiler();
1444+
compiler.enterTrees(compiler.initModules(roots));
1445+
} else {
1446+
compiler.todo.clear();
1447+
}
1448+
1449+
// Free resources
1450+
this.close();
14471451

14481452
if (!taskListener.isEmpty())
14491453
taskListener.finished(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING));
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/**
25+
* @test
26+
* @bug 8210649
27+
* @summary Check that diagnostics can be printed even after the compilation
28+
* stopped.
29+
* @library /tools/lib
30+
* @modules jdk.compiler/com.sun.tools.javac.api
31+
* jdk.compiler/com.sun.tools.javac.main
32+
* @build toolbox.TestRunner toolbox.ToolBox T8210649
33+
* @run main T8210649
34+
*/
35+
36+
import java.io.OutputStream;
37+
import java.nio.file.Files;
38+
import java.nio.file.Path;
39+
import java.nio.file.Paths;
40+
import java.util.List;
41+
import java.util.Set;
42+
import java.util.stream.Collectors;
43+
44+
import javax.annotation.processing.AbstractProcessor;
45+
import javax.annotation.processing.RoundEnvironment;
46+
import javax.annotation.processing.SupportedAnnotationTypes;
47+
import javax.lang.model.SourceVersion;
48+
import javax.lang.model.element.TypeElement;
49+
import javax.tools.DiagnosticCollector;
50+
import javax.tools.JavaCompiler;
51+
import javax.tools.JavaFileObject;
52+
import javax.tools.StandardJavaFileManager;
53+
import javax.tools.ToolProvider;
54+
55+
import java.io.IOException;
56+
import java.util.ArrayList;
57+
import java.util.function.Consumer;
58+
import javax.tools.DiagnosticListener;
59+
import toolbox.JavacTask;
60+
import toolbox.TestRunner;
61+
import toolbox.TestRunner.Test;
62+
import toolbox.ToolBox;
63+
64+
public class T8210649 extends TestRunner {
65+
66+
public static void main(String... args) throws Exception {
67+
new T8210649().runTests(m -> new Object[] { Paths.get(m.getName()) });
68+
}
69+
70+
private final ToolBox tb = new ToolBox();
71+
72+
public T8210649() {
73+
super(System.err);
74+
}
75+
76+
@Test
77+
public void testErrorsAfter(Path outerBase) throws Exception {
78+
Path libSrc = outerBase.resolve("libsrc");
79+
tb.writeJavaFiles(libSrc,
80+
"package lib;\n" +
81+
"@lib2.Helper(1)\n" +
82+
"public @interface Lib {\n" +
83+
"}",
84+
"package lib2;\n" +
85+
"public @interface Helper {\n" +
86+
" public int value() default 0;\n" +
87+
"}");
88+
Path libClasses = outerBase.resolve("libclasses");
89+
Files.createDirectories(libClasses);
90+
new JavacTask(tb)
91+
.outdir(libClasses.toString())
92+
.files(tb.findJavaFiles(libSrc))
93+
.run()
94+
.writeAll();
95+
Files.delete(libClasses.resolve("lib2").resolve("Helper.class"));
96+
Path src = outerBase.resolve("src");
97+
tb.writeJavaFiles(src,
98+
"package t;\n" +
99+
"import lib.Lib;\n" +
100+
"public class T {\n" +
101+
" Undefined<String> undef() {}\n" +
102+
"}");
103+
Path classes = outerBase.resolve("classes");
104+
Files.createDirectories(classes);
105+
Path tTarget = classes.resolve("t").resolve("T.class");
106+
Files.createDirectories(tTarget.getParent());
107+
try (OutputStream in = Files.newOutputStream(tTarget)) {}
108+
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
109+
try (StandardJavaFileManager sjfm = compiler.getStandardFileManager(null, null, null)) {
110+
Consumer<DiagnosticListener<JavaFileObject>> runCompiler = dl -> {
111+
try {
112+
List<String> options = List.of("-processor", "T8210649$P",
113+
"-processorpath", System.getProperty("test.classes"),
114+
"-classpath", libClasses.toString() + ":" + classes.toString(),
115+
"-d", classes.toString());
116+
ToolProvider.getSystemJavaCompiler()
117+
.getTask(null, null, dl, options, null, sjfm.getJavaFileObjects(tb.findJavaFiles(src)))
118+
.call();
119+
} catch (IOException ex) {
120+
throw new IllegalStateException(ex);
121+
}
122+
};
123+
124+
List<String> expected = new ArrayList<>();
125+
runCompiler.accept(d -> expected.add(d.getMessage(null)));
126+
127+
DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<>();
128+
runCompiler.accept(dc);
129+
130+
List<String> actual = dc.getDiagnostics()
131+
.stream()
132+
.map(d -> d.getMessage(null))
133+
.collect(Collectors.toList());
134+
if (!expected.equals(actual)) {
135+
throw new IllegalStateException("Unexpected output: " + actual);
136+
}
137+
}
138+
}
139+
140+
@SupportedAnnotationTypes("*")
141+
public static class P extends AbstractProcessor {
142+
@Override
143+
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
144+
return false;
145+
}
146+
147+
@Override
148+
public SourceVersion getSupportedSourceVersion() {
149+
return SourceVersion.latest();
150+
}
151+
}
152+
153+
}

0 commit comments

Comments
 (0)