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

FXMLLoader violates equals and hashcode contract

XMLWordPrintable

    • generic
    • generic

      FULL PRODUCT VERSION :
      java version "1.8.0_121"
      Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 10.0.14393]
      (any system really, it's in the jdk's source)

      A DESCRIPTION OF THE PROBLEM :
      The FXMLLoader class overrides the Object#equals method to set it so that it is dependent not on the instance of the FXMLLoader but on the field FXMLLoader#location.

      This isn't reflected by the FXMLLoader#hashcode method, which is not implemented and is therefore defaulting to Object#hashcode, which means that it is instance-bound only.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create two instances of FXMLLoader.

      Assert their equalities as per equals() and hashCode() are the same value.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      That the assert succeeds.
      ACTUAL -
      The assert fails.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.lang.AssertionError ...

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      FXMLLoader loader1 = new FXMLLoader();
      FXMLLoader loader2 = new FXMLLoader();

      boolean equalsEquals = loader1.equals(loader2);
      boolean equalsHashcode = loader1.hashCode() == loader2.hashCode();

      assert equalsEquals == equalsHashcode;
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Just never use FXMLLoader#equals.
      Also do not use maps to store them or check their presence.

      Would be nice to have a warning of sorts in the class' JavaDoc.

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: