Skip to content

Commit 01ea1ef

Browse files
author
Vicente Romero
committed
8305971: NPE in JavacProcessingEnvironment for missing enum constructor body
Reviewed-by: darcy
1 parent 1d54e73 commit 01ea1ef

File tree

2 files changed

+132
-1
lines changed

2 files changed

+132
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1650,7 +1650,7 @@ public void visitClassDef(JCClassDecl node) {
16501650
public void visitMethodDef(JCMethodDecl node) {
16511651
// remove super constructor call that may have been added during attribution:
16521652
if (TreeInfo.isConstructor(node) && node.sym != null && node.sym.owner.isEnum() &&
1653-
node.body.stats.nonEmpty() && TreeInfo.isSuperCall(node.body.stats.head) &&
1653+
node.body != null && node.body.stats.nonEmpty() && TreeInfo.isSuperCall(node.body.stats.head) &&
16541654
node.body.stats.head.pos == node.body.pos) {
16551655
node.body.stats = node.body.stats.tail;
16561656
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright (c) 2023, 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 8305971
27+
* @summary NPE in JavacProcessingEnvironment for missing enum constructor body
28+
* @library /tools/lib /tools/javac/lib
29+
* @modules
30+
* jdk.compiler/com.sun.tools.javac.api
31+
* jdk.compiler/com.sun.tools.javac.main
32+
* @build toolbox.ToolBox toolbox.JavacTask
33+
* @run main CrashEmptyEnumConstructorTest
34+
*/
35+
36+
import java.io.IOException;
37+
38+
import java.nio.file.Files;
39+
import java.nio.file.Path;
40+
import java.nio.file.Paths;
41+
42+
import java.util.List;
43+
import java.util.Set;
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+
50+
import toolbox.JavacTask;
51+
import toolbox.Task;
52+
import toolbox.Task.Mode;
53+
import toolbox.Task.OutputKind;
54+
import toolbox.TestRunner;
55+
import toolbox.ToolBox;
56+
57+
public class CrashEmptyEnumConstructorTest extends TestRunner {
58+
protected ToolBox tb;
59+
60+
CrashEmptyEnumConstructorTest() {
61+
super(System.err);
62+
tb = new ToolBox();
63+
}
64+
65+
public static void main(String... args) throws Exception {
66+
new CrashEmptyEnumConstructorTest().runTests();
67+
}
68+
69+
protected void runTests() throws Exception {
70+
runTests(m -> new Object[]{Paths.get(m.getName())});
71+
}
72+
73+
Path[] findJavaFiles(Path... paths) throws IOException {
74+
return tb.findJavaFiles(paths);
75+
}
76+
77+
@Test
78+
public void testEmptyEnumConstructor(Path base) throws Exception {
79+
Path src = base.resolve("src");
80+
Path r = src.resolve("E");
81+
82+
Path classes = base.resolve("classes");
83+
84+
Files.createDirectories(classes);
85+
86+
tb.writeJavaFiles(r,
87+
"""
88+
enum E {
89+
ONE("");
90+
E(String one);
91+
}
92+
""");
93+
94+
List<String> expected = List.of(
95+
"E.java:3: error: missing method body, or declare abstract",
96+
" E(String one);",
97+
" ^",
98+
"1 error");
99+
100+
List<String> log = new JavacTask(tb)
101+
.options("-processor", SimpleProcessor.class.getName())
102+
.files(findJavaFiles(src))
103+
.outdir(classes)
104+
.run(Task.Expect.FAIL)
105+
.writeAll()
106+
.getOutputLines(Task.OutputKind.DIRECT);
107+
108+
if (log.size() != expected.size()) {
109+
throw new AssertionError("Unexpected output: " + log);
110+
} else {
111+
for (int i = 0; i < expected.size(); i++) {
112+
if (!log.get(i).contains(expected.get(i))) {
113+
throw new AssertionError("Unexpected output: " + log);
114+
}
115+
}
116+
}
117+
}
118+
119+
@SupportedAnnotationTypes("*")
120+
public static final class SimpleProcessor extends AbstractProcessor {
121+
@Override
122+
public SourceVersion getSupportedSourceVersion() {
123+
return SourceVersion.latestSupported();
124+
}
125+
126+
@Override
127+
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
128+
return false;
129+
}
130+
}
131+
}

0 commit comments

Comments
 (0)