|
| 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 | +} |
0 commit comments