From 1f56bd103d53c8c6ba7502c6586c26be01bfdb80 Mon Sep 17 00:00:00 2001 From: pascalschumacher Date: Tue, 23 Aug 2016 19:22:55 +0200 Subject: [PATCH 1/2] LANG-1261: ArrayUtils#contains and ArrayUtils#indexOf should compare objects with different types --- .../org/apache/commons/lang3/ArrayUtils.java | 2 +- .../apache/commons/lang3/ArrayUtilsTest.java | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index 1d33fcf13..b2f019eda 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -3184,7 +3184,7 @@ public class ArrayUtils { return i; } } - } else if (array.getClass().getComponentType().isInstance(objectToFind)) { + } else { for (int i = startIndex; i < array.length; i++) { if (objectToFind.equals(array[i])) { return i; diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java index a50ed1ab2..2d137c822 100644 --- a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java @@ -2953,7 +2953,23 @@ public class ArrayUtilsTest { assertTrue(ArrayUtils.contains(array, null)); assertFalse(ArrayUtils.contains(array, "notInArray")); } - + + @Test + public void testContains_LANG_1261() { + class LANG1261ParentObject { + @Override + public boolean equals(Object o) { + return true; + } + } + class LANG1261ChildObject extends LANG1261ParentObject { + } + + Object[] array = new LANG1261ChildObject[] { new LANG1261ChildObject() }; + + assertTrue(ArrayUtils.contains(array, new LANG1261ParentObject())); + } + //----------------------------------------------------------------------- @Test public void testIndexOfLong() { From 556d0fe10ff9d2ffd75185866dc78e259b71d035 Mon Sep 17 00:00:00 2001 From: Benedikt Ritter Date: Sun, 18 Sep 2016 18:01:02 +0200 Subject: [PATCH 2/2] Add LANG-1261 to changes.xml --- src/changes/changes.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index c038ad8b5..5bd9589fd 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -46,6 +46,7 @@ The type attribute can be add,update,fix,remove. + ArrayUtils.contains returns false for instances of subtypes Remove unnecessary synchronization from registry lookup in EqualsBuilder and HashCodeBuilder Extend RandomStringUtils with methods that generate strings between a min and max length Handle "void" in ClassUtils.getClass()