Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ReflectionUtils#getDeclaredMethods to provide consistent result #34110

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -41,6 +41,7 @@
* @author Costin Leau
* @author Sam Brannen
* @author Chris Beams
* @author Yong-Hyun Kim
* @since 1.2.2
*/
public abstract class ReflectionUtils {
Expand Down Expand Up @@ -463,6 +464,7 @@ private static Method[] getDeclaredMethods(Class<?> clazz, boolean defensive) {
if (result == null) {
try {
Method[] declaredMethods = clazz.getDeclaredMethods();
Arrays.sort(declaredMethods, ReflectionUtils::methodDeclaringClassHierarchySorter);
List<Method> defaultMethods = findDefaultMethodsOnInterfaces(clazz);
if (defaultMethods != null) {
result = new Method[declaredMethods.length + defaultMethods.size()];
Expand All @@ -486,6 +488,18 @@ private static Method[] getDeclaredMethods(Class<?> clazz, boolean defensive) {
return (result.length == 0 || !defensive) ? result : result.clone();
}

private static int methodDeclaringClassHierarchySorter(Method method1, Method method2) {
Class<?> clazz1 = method1.getDeclaringClass();
Class<?> clazz2 = method2.getDeclaringClass();
if (clazz1 == clazz2) {
return 0;
}
if (clazz1.isAssignableFrom(clazz2)) {
return 1;
}
return -1;
}

@Nullable
private static List<Method> findDefaultMethodsOnInterfaces(Class<?> clazz) {
List<Method> result = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,6 +37,7 @@
* @author Juergen Hoeller
* @author Sam Brannen
* @author Arjen Poutsma
* @author Yong-Hyun Kim
*/
class ReflectionUtilsTests {

Expand Down Expand Up @@ -231,6 +232,8 @@ void findMethod() {
assertThat(ReflectionUtils.findMethod(B.class, "bar", String.class)).isNotNull();
assertThat(ReflectionUtils.findMethod(B.class, "foo", Integer.class)).isNotNull();
assertThat(ReflectionUtils.findMethod(B.class, "getClass")).isNotNull();
assertThat(ReflectionUtils.findMethod(B.class, "baz", String[].class).isBridge()).isFalse();
assertThat(ReflectionUtils.findMethod(B.class, "baz", String[].class).isVarArgs()).isTrue();
}

@Test
Expand Down Expand Up @@ -381,6 +384,10 @@ private static class A {
@SuppressWarnings({ "unused", "RedundantThrows" })
private void foo(Integer i) throws RemoteException {
}

protected Object baz(String... args) {
return "A";
}
}

@SuppressWarnings("unused")
Expand All @@ -396,6 +403,11 @@ int add(int... args) {
}
return sum;
}

@Override
protected String baz(String... args) {
return "B";
}
}

}