Skip to content

Commit 144a08e

Browse files
committedFeb 1, 2024
8325078: Better escaping of single and double quotes in javac annotation toString() results
Reviewed-by: jlahoda
1 parent b3ecd55 commit 144a08e

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed
 

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -128,7 +128,7 @@ else if (Double.isInfinite(d))
128128
}
129129

130130
private static String formatChar(char c) {
131-
return '\'' + Convert.quote(c) + '\'';
131+
return '\'' + Convert.quote(c, true) + '\'';
132132
}
133133

134134
private static String formatString(String s) {

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

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -298,7 +298,7 @@ public static byte[] string2utf(String s) {
298298
public static String quote(String s) {
299299
StringBuilder buf = new StringBuilder();
300300
for (int i = 0; i < s.length(); i++) {
301-
buf.append(quote(s.charAt(i)));
301+
buf.append(quote(s.charAt(i), false));
302302
}
303303
return buf.toString();
304304
}
@@ -307,15 +307,21 @@ public static String quote(String s) {
307307
* Escapes a character if it has an escape sequence or is
308308
* non-printable ASCII. Leaves non-ASCII characters alone.
309309
*/
310-
public static String quote(char ch) {
310+
public static String quote(char ch, boolean charContext) {
311+
/*
312+
* In a char context, single quote (') must be escaped and
313+
* double quote (") need not be escaped. In a non-char
314+
* context, in other words a string context, the reverse is
315+
* true.
316+
*/
311317
switch (ch) {
312318
case '\b': return "\\b";
313319
case '\f': return "\\f";
314320
case '\n': return "\\n";
315321
case '\r': return "\\r";
316322
case '\t': return "\\t";
317-
case '\'': return "\\'";
318-
case '\"': return "\\\"";
323+
case '\'': return (charContext ? "\\'" : "'");
324+
case '\"': return (charContext ? "\"" : "\\\"");
319325
case '\\': return "\\\\";
320326
default:
321327
return (isPrintableAscii(ch))

‎test/langtools/tools/javac/platform/createsymbols/CreateSymbolsTestImpl.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -360,7 +360,7 @@ void testStringCharLiterals() throws Exception {
360360
"public class T {\n" +
361361
" public static final java.lang.String STR = \"\\u0000\\u0001\\uffff\";\n" +
362362
" public static final java.lang.String EMPTY = \"\";\n" +
363-
" public static final java.lang.String AMP = \"&amp;&&lt;<&gt;>&apos;\\'\";\n\n" +
363+
" public static final java.lang.String AMP = \"&amp;&&lt;<&gt;>&apos;'\";\n\n" +
364364
" public T();\n" +
365365
"}\n",
366366
"t.T",

‎test/langtools/tools/javac/processing/model/element/AnnotationToStringTest.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 8164819
26+
* @bug 8164819 8325078
2727
* @summary Test of toString on normal annotations
2828
* @library /tools/javac/lib
2929
* @build JavacTestingAbstractProcessor AnnotationToStringTest
@@ -281,8 +281,8 @@ static class ArrayAnnotationHost {
281281
public short[] f4;
282282

283283
@ExpectedString(
284-
"@CharArray({'a', 'b', 'c', '\\''})")
285-
@CharArray({'a', 'b', 'c', '\''})
284+
"@CharArray({'a', 'b', 'c', '\\'', '\"'})")
285+
@CharArray({'a', 'b', 'c', '\'', '"'})
286286
public char[] f5;
287287

288288
@ExpectedString(
@@ -298,8 +298,8 @@ static class ArrayAnnotationHost {
298298
public long[] f7;
299299

300300
@ExpectedString(
301-
"@StringArray({\"A\", \"B\", \"C\", \"\\\"Quote\\\"\"})")
302-
@StringArray({"A", "B", "C", "\"Quote\""})
301+
"@StringArray({\"A\", \"B\", \"C\", \"\\\"Quote\\\"\", \"'\", \"\\\"\"})")
302+
@StringArray({"A", "B", "C", "\"Quote\"", "'", "\""})
303303
public String[] f8;
304304

305305
@ExpectedString(

0 commit comments

Comments
 (0)
Please sign in to comment.