8168921: Inconsistent Annotation.toString()
authorigerasim
Wed, 02 Nov 2016 20:53:56 +0300
changeset 41882 0549e50db4bf
parent 41881 b26ea3cb64c5
child 41883 7409556bf935
8168921: Inconsistent Annotation.toString() Reviewed-by: darcy, redestad
jdk/src/java.base/share/classes/sun/reflect/annotation/AnnotationInvocationHandler.java
jdk/test/java/lang/annotation/AnnotationToStringTest.java
--- a/jdk/src/java.base/share/classes/sun/reflect/annotation/AnnotationInvocationHandler.java	Wed Nov 02 18:45:14 2016 +0300
+++ b/jdk/src/java.base/share/classes/sun/reflect/annotation/AnnotationInvocationHandler.java	Wed Nov 02 20:53:56 2016 +0300
@@ -253,20 +253,20 @@
     }
 
     private static String toSourceString(char c) {
-        StringBuilder sb = new StringBuilder();
-        sb.append("'");
+        StringBuilder sb = new StringBuilder(4);
+        sb.append('\'');
         if (c == '\'')
             sb.append("\\'");
         else
             sb.append(c);
-        sb.append("'");
-        return sb.toString();
+        return sb.append('\'')
+                .toString();
     }
 
     private static String toSourceString(long ell) {
-        return (Math.abs(ell) <= Integer.MAX_VALUE) ?
-            String.valueOf(ell) :
-            (String.valueOf(ell) + "L");
+        String str = String.valueOf(ell);
+        return (ell < Integer.MIN_VALUE || ell > Integer.MAX_VALUE)
+                ? (str + 'L') : str;
     }
 
     /**
@@ -278,10 +278,7 @@
         sb.append('"');
         // Escape embedded quote characters, if present, but don't do
         // anything more heroic.
-        if (s.indexOf('"') != -1) {
-            s = s.replace("\"", "\\\"");
-        }
-        sb.append(s);
+        sb.append(s.replace("\"", "\\\""));
         sb.append('"');
         return sb.toString();
     }
--- a/jdk/test/java/lang/annotation/AnnotationToStringTest.java	Wed Nov 02 18:45:14 2016 +0300
+++ b/jdk/test/java/lang/annotation/AnnotationToStringTest.java	Wed Nov 02 20:53:56 2016 +0300
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 8162817
+ * @bug 8162817 8168921
  * @summary Test of toString on normal annotations
  */
 
@@ -70,6 +70,8 @@
         "d1=1.0/0.0, " +
         "l0=5, " +
         "l1=9223372036854775807L, " +
+        "l2=-9223372036854775808L, " +
+        "l3=-2147483648, " +
         "s0=\"Hello world.\", " +
         "s1=\"a\\\"b\", " +
         "class0=Obj[].class)")
@@ -84,6 +86,8 @@
         d1=2.0/0.0,
         l0=5,
         l1=Long.MAX_VALUE,
+        l2=Long.MIN_VALUE,
+        l3=Integer.MIN_VALUE,
         s0="Hello world.",
         s1="a\"b",
         class0=Obj[].class
@@ -185,8 +189,10 @@
         public int[]       f6;
 
         @ExpectedString(
-       "@LongArray(value={-2147483647, 2147483648L, 9223372036854775807L})")
-        @LongArray(value={-Integer.MAX_VALUE, Integer.MAX_VALUE+1L, Long.MAX_VALUE})
+       "@LongArray(value={-9223372036854775808L, -2147483649L, -2147483648," +
+                " -2147483647, 2147483648L, 9223372036854775807L})")
+        @LongArray(value={Long.MIN_VALUE, Integer.MIN_VALUE-1L, Integer.MIN_VALUE,
+                -Integer.MAX_VALUE, Integer.MAX_VALUE+1L, Long.MAX_VALUE})
         public long[]      f7;
 
         @ExpectedString(
@@ -287,6 +293,8 @@
     double d1();
     long   l0();
     long   l1();
+    long   l2();
+    long   l3();
     String s0();
     String s1();
     Class<?> class0();