Skip to content

Commit 5dd8e33

Browse files
committed
MethodUtils cannot find or invoke vararg methods of interface types
1 parent e2b349d commit 5dd8e33

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

src/changes/changes.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ The <action> type attribute can be add,update,fix,remove.
5252
<action type="fix" dev="ggregory" due-to="Gary Gregory">MethodUtils cannot find or invoke a public method on a public class implemented in its package-private superclass.</action>
5353
<action type="fix" dev="ggregory" due-to="Stanislav Fort, Gary Gregory">org.apache.commons.lang3.concurrent.AtomicSafeInitializer.get() can spin internally if the FailableSupplier given to org.apache.commons.lang3.concurrent.AbstractConcurrentInitializer.AbstractBuilder.setInitializer(FailableSupplier) throws a RuntimeException.</action>
5454
<action issue="LANG-1783" type="fix" dev="ggregory" due-to="Arnout Engelen, Stanislav Fort, Gary Gregory">WordUtils.containsAllWords​() may throw PatternSyntaxException.</action>
55-
<action type="fix" dev="ggregory" due-to="Joe Ferner, Gary Gregory">MethodUtils cannot find or invoke a vararg method without providing vararg types or values #1427.</action>
55+
<action type="fix" dev="ggregory" due-to="Joe Ferner, Gary Gregory">MethodUtils cannot find or invoke vararg methods without providing vararg types or values #1427.</action>
56+
<action type="fix" dev="ggregory" due-to="Joe Ferner, Gary Gregory">MethodUtils cannot find or invoke vararg methods of interface types.</action>
5657
<!-- FIX Javadoc -->
5758
<action type="fix" dev="ggregory" due-to="Gary Gregory">[javadoc] General improvements.</action>
5859
<action type="fix" dev="ggregory" due-to="Gary Gregory">[javadoc] Fix thrown exception documentation for org.apache.commons.lang3.reflect.MethodUtils.getMethodObject(Class&lt;?&gt;, String, Class&lt;?&gt;...).</action>

src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ public static Method getMatchingAccessibleMethod(final Class<?> cls, final Strin
358358
final String requestTypeSuperClassName = requestLastType == null ? null
359359
: requestLastType.getSuperclass() != null ? requestLastType.getSuperclass().getName() : null;
360360
if (requestTypeName != null && requestTypeSuperClassName != null && !varVargTypeName.equals(requestTypeName)
361-
&& !varVargTypeName.equals(requestTypeSuperClassName)) {
361+
&& !varVargTypeName.equals(requestTypeSuperClassName) && !componentType.isAssignableFrom(requestLastType)) {
362362
return null;
363363
}
364364
}

src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,14 @@ private interface PrivateEmptyInterface {
176176
// empty
177177
}
178178

179+
public static class PublicImpl1OfPackagePrivateEmptyInterface implements PackagePrivateEmptyInterface {
180+
// empty
181+
}
182+
183+
public static class PublicImpl2OfPackagePrivateEmptyInterface implements PackagePrivateEmptyInterface {
184+
// empty
185+
}
186+
179187
public static class TestBean {
180188

181189
public static String bar() {
@@ -248,16 +256,24 @@ public static void oneParameterStatic(final String s) {
248256
// empty
249257
}
250258

251-
public static String staticIntStringVarArg(final int intArg, final String... args) {
252-
return "static int, String...";
259+
public static String staticIntIntVarArg(final int intArg, final int... args) {
260+
return "static int, int...";
253261
}
254262

255263
public static String staticIntLongVarArg(final int intArg, final long... args) {
256264
return "static int, long...";
257265
}
258266

259-
public static String staticIntIntVarArg(final int intArg, final int... args) {
260-
return "static int, int...";
267+
public static String staticIntStringVarArg(final int intArg, final String... args) {
268+
return "static int, String...";
269+
}
270+
271+
public static String staticPackagePrivateEmptyInterface(final PackagePrivateEmptyInterface... args) {
272+
return "static PackagePrivateEmptyInterface...";
273+
}
274+
275+
public String packagePrivateEmptyInterface(final PackagePrivateEmptyInterface... args) {
276+
return "PackagePrivateEmptyInterface...";
261277
}
262278

263279
public static String varOverload(final Boolean... args) {
@@ -385,16 +401,16 @@ public String foo(final String... s) {
385401
return "foo(String...)";
386402
}
387403

388-
public String intStringVarArg(final int intArg, final String... args) {
389-
return "int, String...";
404+
public String intIntVarArg(final int intArg, final int... args) {
405+
return "int, int...";
390406
}
391407

392408
public String intLongVarArg(final int intArg, final long... args) {
393409
return "int, long...";
394410
}
395411

396-
public String intIntVarArg(final int intArg, final int... args) {
397-
return "int, int...";
412+
public String intStringVarArg(final int intArg, final String... args) {
413+
return "int, String...";
398414
}
399415

400416
public void oneParameter(final String s) {
@@ -1238,6 +1254,31 @@ void testInvokeStaticMethod1PlusVarArgs() throws Exception {
12381254
assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeMethod(testBean, "staticIntIntVarArg", 1, "s1", 5));
12391255
}
12401256

1257+
@Test
1258+
void testInvokeStaticMethodVarArgsOfInterface() throws Exception {
1259+
// staticPackagePrivateEmptyInterface
1260+
assertEquals("static PackagePrivateEmptyInterface...", MethodUtils.invokeStaticMethod(TestBean.class, "staticPackagePrivateEmptyInterface",
1261+
new PublicImpl1OfPackagePrivateEmptyInterface(), new PublicImpl2OfPackagePrivateEmptyInterface()));
1262+
assertEquals("static PackagePrivateEmptyInterface...",
1263+
MethodUtils.invokeStaticMethod(TestBean.class, "staticPackagePrivateEmptyInterface", new PackagePrivateEmptyInterface() {
1264+
// empty
1265+
}, new PackagePrivateEmptyInterface() {
1266+
// empty
1267+
}));
1268+
}
1269+
1270+
@Test
1271+
void testInvokeMethodVarArgsOfInterface() throws Exception {
1272+
// packagePrivateEmptyInterface
1273+
assertEquals("PackagePrivateEmptyInterface...", MethodUtils.invokeMethod(testBean, "packagePrivateEmptyInterface",
1274+
new PublicImpl1OfPackagePrivateEmptyInterface(), new PublicImpl2OfPackagePrivateEmptyInterface()));
1275+
assertEquals("PackagePrivateEmptyInterface...", MethodUtils.invokeMethod(testBean, "packagePrivateEmptyInterface", new PackagePrivateEmptyInterface() {
1276+
// empty
1277+
}, new PackagePrivateEmptyInterface() {
1278+
// empty
1279+
}));
1280+
}
1281+
12411282
@Test
12421283
void testNullArgument() {
12431284
expectMatchingAccessibleMethodParameterTypes(TestBean.class, "oneParameter", singletonArray(null), singletonArray(String.class));

0 commit comments

Comments
 (0)