Benedikt Ritter updated LANG-1261:
    Summary: ArrayUtils.contains returns false for instances of subtypes  (was: 
ArrayUtils.contains returns false)

> ArrayUtils.contains returns false for instances of subtypes
> -----------------------------------------------------------
>                 Key: LANG-1261
>                 URL: https://issues.apache.org/jira/browse/LANG-1261
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.*
>    Affects Versions: 3.4
>         Environment: Android
>            Reporter: Homer Jay
>              Labels: array, equals
> ArrayUtils.contains(Object[] array, Object objectToFind) wrongly returns 
> false.
> =========================================================
> -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.
> =====================================================
> -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.
> ================================================
> 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.

This message was sent by Atlassian JIRA

Reply via email to