Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-22

[lang] Enum.equals() is too fragile

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Duplicate
    • 2.0
    • None
    • None
    • None
    • Operating System: other
      Platform: Other

    • 34600

    Description

      The equals() method of the Enum class in Commons Lang 2.0 is too fragile. If
      the "Other" argument is a non-null abject of any other type than Enum, then a
      ClassCastException is thrown.

      The offending line code is:

      if (other == this)

      { return true; }

      else if (other == null)

      { return false; }

      else if (other.getClass() == this.getClass())

      { // shouldn't happen, but... return iName.equals(((Enum) other).iName); }

      else if (((Enum)
      other).getEnumClass().getName().equals(getEnumClass().getName())) {

      If "other" is, say, a String, then the last else-if clause will throw an
      exception when it tries to cast it to Enum.

      This method should be more robust, and tolerate ANY argument, regardless of type.

      Attachments

        Activity

          People

            Unassigned Unassigned
            kennym@kizoom.com Kenny MacLeod
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: