8210142: java.util.Calendar.clone() doesn't respect sharedZone flag
authornaoto
Thu, 06 Sep 2018 10:49:17 -0700
changeset 51660 c3ad012182b1
parent 51659 ca3003390cf0
child 51661 a4c50d83af82
8210142: java.util.Calendar.clone() doesn't respect sharedZone flag Reviewed-by: rriggs
src/java.base/share/classes/java/util/Calendar.java
test/jdk/java/util/Calendar/CalendarTest.java
--- a/src/java.base/share/classes/java/util/Calendar.java	Thu Sep 06 09:30:47 2018 -0700
+++ b/src/java.base/share/classes/java/util/Calendar.java	Thu Sep 06 10:49:17 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2018, 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
@@ -3326,7 +3326,9 @@
                 other.stamp[i] = stamp[i];
                 other.isSet[i] = isSet[i];
             }
-            other.zone = (TimeZone) zone.clone();
+            if (!sharedZone) {
+                other.zone = (TimeZone) zone.clone();
+            }
             return other;
         }
         catch (CloneNotSupportedException e) {
--- a/test/jdk/java/util/Calendar/CalendarTest.java	Thu Sep 06 09:30:47 2018 -0700
+++ b/test/jdk/java/util/Calendar/CalendarTest.java	Thu Sep 06 10:49:17 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2018, 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,9 +23,10 @@
 
 /**
  * @test
- * @bug 4064654 4374886 4984320 4984574 4944795
+ * @bug 4064654 4374886 4984320 4984574 4944795 8210142
  * @summary test for Calendar
  * @library /java/text/testlib
+ * @modules java.base/java.util:+open
  * @run main CalendarTest
  * @key randomness
  */
@@ -37,6 +38,7 @@
 import java.io.ObjectInputStream;
 import java.io.ObjectOutput;
 import java.io.ObjectOutputStream;
+import java.lang.reflect.Field;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.GregorianCalendar;
@@ -1176,6 +1178,29 @@
             TimeZone.setDefault(savedTimeZone);
         }
     }
+
+    public void TestClonedSharedZones() throws NoSuchFieldException, IllegalAccessException {
+        Field zone = Calendar.class.getDeclaredField("zone");
+        zone.setAccessible(true);
+        Field sharedZone = Calendar.class.getDeclaredField("sharedZone");
+        sharedZone.setAccessible(true);
+
+        // create a new calendar with any date, and clone it.
+        Calendar c1 = new GregorianCalendar();
+        Calendar c2 = (Calendar) c1.clone();
+
+        // c1 should have a shared zone
+        if (!sharedZone.getBoolean(c1)) {
+            errln("Failed : c1.sharedZone == false");
+        } else {
+            // c2 should have a shared zone too
+            if (!sharedZone.getBoolean(c2)) {
+                errln("Failed : c2.sharedZone == false");
+            } else if (zone.get(c1) != zone.get(c2)) {
+                errln("Failed : c1.zone != c2.zone");
+            }
+        }
+    }
 }
 
 //eof