|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Google 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 8356441 |
| 27 | + * @summary Recursive formatting in RichDiagnosticFormatter |
| 28 | + * @library /tools/lib |
| 29 | + * @modules jdk.compiler/com.sun.tools.javac.main jdk.compiler/com.sun.tools.javac.api |
| 30 | + * @build toolbox.ToolBox toolbox.JavacTask |
| 31 | + * @run main RichFormatterWithTypeAnnotationsReentrantTest |
| 32 | + */ |
| 33 | +import toolbox.JavacTask; |
| 34 | +import toolbox.Task; |
| 35 | +import toolbox.TestRunner; |
| 36 | +import toolbox.ToolBox; |
| 37 | + |
| 38 | +import java.nio.file.Files; |
| 39 | +import java.nio.file.Path; |
| 40 | +import java.nio.file.Paths; |
| 41 | +import java.util.Arrays; |
| 42 | +import java.util.List; |
| 43 | + |
| 44 | +public class RichFormatterWithTypeAnnotationsReentrantTest extends TestRunner { |
| 45 | + ToolBox tb; |
| 46 | + |
| 47 | + public RichFormatterWithTypeAnnotationsReentrantTest() { |
| 48 | + super(System.err); |
| 49 | + tb = new ToolBox(); |
| 50 | + } |
| 51 | + |
| 52 | + public static void main(String[] args) throws Exception { |
| 53 | + new RichFormatterWithTypeAnnotationsReentrantTest() |
| 54 | + .runTests(m -> new Object[] {Paths.get(m.getName())}); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void test(Path base) throws Exception { |
| 59 | + Path libClasses = base.resolve("libclasses"); |
| 60 | + Files.createDirectories(libClasses); |
| 61 | + new JavacTask(tb) |
| 62 | + .outdir(libClasses) |
| 63 | + .sources( |
| 64 | + """ |
| 65 | + package lib; |
| 66 | + enum Bar { |
| 67 | + BAZ |
| 68 | + } |
| 69 | + """, |
| 70 | + """ |
| 71 | + package lib; |
| 72 | + import java.lang.annotation.ElementType; |
| 73 | + import java.lang.annotation.Retention; |
| 74 | + import java.lang.annotation.RetentionPolicy; |
| 75 | + import java.lang.annotation.Target; |
| 76 | +
|
| 77 | + @Retention(RetentionPolicy.RUNTIME) |
| 78 | + @interface Foo { |
| 79 | + Bar value(); |
| 80 | + } |
| 81 | + """, |
| 82 | + """ |
| 83 | + package lib; |
| 84 | + import java.lang.annotation.ElementType; |
| 85 | + import java.lang.annotation.Retention; |
| 86 | + import java.lang.annotation.RetentionPolicy; |
| 87 | + import java.lang.annotation.Target; |
| 88 | +
|
| 89 | + @Retention(RetentionPolicy.RUNTIME) |
| 90 | + @Target({ElementType.TYPE_USE, ElementType.PARAMETER}) |
| 91 | + @Foo(Bar.BAZ) |
| 92 | + @interface A {} |
| 93 | + """, |
| 94 | + """ |
| 95 | + package lib; |
| 96 | + public interface M { |
| 97 | + String f(@A String k, @A String v); |
| 98 | + } |
| 99 | + """) |
| 100 | + .run() |
| 101 | + .writeAll(); |
| 102 | + Files.delete(libClasses.resolve("lib").resolve("Bar.class")); |
| 103 | + String code = |
| 104 | + """ |
| 105 | + import lib.M; |
| 106 | + class T implements M { |
| 107 | + } |
| 108 | + """; |
| 109 | + // verify that the compilation fails wtih an error, and does not crash |
| 110 | + new JavacTask(tb) |
| 111 | + .classpath(libClasses) |
| 112 | + .sources(code) |
| 113 | + .run(Task.Expect.FAIL); |
| 114 | + } |
| 115 | +} |
0 commit comments