From 1f56bd103d53c8c6ba7502c6586c26be01bfdb80 Mon Sep 17 00:00:00 2001 From: pascalschumacher Date: Tue, 23 Aug 2016 19:22:55 +0200 Subject: [PATCH] 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() {