Skip to content

Commit 083b980

Browse files
committed
8340568: Incorrect escaping of single quotes when pretty-printing character literals
Reviewed-by: mcimadamore
1 parent d8790aa commit 083b980

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/tree/Pretty.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1467,7 +1467,7 @@ public void visitLiteral(JCLiteral tree) {
14671467
break;
14681468
case CHAR:
14691469
print('\'');
1470-
print(Convert.quote(String.valueOf((char)((Number)tree.value).intValue())));
1470+
print(Convert.quote((char)((Number)tree.value).intValue(), true));
14711471
print('\'');
14721472
break;
14731473
case BOOLEAN:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (c) 2024, 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 8340568
27+
* @summary Incorrect escaping of single quotes when pretty-printing character literals
28+
* @modules jdk.compiler/com.sun.tools.javac.api
29+
* jdk.compiler/com.sun.tools.javac.file
30+
* jdk.compiler/com.sun.tools.javac.tree
31+
* jdk.compiler/com.sun.tools.javac.util
32+
*/
33+
34+
import com.sun.tools.javac.file.JavacFileManager;
35+
import com.sun.tools.javac.tree.Pretty;
36+
import com.sun.tools.javac.tree.TreeMaker;
37+
import com.sun.tools.javac.util.Context;
38+
39+
import java.io.IOException;
40+
import java.io.StringWriter;
41+
42+
public class PrettyCharLiteral {
43+
public static void main(String... args) throws Exception {
44+
new PrettyCharLiteral().run();
45+
}
46+
47+
private final TreeMaker make;
48+
49+
private PrettyCharLiteral() {
50+
Context ctx = new Context();
51+
JavacFileManager.preRegister(ctx);
52+
this.make = TreeMaker.instance(ctx);
53+
}
54+
55+
void run() throws Exception {
56+
assertEquals(
57+
prettyPrintLiteral('\''),
58+
"""
59+
'\\''
60+
""".trim());
61+
assertEquals(
62+
prettyPrintLiteral('"'),
63+
"""
64+
'"'
65+
""".trim());
66+
assertEquals(
67+
prettyPrintLiteral("'"),
68+
"""
69+
"'"
70+
""".trim());
71+
assertEquals(
72+
prettyPrintLiteral("\""),
73+
"""
74+
"\\""
75+
""".trim());
76+
}
77+
78+
private void assertEquals(String actual, String expected) {
79+
if (!actual.equals(expected)) {
80+
throw new AssertionError("expected: " + expected + ", actual: " + actual);
81+
}
82+
}
83+
84+
private String prettyPrintLiteral(Object value) throws IOException {
85+
StringWriter sw = new StringWriter();
86+
new Pretty(sw, true).printExpr(make.Literal(value));
87+
return sw.toString();
88+
}
89+
}

0 commit comments

Comments
 (0)