8322218: Better escaping of single and double quotes in annotation toString() results

Reviewed-by: mchung
This commit is contained in:
Joe Darcy 2024-02-06 23:22:46 +00:00
parent 0f5f3c9b97
commit 1797efd68d
2 changed files with 18 additions and 12 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -271,7 +271,7 @@ class AnnotationInvocationHandler implements InvocationHandler, Serializable {
private static String toSourceString(char c) { private static String toSourceString(char c) {
StringBuilder sb = new StringBuilder(4); StringBuilder sb = new StringBuilder(4);
sb.append('\''); sb.append('\'');
sb.append(quote(c)); sb.append(quote(c, true));
return sb.append('\'') .toString(); return sb.append('\'') .toString();
} }
@ -279,15 +279,21 @@ class AnnotationInvocationHandler implements InvocationHandler, Serializable {
* Escapes a character if it has an escape sequence or is * Escapes a character if it has an escape sequence or is
* non-printable ASCII. Leaves non-ASCII characters alone. * non-printable ASCII. Leaves non-ASCII characters alone.
*/ */
private static String quote(char ch) { private static String quote(char ch, boolean charContext) {
/*
* In a char context, single quote (') must be escaped and
* double quote (") need not be escaped. In a non-char
* context, in other words a string context, the reverse is
* true.
*/
switch (ch) { switch (ch) {
case '\b': return "\\b"; case '\b': return "\\b";
case '\f': return "\\f"; case '\f': return "\\f";
case '\n': return "\\n"; case '\n': return "\\n";
case '\r': return "\\r"; case '\r': return "\\r";
case '\t': return "\\t"; case '\t': return "\\t";
case '\'': return "\\'"; case '\'': return (charContext ? "\\'" : "'");
case '\"': return "\\\""; case '\"': return (charContext ? "\"" : "\\\"");
case '\\': return "\\\\"; case '\\': return "\\\\";
default: default:
return (isPrintableAscii(ch)) return (isPrintableAscii(ch))
@ -323,7 +329,7 @@ class AnnotationInvocationHandler implements InvocationHandler, Serializable {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append('"'); sb.append('"');
for (int i = 0; i < s.length(); i++) { for (int i = 0; i < s.length(); i++) {
sb.append(quote(s.charAt(i))); sb.append(quote(s.charAt(i), false));
} }
sb.append('"'); sb.append('"');
return sb.toString(); return sb.toString();

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,7 +23,7 @@
/* /*
* @test * @test
* @bug 8162817 8168921 * @bug 8162817 8168921 8322218
* @summary Test of toString on normal annotations * @summary Test of toString on normal annotations
*/ */
@ -192,8 +192,8 @@ public class AnnotationToStringTest {
public short[] f4; public short[] f4;
@ExpectedString( @ExpectedString(
"@CharArray({'a', 'b', 'c', '\\''})") "@CharArray({'a', 'b', 'c', '\\'', '\"'})")
@CharArray({'a', 'b', 'c', '\''}) @CharArray({'a', 'b', 'c', '\'', '"'})
public char[] f5; public char[] f5;
@ExpectedString( @ExpectedString(
@ -209,8 +209,8 @@ public class AnnotationToStringTest {
public long[] f7; public long[] f7;
@ExpectedString( @ExpectedString(
"@StringArray({\"A\", \"B\", \"C\", \"\\\"Quote\\\"\"})") "@StringArray({\"A\", \"B\", \"C\", \"\\\"Quote\\\"\", \"'\", \"\\\"\"})")
@StringArray({"A", "B", "C", "\"Quote\""}) @StringArray({"A", "B", "C", "\"Quote\"", "'", "\""})
public String[] f8; public String[] f8;
@ExpectedString( @ExpectedString(