Polish ReflectionUtils[Tests]
This commit is contained in:
parent
5ef023afd2
commit
d698446f7c
|
|
@ -46,7 +46,7 @@ import org.springframework.lang.Nullable;
|
||||||
public abstract class ReflectionUtils {
|
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}.
|
* which are not declared on {@code java.lang.Object}.
|
||||||
* @since 3.0.5
|
* @since 3.0.5
|
||||||
*/
|
*/
|
||||||
|
|
@ -354,7 +354,6 @@ public abstract class ReflectionUtils {
|
||||||
* @throws IllegalStateException if introspection fails
|
* @throws IllegalStateException if introspection fails
|
||||||
*/
|
*/
|
||||||
public static void doWithMethods(Class<?> clazz, MethodCallback mc, @Nullable MethodFilter mf) {
|
public static void doWithMethods(Class<?> clazz, MethodCallback mc, @Nullable MethodFilter mf) {
|
||||||
// Keep backing up the inheritance hierarchy.
|
|
||||||
Method[] methods = getDeclaredMethods(clazz, false);
|
Method[] methods = getDeclaredMethods(clazz, false);
|
||||||
for (Method method : methods) {
|
for (Method method : methods) {
|
||||||
if (mf != null && !mf.matches(method)) {
|
if (mf != null && !mf.matches(method)) {
|
||||||
|
|
@ -367,6 +366,7 @@ public abstract class ReflectionUtils {
|
||||||
throw new IllegalStateException("Not allowed to access method '" + method.getName() + "': " + ex);
|
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)) {
|
if (clazz.getSuperclass() != null && (mf != USER_DECLARED_METHODS || clazz.getSuperclass() != Object.class)) {
|
||||||
doWithMethods(clazz.getSuperclass(), mc, mf);
|
doWithMethods(clazz.getSuperclass(), mc, mf);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2020 the original author or authors.
|
* Copyright 2002-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -184,27 +184,20 @@ class ReflectionUtilsTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void doWithProtectedMethods() {
|
void doWithMethodsUsingProtectedFilter() {
|
||||||
ListSavingMethodCallback mc = new ListSavingMethodCallback();
|
ListSavingMethodCallback mc = new ListSavingMethodCallback();
|
||||||
ReflectionUtils.doWithMethods(TestObject.class, mc, method -> Modifier.isProtected(method.getModifiers()));
|
ReflectionUtils.doWithMethods(TestObject.class, mc, method -> Modifier.isProtected(method.getModifiers()));
|
||||||
assertThat(mc.getMethodNames().isEmpty()).isFalse();
|
assertThat(mc.getMethodNames())
|
||||||
assertThat(mc.getMethodNames().contains("clone")).as("Must find protected method on Object").isTrue();
|
.hasSizeGreaterThanOrEqualTo(2)
|
||||||
assertThat(mc.getMethodNames().contains("finalize")).as("Must find protected method on Object").isTrue();
|
.as("Must find protected methods on Object").contains("clone", "finalize")
|
||||||
assertThat(mc.getMethodNames().contains("hashCode")).as("Public, not protected").isFalse();
|
.as("Public, not protected").doesNotContain("hashCode", "absquatulate");
|
||||||
assertThat(mc.getMethodNames().contains("absquatulate")).as("Public, not protected").isFalse();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void duplicatesFound() {
|
void doWithMethodsFindsDuplicatesInClassHierarchy() {
|
||||||
ListSavingMethodCallback mc = new ListSavingMethodCallback();
|
ListSavingMethodCallback mc = new ListSavingMethodCallback();
|
||||||
ReflectionUtils.doWithMethods(TestObjectSubclass.class, mc);
|
ReflectionUtils.doWithMethods(TestObjectSubclass.class, mc);
|
||||||
int absquatulateCount = 0;
|
assertThat(mc.getMethodNames().stream()).filteredOn("absquatulate"::equals).as("Found 2 absquatulates").hasSize(2);
|
||||||
for (String name : mc.getMethodNames()) {
|
|
||||||
if (name.equals("absquatulate")) {
|
|
||||||
++absquatulateCount;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
assertThat(absquatulateCount).as("Found 2 absquatulates").isEqualTo(2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue