[ https://issues.apache.org/jira/browse/LANG-1261?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15433237#comment-15433237 ]
ASF GitHub Bot commented on LANG-1261: -------------------------------------- GitHub user PascalSchumacher opened a pull request: https://github.com/apache/commons-lang/pull/183 LANG-1261: ArrayUtils#contains ArrayUtils#indexOf should compare obje… …cts with different types You can merge this pull request into a Git repository by running: $ git pull https://github.com/PascalSchumacher/commons-lang LANG_1261 Alternatively you can review and apply these changes as the patch at: https://github.com/apache/commons-lang/pull/183.patch To close this pull request, make a commit to your master/trunk branch with (at least) the following in the commit message: This closes #183 ---- commit 82a54681e2e7ffbb21fbc7de4b252f0749201f12 Author: pascalschumacher <pascalschumac...@gmx.net> Date: 2016-08-23T17:22:55Z LANG-1261: ArrayUtils#contains ArrayUtils#indexOf should compare objects with different types ---- > ArrayUtils.contains returns false > --------------------------------- > > 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. > 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. -- This message was sent by Atlassian JIRA (v6.3.4#6332)