Skip to content

Commit efc8e2e

Browse files
committed
Keep static declarations: a static candidate is not re-resolved and a static interface method is not a stand-in
1 parent a1951aa commit efc8e2e

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,12 @@ private static Method getAccessibleMethodFromInterfaceNest(Class<?> cls, final S
179179
if (!ClassUtils.isPublic(anInterface)) {
180180
continue;
181181
}
182-
// Does the method exist on this interface?
182+
// Does the method exist on this interface? A static one is not inherited.
183183
try {
184-
return anInterface.getDeclaredMethod(methodName, parameterTypes);
184+
final Method declared = anInterface.getDeclaredMethod(methodName, parameterTypes);
185+
if (!MemberUtils.isStatic(declared)) {
186+
return declared;
187+
}
185188
} catch (final NoSuchMethodException ignored) {
186189
/*
187190
* Swallow, if no method is found after the loop then this method returns null.
@@ -370,8 +373,8 @@ public static Method getMatchingAccessibleMethod(final Class<?> cls, final Strin
370373
final Method candidate = getMethodObject(cls, methodName, requestTypes);
371374
if (candidate != null) {
372375
// The exact match may be declared on a non-public class, so prefer the public
373-
// declaration the way the search below does.
374-
final Method accessibleCandidate = getAccessibleMethod(cls, candidate);
376+
// declaration the way the search below does; a static method hides, so it is kept.
377+
final Method accessibleCandidate = MemberUtils.isStatic(candidate) ? null : getAccessibleMethod(cls, candidate);
375378
return MemberUtils.setAccessibleWorkaround(accessibleCandidate != null ? accessibleCandidate : candidate);
376379
}
377380
// search through all methods

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,30 @@ public String foo() {
525525
}
526526
}
527527

528+
public static class StaticParent {
529+
public static String who() {
530+
return "parent";
531+
}
532+
}
533+
534+
static class StaticChild extends StaticParent {
535+
public static String who() {
536+
return "child";
537+
}
538+
}
539+
540+
public interface StaticLabel {
541+
static String label() {
542+
return "interface";
543+
}
544+
}
545+
546+
static class InstanceLabel implements StaticLabel {
547+
public String label() {
548+
return "instance";
549+
}
550+
}
551+
528552
private static class TestMutable implements Mutable<Object> {
529553
@Override
530554
public Object getValue() {
@@ -640,6 +664,20 @@ void testInvokeMethodOnNonPublicClass() throws Exception {
640664
assertNull(MethodUtils.invokeMethod(new TestMutable(), "getValue"));
641665
}
642666

667+
@Test
668+
void testInvokeStaticMethodOnNonPublicSubclass() throws Exception {
669+
// A static method hides rather than overrides, so the subclass's own declaration is invoked.
670+
assertSame(StaticChild.class, MethodUtils.getMatchingAccessibleMethod(StaticChild.class, "who").getDeclaringClass());
671+
assertEquals("child", MethodUtils.invokeStaticMethod(StaticChild.class, "who"));
672+
}
673+
674+
@Test
675+
void testInvokeMethodIgnoresStaticInterfaceMethod() throws Exception {
676+
assertSame(InstanceLabel.class, MethodUtils.getMatchingAccessibleMethod(InstanceLabel.class, "label").getDeclaringClass());
677+
assertEquals("instance", MethodUtils.invokeMethod(new InstanceLabel(), "label"));
678+
assertNull(MethodUtils.getAccessibleMethod(InstanceLabel.class, "label"));
679+
}
680+
643681
@Test
644682
void testGetAccessibleMethodInaccessible() throws Exception {
645683
assertNull(MethodUtils.getAccessibleMethod(TestBean.class.getDeclaredMethod("privateStuff")));

0 commit comments

Comments
 (0)