JDK-8322218 : Better escaping of single and double quotes in annotation toString() results
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 14,21.0.1,22
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2023-12-15
  • Updated: 2024-02-14
  • Resolved: 2024-02-06
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 23
23 b09Fixed
Related Reports
Relates :  
Description
Since Java 14, the toString() representation of an annotation unnecessarily escapes single quotes (') within String values.

For example, "Tony's Pizza Parlor" is now represented as "Tony\'s Pizza Parlor".

You can confirm the change in behavior with the following.

import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@AnotationToString.Test("Tony's Pizza Parlor")
public class AnotationToString {

	@Retention(RUNTIME)
	@interface Test {
		String value();
	}

	public static void main(String[] args) {
		Test annotation = AnotationToString.class.getAnnotation(Test.class);
		System.out.println(annotation);
	}
}


Tested against Java 13, 14, and 22 EA.

Java 13:

$ java --show-version AnnotationToString.java
java 13.0.2 2020-01-14
Java(TM) SE Runtime Environment (build 13.0.2+8)
Java HotSpot(TM) 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)
@AnotationToString$Test(value="Tony's Pizza Parlor")

Java 14:

$ java --show-version AnnotationToString.java
java 14.0.2 2020-07-14
Java(TM) SE Runtime Environment (build 14.0.2+12-46)
Java HotSpot(TM) 64-Bit Server VM (build 14.0.2+12-46, mixed mode, sharing)
@AnotationToString$Test("Tony\'s Pizza Parlor")

Java 22 EA:

$ java --show-version AnnotationToString.java
openjdk 22-ea 2024-03-19
OpenJDK Runtime Environment (build 22-ea+27-2262)
OpenJDK 64-Bit Server VM (build 22-ea+27-2262, mixed mode, sharing)
@AnotationToString.Test("Tony\'s Pizza Parlor")

There are a few ways to address this; however, one option would be to modify  sun.reflect.annotation.AnnotationInvocationHandler.toSourceString(String) so that it does not quote s.charAt(i) if s.charAt(i) is '\''.

Comments
Changeset: 1797efd6 Author: Joe Darcy <darcy@openjdk.org> Date: 2024-02-06 23:22:46 +0000 URL: https://git.openjdk.org/jdk/commit/1797efd68d4f30cc38a96fc5902999ee504e182f
06-02-2024

A pull request was submitted for review. URL: https://git.openjdk.org/jdk/pull/17664 Date: 2024-02-01 01:32:42 +0000
01-02-2024

Per the JLS secions 3.10.4 Character Literals 3.10.5 String Literals A single quote needs to be escaped as a character literal, but *not* as a string literal. Conversely, a double quote needs to be escaped in a string, but not in as a character. The quoting code could be improved to take into account "character context" or "string context" in determining what to quote.
31-01-2024

I assume this is about the changes to the String representation in JDK 14 to have javac and core reflection be consistent (JDK-8164819). There is a release note for that change but it doesn't mention escaping.
16-12-2023

Please note that the same is also true for characters. A double quote stored in a char annotation element will be unnecessarily escaped. For example, @CharValue('"') has a toString() representation of @CharValue('\"').
16-12-2023