|
1 | 1 | /*
|
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. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
23 | 23 |
|
24 | 24 | /*
|
25 | 25 | * @test
|
26 |
| - * @bug 8270139 |
| 26 | + * @bug 8270139 8361445 |
27 | 27 | * @summary Verify error recovery w.r.t. annotations
|
28 | 28 | * @library /tools/lib
|
29 | 29 | * @modules jdk.compiler/com.sun.tools.javac.api
|
|
33 | 33 | */
|
34 | 34 |
|
35 | 35 | import java.nio.file.Path;
|
| 36 | +import java.util.HashSet; |
36 | 37 | import java.util.List;
|
37 | 38 | import java.util.Objects;
|
| 39 | +import java.util.Set; |
38 | 40 |
|
39 | 41 | import toolbox.JavacTask;
|
40 | 42 | import toolbox.Task.Expect;
|
@@ -110,4 +112,116 @@ public void testRepeatableAnnotationWrongAttribute() throws Exception {
|
110 | 112 | }
|
111 | 113 | }
|
112 | 114 |
|
| 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 | + |
113 | 227 | }
|
0 commit comments