|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Oracle and/or its affiliates. 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 8348212 |
| 27 | + * @summary Ensure the warn() phase executes when the compiler is invoked via the API |
| 28 | + * @modules jdk.compiler/com.sun.tools.javac.api |
| 29 | + */ |
| 30 | + |
| 31 | +import com.sun.tools.javac.api.JavacTaskImpl; |
| 32 | + |
| 33 | +import java.io.File; |
| 34 | +import java.io.FileOutputStream; |
| 35 | +import java.io.PrintStream; |
| 36 | +import java.io.PrintWriter; |
| 37 | +import java.io.StringWriter; |
| 38 | +import java.util.List; |
| 39 | + |
| 40 | +import javax.tools.JavaCompiler; |
| 41 | +import javax.tools.JavaFileObject; |
| 42 | +import javax.tools.StandardJavaFileManager; |
| 43 | +import javax.tools.ToolProvider; |
| 44 | + |
| 45 | +public class TestJavacTaskWithWarning { |
| 46 | + |
| 47 | + static final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
| 48 | + static final StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null); |
| 49 | + |
| 50 | + public static void warningTest() throws Exception { |
| 51 | + |
| 52 | + // Create a source file that will generate a warning |
| 53 | + String srcdir = System.getProperty("test.src"); |
| 54 | + File file = new File(srcdir, "GeneratesWarning.java"); |
| 55 | + try (PrintStream out = new PrintStream(new FileOutputStream(file))) { |
| 56 | + out.print( |
| 57 | + """ |
| 58 | + public class GeneratesWarning { |
| 59 | + public GeneratesWarning() { |
| 60 | + hashCode(); // generates a "this-escape" warning |
| 61 | + } |
| 62 | + } |
| 63 | + """); |
| 64 | + } |
| 65 | + |
| 66 | + // Compile it using API |
| 67 | + Iterable<? extends JavaFileObject> files = fm.getJavaFileObjectsFromFiles(List.of(file)); |
| 68 | + StringWriter buf = new StringWriter(); |
| 69 | + List<String> options = List.of( |
| 70 | + "-Xlint:this-escape", |
| 71 | + "-XDrawDiagnostics" |
| 72 | + ); |
| 73 | + JavacTaskImpl task = (JavacTaskImpl)compiler.getTask(new PrintWriter(buf), fm, null, options, null, files); |
| 74 | + task.analyze(); |
| 75 | + |
| 76 | + // Verify warning was generated |
| 77 | + if (!buf.toString().contains("compiler.warn.possible.this.escape")) |
| 78 | + throw new AssertionError("warning not found in:\n" + buf); |
| 79 | + } |
| 80 | + |
| 81 | + public static void main(String[] args) throws Exception { |
| 82 | + try { |
| 83 | + warningTest(); |
| 84 | + } finally { |
| 85 | + fm.close(); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments