diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Constants.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Constants.java index 3e2fc7caeff..66b7940a5a2 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Constants.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Constants.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -128,7 +128,7 @@ public class Constants { } private static String formatChar(char c) { - return '\'' + Convert.quote(c) + '\''; + return '\'' + Convert.quote(c, true) + '\''; } private static String formatString(String s) { diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Convert.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Convert.java index c0cc7ee1644..16127224dc8 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Convert.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Convert.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -298,7 +298,7 @@ public class Convert { public static String quote(String s) { StringBuilder buf = new StringBuilder(); for (int i = 0; i < s.length(); i++) { - buf.append(quote(s.charAt(i))); + buf.append(quote(s.charAt(i), false)); } return buf.toString(); } @@ -307,15 +307,21 @@ public class Convert { * Escapes a character if it has an escape sequence or is * non-printable ASCII. Leaves non-ASCII characters alone. */ - public static String quote(char ch) { + public 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) { case '\b': return "\\b"; case '\f': return "\\f"; case '\n': return "\\n"; case '\r': return "\\r"; case '\t': return "\\t"; - case '\'': return "\\'"; - case '\"': return "\\\""; + case '\'': return (charContext ? "\\'" : "'"); + case '\"': return (charContext ? "\"" : "\\\""); case '\\': return "\\\\"; default: return (isPrintableAscii(ch)) diff --git a/test/langtools/tools/javac/platform/createsymbols/CreateSymbolsTestImpl.java b/test/langtools/tools/javac/platform/createsymbols/CreateSymbolsTestImpl.java index 5037561df10..be9137226b4 100644 --- a/test/langtools/tools/javac/platform/createsymbols/CreateSymbolsTestImpl.java +++ b/test/langtools/tools/javac/platform/createsymbols/CreateSymbolsTestImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -360,7 +360,7 @@ public class CreateSymbolsTestImpl { "public class T {\n" + " public static final java.lang.String STR = \"\\u0000\\u0001\\uffff\";\n" + " public static final java.lang.String EMPTY = \"\";\n" + - " public static final java.lang.String AMP = \"&&<<>>'\\'\";\n\n" + + " public static final java.lang.String AMP = \"&&<<>>''\";\n\n" + " public T();\n" + "}\n", "t.T", diff --git a/test/langtools/tools/javac/processing/model/element/AnnotationToStringTest.java b/test/langtools/tools/javac/processing/model/element/AnnotationToStringTest.java index 35003d673e0..27c90926114 100644 --- a/test/langtools/tools/javac/processing/model/element/AnnotationToStringTest.java +++ b/test/langtools/tools/javac/processing/model/element/AnnotationToStringTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2019, 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. * * This code is free software; you can redistribute it and/or modify it @@ -23,7 +23,7 @@ /* * @test - * @bug 8164819 + * @bug 8164819 8325078 * @summary Test of toString on normal annotations * @library /tools/javac/lib * @build JavacTestingAbstractProcessor AnnotationToStringTest @@ -281,8 +281,8 @@ public class AnnotationToStringTest extends JavacTestingAbstractProcessor { public short[] f4; @ExpectedString( - "@CharArray({'a', 'b', 'c', '\\''})") - @CharArray({'a', 'b', 'c', '\''}) + "@CharArray({'a', 'b', 'c', '\\'', '\"'})") + @CharArray({'a', 'b', 'c', '\'', '"'}) public char[] f5; @ExpectedString( @@ -298,8 +298,8 @@ public class AnnotationToStringTest extends JavacTestingAbstractProcessor { public long[] f7; @ExpectedString( - "@StringArray({\"A\", \"B\", \"C\", \"\\\"Quote\\\"\"})") - @StringArray({"A", "B", "C", "\"Quote\""}) + "@StringArray({\"A\", \"B\", \"C\", \"\\\"Quote\\\"\", \"'\", \"\\\"\"})") + @StringArray({"A", "B", "C", "\"Quote\"", "'", "\""}) public String[] f8; @ExpectedString(