Skip to content

Commit 96a2184

Browse files
committed
Backport c40e6ef3dcb25b8acc156aa4051694322bf7e351
1 parent e9f01f4 commit 96a2184

File tree

4 files changed

+116
-3
lines changed

4 files changed

+116
-3
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import com.sun.tools.javac.jvm.ClassFile.Version;
6262
import com.sun.tools.javac.jvm.PoolConstant.NameAndType;
6363
import com.sun.tools.javac.main.Option;
64+
import com.sun.tools.javac.resources.CompilerProperties.Errors;
6465
import com.sun.tools.javac.resources.CompilerProperties.Fragments;
6566
import com.sun.tools.javac.resources.CompilerProperties.Warnings;
6667
import com.sun.tools.javac.util.*;
@@ -2202,9 +2203,17 @@ public void run() {
22022203
* 4.7.20-A target_type to locate the correct type to rewrite, and then interpreting the JVMS
22032204
* 4.7.20.2 type_path to associate the annotation with the correct contained type.
22042205
*/
2205-
private static void addTypeAnnotationsToSymbol(
2206-
Symbol s, List<Attribute.TypeCompound> attributes) {
2207-
new TypeAnnotationSymbolVisitor(attributes).visit(s, null);
2206+
private void addTypeAnnotationsToSymbol(Symbol s, List<Attribute.TypeCompound> attributes) {
2207+
try {
2208+
new TypeAnnotationSymbolVisitor(attributes).visit(s, null);
2209+
} catch (CompletionFailure ex) {
2210+
JavaFileObject prev = log.useSource(currentClassFile);
2211+
try {
2212+
log.error(Errors.CantAttachTypeAnnotations(attributes, s.owner, s.name, ex.getDetailValue()));
2213+
} finally {
2214+
log.useSource(prev);
2215+
}
2216+
}
22082217
}
22092218

22102219
private static class TypeAnnotationSymbolVisitor

src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,6 +2121,11 @@ compiler.warn.annotation.method.not.found=\
21212121
compiler.warn.annotation.method.not.found.reason=\
21222122
Cannot find annotation method ''{1}()'' in type ''{0}'': {2}
21232123

2124+
# 0: list of annotation, 1: symbol, 2: name, 3: message segment
2125+
compiler.err.cant.attach.type.annotations=\
2126+
Cannot attach type annotations {0} to {1}.{2}:\n\
2127+
{3}
2128+
21242129
# 0: file object, 1: symbol, 2: name
21252130
compiler.warn.unknown.enum.constant=\
21262131
unknown enum constant {1}.{2}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright (c) 2024, Alphabet LLC. 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 8337998
27+
* @summary CompletionFailure in getEnclosingType attaching type annotations
28+
* @library /tools/javac/lib /tools/lib
29+
* @modules jdk.compiler/com.sun.tools.javac.api
30+
* jdk.compiler/com.sun.tools.javac.main
31+
*/
32+
33+
import toolbox.*;
34+
import toolbox.Task.*;
35+
36+
import java.nio.file.Path;
37+
import java.nio.file.Paths;
38+
import java.util.List;
39+
40+
public class CompletionErrorOnEnclosingType {
41+
ToolBox tb = new ToolBox();
42+
43+
public static void main(String... args) throws Exception {
44+
CompletionErrorOnEnclosingType t = new CompletionErrorOnEnclosingType();
45+
t.testMissingEnclosingType();
46+
}
47+
48+
void testMissingEnclosingType() throws Exception {
49+
String annoSrc =
50+
"""
51+
import static java.lang.annotation.ElementType.TYPE_USE;
52+
import java.lang.annotation.Target;
53+
@Target(TYPE_USE)
54+
@interface Anno {}
55+
56+
class A<E> {}
57+
58+
class B {
59+
private @Anno A<String> a;
60+
}
61+
""";
62+
String cSrc =
63+
"""
64+
class C {
65+
B b;
66+
}
67+
""";
68+
69+
Path base = Paths.get(".");
70+
Path src = base.resolve("src");
71+
tb.createDirectories(src);
72+
tb.writeJavaFiles(src, annoSrc, cSrc);
73+
Path out = base.resolve("out");
74+
tb.createDirectories(out);
75+
new JavacTask(tb).outdir(out).files(tb.findJavaFiles(src)).run();
76+
77+
// now if we remove A.class there will be an error but javac should not crash
78+
tb.deleteFiles(out.resolve("A.class"));
79+
List<String> log =
80+
new JavacTask(tb)
81+
.outdir(out)
82+
.classpath(out)
83+
.options("-XDrawDiagnostics")
84+
.files(src.resolve("C.java"))
85+
.run(Expect.FAIL)
86+
.writeAll()
87+
.getOutputLines(Task.OutputKind.DIRECT);
88+
89+
var expectedOutput =
90+
List.of(
91+
"B.class:-:-: compiler.err.cant.attach.type.annotations: @Anno, B, a,"
92+
+ " (compiler.misc.class.file.not.found: A)",
93+
"1 error");
94+
if (!expectedOutput.equals(log)) {
95+
throw new Exception("expected output not found: " + log);
96+
}
97+
}
98+
}

test/langtools/tools/javac/diags/examples.not-yet.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ compiler.err.signature.doesnt.match.intf # UNUSED
4141
compiler.err.signature.doesnt.match.supertype # UNUSED
4242
compiler.err.source.cant.overwrite.input.file
4343
compiler.err.stack.sim.error
44+
compiler.err.cant.attach.type.annotations # bad class file
4445
compiler.err.type.var.more.than.once # UNUSED
4546
compiler.err.type.var.more.than.once.in.result # UNUSED
4647
compiler.err.unexpected.type

0 commit comments

Comments
 (0)