8014814: (str) StringBuffer "null" is not appended
authordholmes
Wed, 22 May 2013 20:21:13 -0400
changeset 17709 fc6f678fdb83
parent 17708 eccbcb1b38be
child 17710 ce8517f5a2fe
8014814: (str) StringBuffer "null" is not appended Reviewed-by: alanb
jdk/src/share/classes/java/lang/StringBuffer.java
jdk/test/java/lang/StringBuffer/ToStringCache.java
--- a/jdk/src/share/classes/java/lang/StringBuffer.java	Wed May 22 16:43:04 2013 -0700
+++ b/jdk/src/share/classes/java/lang/StringBuffer.java	Wed May 22 20:21:13 2013 -0400
@@ -335,10 +335,8 @@
      * @since 1.5
      */
     @Override
-    public StringBuffer append(CharSequence s) {
-        // Note, synchronization achieved via invocations of other StringBuffer methods after
-        // narrowing of s to specific type
-        // Ditto for toStringCache clearing
+    public synchronized StringBuffer append(CharSequence s) {
+        toStringCache = null;
         super.append(s);
         return this;
     }
--- a/jdk/test/java/lang/StringBuffer/ToStringCache.java	Wed May 22 16:43:04 2013 -0700
+++ b/jdk/test/java/lang/StringBuffer/ToStringCache.java	Wed May 22 20:21:13 2013 -0400
@@ -22,7 +22,7 @@
  */
 
 /* @test
- * @bug 8013395
+ * @bug 8013395 8014814
  * @summary Test StringBuffer.toString caching
  */
 
@@ -199,6 +199,28 @@
         b = sb.toString();
         checkUnequal(a, b);
 
+        // Extra checks that append(null) works correctly
+
+        sb.append((String)null);
+        b = sb.toString();
+        checkUnequal(a, b);
+        a = b;
+
+        sb.append((StringBuffer)null);
+        b = sb.toString();
+        checkUnequal(a, b);
+        a = b;
+
+        sb.append((StringBuilder)null);
+        b = sb.toString();
+        checkUnequal(a, b);
+        a = b;
+
+        sb.append((CharSequence)null);
+        b = sb.toString();
+        checkUnequal(a, b);
+        a = b;
+
         // non-mutating methods
 
         // Reset to known value