Skip to content

Commit 5e24aaf

Browse files
author
Vicente Romero
committed
8320001: javac crashes while adding type annotations to the return type of a constructor
Reviewed-by: cushon, jlahoda
1 parent f9e9131 commit 5e24aaf

File tree

2 files changed

+143
-1
lines changed

2 files changed

+143
-1
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2332,7 +2332,12 @@ public Void visitMethodSymbol(Symbol.MethodSymbol s, Void unused) {
23322332
thrown.add(addTypeAnnotations(thrownType, thrownType(i++)));
23332333
}
23342334
mt.thrown = thrown.toList();
2335-
mt.restype = addTypeAnnotations(mt.restype, TargetType.METHOD_RETURN);
2335+
/* possible information loss if the type of the method is void then we can't add type
2336+
* annotations to it
2337+
*/
2338+
if (!mt.restype.hasTag(TypeTag.VOID)) {
2339+
mt.restype = addTypeAnnotations(mt.restype, TargetType.METHOD_RETURN);
2340+
}
23362341

23372342
Type recvtype = mt.recvtype != null ? mt.recvtype : s.implicitReceiverType();
23382343
if (recvtype != null) {
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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 8320001
27+
* @summary javac crashes while adding type annotations to the return type of a constructor
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 TypeAnnosOnConstructorsTest
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 TypeAnnosOnConstructorsTest extends TestRunner {
58+
protected ToolBox tb;
59+
60+
TypeAnnosOnConstructorsTest() {
61+
super(System.err);
62+
tb = new ToolBox();
63+
}
64+
65+
public static void main(String... args) throws Exception {
66+
new TypeAnnosOnConstructorsTest().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 testAnnoOnConstructors(Path base) throws Exception {
79+
Path src = base.resolve("src");
80+
Path y = src.resolve("Y.java");
81+
Path classes = base.resolve("classes");
82+
83+
Files.createDirectories(classes);
84+
85+
tb.writeJavaFiles(src,
86+
"""
87+
import java.lang.annotation.Target;
88+
import java.lang.annotation.ElementType;
89+
import java.lang.annotation.Retention;
90+
import java.lang.annotation.RetentionPolicy;
91+
92+
class Y {
93+
@TA public Y() {}
94+
}
95+
96+
@Target(ElementType.TYPE_USE)
97+
@Retention(RetentionPolicy.RUNTIME)
98+
@interface TA {}
99+
""");
100+
101+
// we need to compile Y first
102+
new JavacTask(tb)
103+
.files(y)
104+
.outdir(classes)
105+
.run();
106+
107+
Path classDir = getClassDir();
108+
new JavacTask(tb)
109+
.classpath(classes, classDir)
110+
.options("-processor", SimpleProcessor.class.getName())
111+
.classes("Y")
112+
.outdir(classes)
113+
.run(Task.Expect.SUCCESS);
114+
}
115+
116+
public Path getClassDir() {
117+
String classes = ToolBox.testClasses;
118+
if (classes == null) {
119+
return Paths.get("build");
120+
} else {
121+
return Paths.get(classes);
122+
}
123+
}
124+
125+
@SupportedAnnotationTypes("*")
126+
public static final class SimpleProcessor extends AbstractProcessor {
127+
@Override
128+
public SourceVersion getSupportedSourceVersion() {
129+
return SourceVersion.latestSupported();
130+
}
131+
132+
@Override
133+
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
134+
return false;
135+
}
136+
}
137+
}

0 commit comments

Comments
 (0)