Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8322218

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

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P4
    • 23
    • 14, 21.0.1, 22
    • core-libs
    • None

    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 '\''.

      Attachments

        Issue Links

          Activity

            People

              darcy Joe Darcy
              sbrannen Sam Brannen
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: