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

ArrayUtils.contains returns false for instances of subtypes

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 3.4
    • 3.5
    • lang.*
    • Android

    • Important

    Description

      ArrayUtils.contains(Object[] array, Object objectToFind) wrongly returns false.

      STEPS TO REPRODUCE
      =========================================================
      -Create a superclass "Parent" and override equals and hashcode based on some member id variable.
      -Create a class "Child" extending "Parent". Do not override equals nor hashcode.

      -Let "childrens" be an array of type Child[] containing several instances.
      Create an instance of Parent "p" with the same id as childrens[0], such that childrens[0].equals(p) returns true and p.equals(childrens[0]) returns true as well.

      Because they are equals, ArrayUtils.contains(childrens, p) should return true. However it returns false.

      WHERE THE BUG IS LOCATED
      =====================================================
      -Go to ArrayUtils.class, line 1917. In the "indexOf" method implementation, before going into calling equals for each element of the input array, there is some sort of optimization check to make sure the instance to be found is an instance of the array type:

      } else if (array.getClass().getComponentType().isInstance(objectToFind)) {

      That line is wrong. In our case, the array contains elements of type "Child", whereas the object to be found is of type "Parent". They are equals according to the equals implementation of "Parent", but obviously Children.class.isInstance(p) is false.

      EXPECTED BEHAVIOR
      ================================================
      Since the method signature accepts an array of Object[] and an instance of Object, it should ignore the classes of the arguments. It should be possible to call "ArrayUtils.contains(Child[] children, Parent p)", in fact it should be possible to do this with any combination of classes, not only the ones assignable from the class hierarchy.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              h0m3r_j4y Homer Jay
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: