Skip to content

Commit 5431668

Browse files
committed
8348212: Need to add warn() step to JavacTaskImpl after JDK-8344148
Reviewed-by: mcimadamore
1 parent 1d2eb2f commit 5431668

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,12 +401,12 @@ public Iterable<? extends Element> analyze(Iterable<? extends Element> classes)
401401
final ListBuffer<Element> results = new ListBuffer<>();
402402
try {
403403
if (classes == null) {
404-
handleFlowResults(compiler.flow(compiler.attribute(compiler.todo)), results);
404+
handleFlowResults(compiler.warn(compiler.flow(compiler.attribute(compiler.todo))), results);
405405
} else {
406406
Filter f = new Filter() {
407407
@Override
408408
public void process(Env<AttrContext> env) {
409-
handleFlowResults(compiler.flow(compiler.attribute(env)), results);
409+
handleFlowResults(compiler.warn(compiler.flow(compiler.attribute(env))), results);
410410
}
411411
};
412412
f.run(compiler.todo, classes);
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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

Comments
 (0)