Merge branch '5.3.x'

This commit is contained in:
Sam Brannen
2022-01-24 20:33:51 +01:00
2 changed files with 42 additions and 18 deletions

View File

@@ -46,12 +46,12 @@ import org.springframework.lang.Nullable;
public abstract class ReflectionUtils {
/**
* Pre-built MethodFilter that matches all non-bridge non-synthetic methods
* Pre-built {@link MethodFilter} that matches all non-bridge non-synthetic methods
* which are not declared on {@code java.lang.Object}.
* @since 3.0.5
*/
public static final MethodFilter USER_DECLARED_METHODS =
(method -> !method.isBridge() && !method.isSynthetic());
(method -> !method.isBridge() && !method.isSynthetic() && (method.getDeclaringClass() != Object.class));
/**
* Pre-built FieldFilter that matches all non-static, non-final fields.
@@ -353,7 +353,10 @@ public abstract class ReflectionUtils {
* @throws IllegalStateException if introspection fails
*/
public static void doWithMethods(Class<?> clazz, MethodCallback mc, @Nullable MethodFilter mf) {
// Keep backing up the inheritance hierarchy.
if (mf == USER_DECLARED_METHODS && clazz == Object.class) {
// nothing to introspect
return;
}
Method[] methods = getDeclaredMethods(clazz, false);
for (Method method : methods) {
if (mf != null && !mf.matches(method)) {
@@ -366,6 +369,7 @@ public abstract class ReflectionUtils {
throw new IllegalStateException("Not allowed to access method '" + method.getName() + "': " + ex);
}
}
// Keep backing up the inheritance hierarchy.
if (clazz.getSuperclass() != null && (mf != USER_DECLARED_METHODS || clazz.getSuperclass() != Object.class)) {
doWithMethods(clazz.getSuperclass(), mc, mf);
}