Skip to content

Commit 0e0bd64

Browse files
committed
8356441: IllegalStateException in RichDiagnosticFormatter after JDK-8355065
Reviewed-by: liach, mcimadamore
1 parent 9ebb5d4 commit 0e0bd64

File tree

2 files changed

+130
-20
lines changed

2 files changed

+130
-20
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/util/RichDiagnosticFormatter.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.LinkedHashMap;
3333
import java.util.Locale;
3434
import java.util.Map;
35+
import java.util.function.Supplier;
3536

3637
import com.sun.tools.javac.code.Printer;
3738
import com.sun.tools.javac.code.Symbol;
@@ -102,17 +103,17 @@ public Map<Type, JCDiagnostic> get(WhereClauseKind kind) {
102103
/* map for keeping track of a where clause associated to a given type */
103104
WhereClauses whereClauses;
104105

105-
private void enter() {
106-
if (nameSimplifier != null || whereClauses != null) {
107-
throw new IllegalStateException();
106+
private String enter(Supplier<String> r) {
107+
ClassNameSimplifier nameSimplifier = this.nameSimplifier;
108+
WhereClauses whereClauses = this.whereClauses;
109+
try {
110+
this.nameSimplifier = new ClassNameSimplifier();
111+
this.whereClauses = new WhereClauses();
112+
return r.get();
113+
} finally {
114+
this.nameSimplifier = nameSimplifier;
115+
this.whereClauses = whereClauses;
108116
}
109-
nameSimplifier = new ClassNameSimplifier();
110-
whereClauses = new WhereClauses();
111-
}
112-
113-
private void exit() {
114-
nameSimplifier = null;
115-
whereClauses = null;
116117
}
117118

118119
/** Get the DiagnosticFormatter instance for this context. */
@@ -136,8 +137,7 @@ protected RichDiagnosticFormatter(Context context) {
136137

137138
@Override
138139
public String format(JCDiagnostic diag, Locale l) {
139-
enter();
140-
try {
140+
return enter(() -> {
141141
StringBuilder sb = new StringBuilder();
142142
preprocessDiagnostic(diag);
143143
sb.append(formatter.format(diag, l));
@@ -153,20 +153,15 @@ public String format(JCDiagnostic diag, Locale l) {
153153
}
154154
}
155155
return sb.toString();
156-
} finally {
157-
exit();
158-
}
156+
});
159157
}
160158

161159
@Override
162160
public String formatMessage(JCDiagnostic diag, Locale l) {
163-
enter();
164-
try {
161+
return enter(() -> {
165162
preprocessDiagnostic(diag);
166163
return super.formatMessage(diag, l);
167-
} finally {
168-
exit();
169-
}
164+
});
170165
}
171166

172167
/**
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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

Comments
 (0)