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

XMLGregorianCalendar.hashCode() produces far too many identical hashes

    XMLWordPrintable

Details

    • b11
    • x86_64
    • windows_10

    Description

      A DESCRIPTION OF THE PROBLEM :
      javax.xml.datatype.XMLGregorianCalendar.hashCode() and com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl.hashCode() simply sum up the fields of calendar (and not even the milliseconds) it produces far too many identical hashes.
      Of course the contract of a hashCode() function is not violated, still collisions occur very frequently.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      DatatypeFactory dataTypeFactory = DatatypeFactory.newInstance();
      XMLGregorianCalendar cal1 = dataTypeFactory.newXMLGregorianCalendar("2020-04-24T12:53:00+02:00");
      XMLGregorianCalendar cal2 = dataTypeFactory.newXMLGregorianCalendar("2020-06-04T06:58:17.727Z");

      // This will produce identical hash codes
      int hashCode1 = cal1.hashCode();
      int hashCode2 = cal2.hashCode();


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Use a proper hash code implementation, in the worst case I think even XMLGregorianCalendar.normalize().toString().hashCode() is acceptable or use org.apache.commons.lang.builder.HashCodeBuilder to hash the individual fields.
      ACTUAL -
      Frequent collisions

      ---------- BEGIN SOURCE ----------
      DatatypeFactory dataTypeFactory = DatatypeFactory.newInstance();
      XMLGregorianCalendar cal1 = dataTypeFactory.newXMLGregorianCalendar("2020-04-24T12:53:00+02:00");
      XMLGregorianCalendar cal2 = dataTypeFactory.newXMLGregorianCalendar("2020-06-04T06:58:17.727Z");

      // This will produce identical hash codes
      int hashCode1 = cal1.hashCode();
      int hashCode2 = cal2.hashCode();

      assertNotEquals(
      "Version should be different because XMLGregorianCalendars are different. cal1: " + cal1 + " cal2: " + cal2,
      hashCode1, hashCode2);
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      XMLGregorianCalendar.normalize().toString().hashCode()

      Attachments

        Activity

          People

            joehw Joe Wang
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: