--- old/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/jaxp/datatype/XMLGregorianCalendarImpl.java 2020-08-06 05:48:19.590683450 +0000 +++ new/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/jaxp/datatype/XMLGregorianCalendarImpl.java 2020-08-06 05:48:19.287675689 +0000 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2004, 2020, 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 @@ -32,6 +32,7 @@ import java.math.BigDecimal; import java.math.BigInteger; import java.math.RoundingMode; +import java.util.Arrays; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; @@ -190,7 +191,7 @@ * @author Sunitha Reddy * @see javax.xml.datatype.Duration * @since 1.5 - * @LastModified: June 2018 + * @LastModified: Aug 2020 */ public class XMLGregorianCalendarImpl @@ -1720,8 +1721,15 @@ if (timezone != 0) { gc = this.normalizeToTimezone(getTimezone()); } - return gc.getYear() + gc.getMonth() + gc.getDay() + - gc.getHour() + gc.getMinute() + gc.getSecond(); + + BigInteger ey = gc.getEonAndYear(); + int result = 31 + ((ey == null) ? 0 : ey.hashCode()); + int[] elements = {gc.getMonth(), gc.getDay(), gc.getHour(), + gc.getMinute(), gc.getSecond()}; + result = 31 * result + Arrays.hashCode(elements); + BigDecimal fs = gc.getFractionalSecond(); + result = 31 * result + ((fs == null) ? 0 : fs.hashCode()); + return result; } --- old/src/java.xml/share/classes/javax/xml/datatype/XMLGregorianCalendar.java 2020-08-06 05:48:20.371703453 +0000 +++ new/src/java.xml/share/classes/javax/xml/datatype/XMLGregorianCalendar.java 2020-08-06 05:48:20.068695692 +0000 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2020, 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 @@ -28,6 +28,7 @@ import javax.xml.namespace.QName; import java.math.BigDecimal; import java.math.BigInteger; +import java.util.Arrays; import java.util.TimeZone; import java.util.GregorianCalendar; @@ -716,12 +717,15 @@ if (timezone != 0) { gc = this.normalize(); } - return gc.getYear() - + gc.getMonth() - + gc.getDay() - + gc.getHour() - + gc.getMinute() - + gc.getSecond(); + + BigInteger ey = gc.getEonAndYear(); + int result = 31 + ((ey == null) ? 0 : ey.hashCode()); + int[] elements = {gc.getMonth(), gc.getDay(), gc.getHour(), + gc.getMinute(), gc.getSecond()}; + result = 31 * result + Arrays.hashCode(elements); + BigDecimal fs = gc.getFractionalSecond(); + result = 31 * result + ((fs == null) ? 0 : fs.hashCode()); + return result; } /** --- /dev/null 2020-03-03 22:06:13.039758260 +0000 +++ new/test/jaxp/javax/xml/jaxp/unittest/datatype/HashCodeTest.java 2020-08-06 05:48:20.828715157 +0000 @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2020, 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 + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package datatype; + +import javax.xml.datatype.DatatypeFactory; +import javax.xml.datatype.XMLGregorianCalendar; +import org.testng.annotations.Test; +import org.testng.Assert; +import org.testng.annotations.DataProvider; + +/* + * @test + * @bug 8246816 + * @run testng datatype.HashCodeTest + * @summary Test hashCode generation. + */ +public class HashCodeTest { + /* + DataProvider: for testHashCode + Data: datetime1, datetime1, flag indicating if their hashCodes are equal + */ + @DataProvider(name = "testHashCode") + public Object[][] getData() { + + return new Object[][]{ + // the reported case: identical hash codes before the patch + {"2020-04-24T12:53:00+02:00", "2020-06-04T06:58:17.727Z", false}, + {"1002000-01-15T12:00:00-05:00", "2002000-01-15T13:00:00-04:00", false}, + {"2000-01-15T12:00:59.99999-05:00", "2000-01-15T13:00:59-04:00", false}, + {"2000-01-15T12:00:00-05:00", "2000-01-15T13:00:00-04:00", true}, + {"2000-01-15T12:00:59.99999-05:00", "2000-01-15T13:00:59.99999-04:00", true}, + {"999999999-01-15T12:00:00-05:00", "999999999-01-15T13:00:00-04:00", true}, + {"999999999-01-15T12:00:59.99999-05:00", "999999999-01-15T13:00:59.99999-04:00", true}, + }; + } + + @Test(dataProvider = "testHashCode") + public final void testHashCode(String dt1, String dt2, boolean equal) throws Exception { + DatatypeFactory dataTypeFactory = DatatypeFactory.newInstance(); + XMLGregorianCalendar cal1 = dataTypeFactory.newXMLGregorianCalendar(dt1); + XMLGregorianCalendar cal2 = dataTypeFactory.newXMLGregorianCalendar(dt2); + + // identical hash codes before the patch + int hashCode1 = cal1.hashCode(); + int hashCode2 = cal2.hashCode(); + + if (equal) { + Assert.assertTrue(cal1.equals(cal2)); + Assert.assertEquals(hashCode1, hashCode2); + } else { + Assert.assertFalse(cal1.equals(cal2)); + Assert.assertNotEquals(hashCode1, hashCode2); + } + } +}