Skip to content

Commit 0bd2f9c

Browse files
committed
8361445: javac crashes on unresolvable constant in @SuppressWarnings
Reviewed-by: asotona, liach
1 parent 27e6a4d commit 0bd2f9c

File tree

2 files changed

+119
-3
lines changed

2 files changed

+119
-3
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Lint.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,9 @@ private EnumSet<LintCategory> suppressionsFrom(Attribute.Compound suppressWarnin
530530
EnumSet<LintCategory> result = LintCategory.newEmptySet();
531531
Attribute.Array values = (Attribute.Array)suppressWarnings.member(names.value);
532532
for (Attribute value : values.values) {
533-
Optional.of((String)((Attribute.Constant)value).value)
533+
Optional.of(value)
534+
.filter(val -> val instanceof Attribute.Constant)
535+
.map(val -> (String) ((Attribute.Constant) val).value)
534536
.flatMap(LintCategory::get)
535537
.filter(lc -> lc.annotationSuppression)
536538
.ifPresent(result::add);

test/langtools/tools/javac/recovery/AnnotationRecovery.java

Lines changed: 116 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 8270139
26+
* @bug 8270139 8361445
2727
* @summary Verify error recovery w.r.t. annotations
2828
* @library /tools/lib
2929
* @modules jdk.compiler/com.sun.tools.javac.api
@@ -33,8 +33,10 @@
3333
*/
3434

3535
import java.nio.file.Path;
36+
import java.util.HashSet;
3637
import java.util.List;
3738
import java.util.Objects;
39+
import java.util.Set;
3840

3941
import toolbox.JavacTask;
4042
import toolbox.Task.Expect;
@@ -110,4 +112,116 @@ public void testRepeatableAnnotationWrongAttribute() throws Exception {
110112
}
111113
}
112114

115+
@Test //JDK-8361445
116+
public void testSuppressWarningsErroneousAttribute1() throws Exception {
117+
String code = """
118+
@SuppressWarnings(CONST)
119+
public class Test {
120+
public static final String CONST = "";
121+
}
122+
""";
123+
Path curPath = Path.of(".");
124+
List<String> actual = new JavacTask(tb)
125+
.options("-XDrawDiagnostics", "-XDdev")
126+
.sources(code)
127+
.outdir(curPath)
128+
.run(Expect.FAIL)
129+
.getOutputLines(OutputKind.DIRECT);
130+
131+
List<String> expected = List.of(
132+
"Test.java:1:19: compiler.err.cant.resolve: kindname.variable, CONST, , ",
133+
"1 error"
134+
);
135+
136+
if (!Objects.equals(actual, expected)) {
137+
error("Expected: " + expected + ", but got: " + actual);
138+
}
139+
}
140+
141+
@Test //JDK-8361445
142+
public void testSuppressWarningsErroneousAttribute2() throws Exception {
143+
String code = """
144+
@SuppressWarnings(0)
145+
public class Test {
146+
}
147+
""";
148+
Path curPath = Path.of(".");
149+
List<String> actual = new JavacTask(tb)
150+
.options("-XDrawDiagnostics", "-XDdev")
151+
.sources(code)
152+
.outdir(curPath)
153+
.run(Expect.FAIL)
154+
.getOutputLines(OutputKind.DIRECT);
155+
156+
List<String> expected = List.of(
157+
"Test.java:1:19: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: int, java.lang.String)",
158+
"1 error"
159+
);
160+
161+
if (!Objects.equals(actual, expected)) {
162+
error("Expected: " + expected + ", but got: " + actual);
163+
}
164+
}
165+
166+
@Test //JDK-8361445
167+
public void testSuppressWarningsErroneousAttribute3() throws Exception {
168+
String[] attributeValues = {
169+
"Test.BOOLEAN",
170+
"Test.BYTE",
171+
"Test.SHORT",
172+
"Test.INT",
173+
"Test.LONG",
174+
"Test.FLOAT",
175+
"Test.DOUBLE",
176+
"Test.CHAR",
177+
"Test.class",
178+
"@Deprecated",
179+
"E.A",
180+
};
181+
Set<String> variants = new HashSet<>();
182+
183+
for (String attributeValue : attributeValues) {
184+
variants.add(attributeValue);
185+
variants.add("{" + attributeValue + "}");
186+
}
187+
188+
for (String attributeValue1 : attributeValues) {
189+
for (String attributeValue2 : attributeValues) {
190+
variants.add("{" + attributeValue1 + ", " + attributeValue2 + "}");
191+
}
192+
}
193+
194+
String code = """
195+
@SuppressWarnings($ATTRIBUTE_VALUE)
196+
public class Test {
197+
public static final boolean BOOLEAN = false;
198+
public static final byte BYTE = 0;
199+
public static final short SHORT = 0;
200+
public static final int INT = 0;
201+
public static final long LONG = 0l;
202+
public static final float FLOAT = 0.0;
203+
public static final double DOUBLE = 0.0;
204+
public static final char CHAR = '\0';
205+
}
206+
enum E {
207+
A
208+
}
209+
""";
210+
211+
for (String variant : variants) {
212+
System.out.println("current variant: " + variant);
213+
Path curPath = Path.of(".");
214+
List<String> actual = new JavacTask(tb)
215+
.options("-XDrawDiagnostics", "-XDdev")
216+
.sources(code.replace("$ATTRIBUTE_VALUE", variant))
217+
.outdir(curPath)
218+
.run(Expect.FAIL)
219+
.getOutputLines(OutputKind.DIRECT);
220+
221+
if (actual.isEmpty() || !actual.get(actual.size() - 1).contains("error")) {
222+
error("Incorrect actual errors: " + actual + " for variant: " + variant);
223+
}
224+
}
225+
}
226+
113227
}

0 commit comments

Comments
 (0)