You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by pa...@apache.org on 2017/03/19 16:14:50 UTC
[1/2] [lang] LANG-1310: MethodUtils.invokeMethod throws
ArrayStoreException if using varargs arguments and smaller types than the
method defines (closes #256)
Repository: commons-lang
Updated Branches:
refs/heads/master 7ac12154b -> 5d6c17638
LANG-1310: MethodUtils.invokeMethod throws ArrayStoreException if using varargs arguments and smaller types than the method defines (closes #256)
Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/1a20867d
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/1a20867d
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/1a20867d
Branch: refs/heads/master
Commit: 1a20867d01ef8ee5b32b0620125b7c38a10b80c5
Parents: 7ac1215
Author: drajakumar <dr...@paypal.com>
Authored: Mon Mar 13 22:21:12 2017 +0530
Committer: pascalschumacher <pa...@gmx.net>
Committed: Sun Mar 19 17:12:16 2017 +0100
----------------------------------------------------------------------
.../commons/lang3/reflect/MethodUtils.java | 14 +++++++++
.../commons/lang3/reflect/MethodUtilsTest.java | 32 ++++++++++++++++----
2 files changed, 40 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/1a20867d/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java b/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
index 0f94d93..4602378 100644
--- a/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
+++ b/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
@@ -700,6 +700,20 @@ public class MethodUtils {
if (bestMatch != null) {
MemberUtils.setAccessibleWorkaround(bestMatch);
}
+
+ if (bestMatch != null && bestMatch.isVarArgs() && bestMatch.getParameterTypes().length > 0 && parameterTypes.length > 0) {
+ final Class<?>[] methodParameterTypes = bestMatch.getParameterTypes();
+ final Class<?> methodParameterComponentType = methodParameterTypes[methodParameterTypes.length - 1].getComponentType();
+ final String methodParameterComponentTypeName = ClassUtils.primitiveToWrapper(methodParameterComponentType).getName();
+ final String parameterTypeName = parameterTypes[parameterTypes.length - 1].getName();
+ final String parameterTypeSuperClassName = parameterTypes[parameterTypes.length - 1].getSuperclass().getName();
+
+ if (!methodParameterComponentTypeName.equals(parameterTypeName)
+ && !methodParameterComponentTypeName.equals(parameterTypeSuperClassName)) {
+ return null;
+ }
+ }
+
return bestMatch;
}
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/1a20867d/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java b/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
index 351bed1..df57a13 100644
--- a/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
@@ -96,6 +96,10 @@ public class MethodUtilsTest {
return "bar(String...)";
}
+ public static String bar(final long... s) {
+ return "bar(long...)";
+ }
+
public static String bar(final Integer i, final String... s) {
return "bar(int, String...)";
}
@@ -138,7 +142,6 @@ public class MethodUtilsTest {
return "privateStringStuff(Object)";
}
-
public String foo() {
return "foo()";
}
@@ -155,6 +158,10 @@ public class MethodUtilsTest {
return "foo(double)";
}
+ public String foo(final long l) {
+ return "foo(long)";
+ }
+
public String foo(final String s) {
return "foo(String)";
}
@@ -167,6 +174,10 @@ public class MethodUtilsTest {
return "foo(String...)";
}
+ public String foo(final long... l) {
+ return "foo(long...)";
+ }
+
public String foo(final Integer i, final String... s) {
return "foo(int, String...)";
}
@@ -356,7 +367,7 @@ public class MethodUtilsTest {
NumberUtils.INTEGER_ONE));
assertEquals("foo(int)", MethodUtils.invokeMethod(testBean, "foo",
NumberUtils.BYTE_ONE));
- assertEquals("foo(double)", MethodUtils.invokeMethod(testBean, "foo",
+ assertEquals("foo(long)", MethodUtils.invokeMethod(testBean, "foo",
NumberUtils.LONG_ONE));
assertEquals("foo(double)", MethodUtils.invokeMethod(testBean, "foo",
NumberUtils.DOUBLE_ONE));
@@ -366,6 +377,15 @@ public class MethodUtilsTest {
"a", "b", "c"));
assertEquals("foo(int, String...)", MethodUtils.invokeMethod(testBean, "foo",
5, "a", "b", "c"));
+ assertEquals("foo(long...)", MethodUtils.invokeMethod(testBean, "foo",
+ 1L, 2L));
+
+ try {
+ MethodUtils.invokeMethod(testBean, "foo",
+ 1, 2);
+ fail("should throw NoSuchMethodException");
+ } catch (NoSuchMethodException expected) {
+ }
TestBean.verify(new ImmutablePair<String, Object[]>("String...", new String[]{"x", "y"}),
MethodUtils.invokeMethod(testBean, "varOverloadEcho", "x", "y"));
@@ -434,11 +454,11 @@ public class MethodUtilsTest {
assertEquals("bar(int)", MethodUtils.invokeStaticMethod(TestBean.class,
"bar", NumberUtils.BYTE_ONE));
assertEquals("bar(double)", MethodUtils.invokeStaticMethod(
- TestBean.class, "bar", NumberUtils.LONG_ONE));
- assertEquals("bar(double)", MethodUtils.invokeStaticMethod(
TestBean.class, "bar", NumberUtils.DOUBLE_ONE));
assertEquals("bar(String...)", MethodUtils.invokeStaticMethod(
TestBean.class, "bar", "a", "b"));
+ assertEquals("bar(long...)", MethodUtils.invokeStaticMethod(
+ TestBean.class, "bar", 1L, 2L));
assertEquals("bar(int, String...)", MethodUtils.invokeStaticMethod(
TestBean.class, "bar", NumberUtils.INTEGER_ONE, "a", "b"));
@@ -576,9 +596,9 @@ public class MethodUtilsTest {
expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo",
singletonArray(Integer.TYPE), singletonArray(Integer.TYPE));
expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo",
- singletonArray(Long.class), singletonArray(Double.TYPE));
+ singletonArray(Long.class), singletonArray(Long.TYPE));
expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo",
- singletonArray(Long.TYPE), singletonArray(Double.TYPE));
+ singletonArray(Long.TYPE), singletonArray(Long.TYPE));
expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo",
singletonArray(Float.class), singletonArray(Double.TYPE));
expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo",
[2/2] [lang] changes.xml: add entry for LANG-1310
Posted by pa...@apache.org.
changes.xml: add entry for LANG-1310
Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/5d6c1763
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/5d6c1763
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/5d6c1763
Branch: refs/heads/master
Commit: 5d6c176388e417869421adf2eb21ac5c291f0884
Parents: 1a20867
Author: pascalschumacher <pa...@gmx.net>
Authored: Sun Mar 19 17:14:42 2017 +0100
Committer: pascalschumacher <pa...@gmx.net>
Committed: Sun Mar 19 17:14:42 2017 +0100
----------------------------------------------------------------------
src/changes/changes.xml | 1 +
1 file changed, 1 insertion(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/5d6c1763/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 3edd5e6..78c3514 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -80,6 +80,7 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="LANG-1265" type="fix" dev="pschumacher">Build failures when building with Java 9 EA</action>
<action issue="LANG-1314" type="fix" dev="pschumacher" due-to="Allon Murienik">javadoc creation broken with Java 8</action>
<action issue="LANG-1316" type="update" dev="pschumacher">Deprecate classes/methods moved to commons-text</action>
+ <action issue="LANG-1310" type="fix" dev="pschumacher" due-to="Don Jeba">MethodUtils.invokeMethod throws ArrayStoreException if using varargs arguments and smaller types than the method defines</action>
</release>
<release version="3.5" date="2016-10-13" description="New features including Java 9 detection">