use of cn.taketoday.util.ReflectionUtils.MethodFilter in project today-framework by TAKETODAY.
the class ReflectionUtilsTest method doWithMethodsUsingUserDeclaredMethodsComposedFilter.
@Test
void doWithMethodsUsingUserDeclaredMethodsComposedFilter() {
ListSavingMethodCallback mc = new ListSavingMethodCallback();
// "q" because both absquatulate() and equals() contain "q"
MethodFilter isSetterMethodOrNameContainsQ = m -> m.getName().startsWith("set") || m.getName().contains("q");
MethodFilter methodFilter = ReflectionUtils.USER_DECLARED_METHODS.and(isSetterMethodOrNameContainsQ);
ReflectionUtils.doWithMethods(TestObject.class, mc, methodFilter);
assertThat(mc.getMethodNames()).containsExactlyInAnyOrder("setName", "setAge", "setSpouse", "absquatulate");
}
use of cn.taketoday.util.ReflectionUtils.MethodFilter in project today-framework by TAKETODAY.
the class ReflectionUtilsTest method testDoWithProtectedMethods.
@Test
public void testDoWithProtectedMethods() {
ListSavingMethodCallback mc = new ListSavingMethodCallback();
ReflectionUtils.doWithMethods(TestObject.class, mc, new MethodFilter() {
@Override
public boolean matches(Method m) {
return Modifier.isProtected(m.getModifiers());
}
});
assertThat(mc.getMethodNames().isEmpty()).isFalse();
assertThat(mc.getMethodNames().contains("clone")).as("Must find protected method on Object").isTrue();
assertThat(mc.getMethodNames().contains("finalize")).as("Must find protected method on Object").isTrue();
assertThat(mc.getMethodNames().contains("hashCode")).as("Public, not protected").isFalse();
assertThat(mc.getMethodNames().contains("absquatulate")).as("Public, not protected").isFalse();
}
use of cn.taketoday.util.ReflectionUtils.MethodFilter in project today-framework by TAKETODAY.
the class BridgeMethodResolver method findBridgedMethod.
/**
* Find the original method for the supplied {@link Method bridge Method}.
* <p>It is safe to call this method passing in a non-bridge {@link Method} instance.
* In such a case, the supplied {@link Method} instance is returned directly to the caller.
* Callers are <strong>not</strong> required to check for bridging before calling this method.
*
* @param bridgeMethod the method to introspect
* @return the original method (either the bridged method or the passed-in method
* if no more specific one could be found)
*/
public static Method findBridgedMethod(Method bridgeMethod) {
if (!bridgeMethod.isBridge()) {
return bridgeMethod;
}
Method bridgedMethod = cache.get(bridgeMethod);
if (bridgedMethod == null) {
// Gather all methods with matching name and parameter size.
ArrayList<Method> candidateMethods = new ArrayList<>();
MethodFilter filter = candidateMethod -> isBridgedCandidateFor(candidateMethod, bridgeMethod);
ReflectionUtils.doWithMethods(bridgeMethod.getDeclaringClass(), candidateMethods::add, filter);
if (!candidateMethods.isEmpty()) {
bridgedMethod = candidateMethods.size() == 1 ? candidateMethods.get(0) : searchCandidates(candidateMethods, bridgeMethod);
}
if (bridgedMethod == null) {
// A bridge method was passed in but we couldn't find the bridged method.
// Let's proceed with the passed-in method and hope for the best...
bridgedMethod = bridgeMethod;
}
cache.put(bridgeMethod, bridgedMethod);
}
return bridgedMethod;
}
use of cn.taketoday.util.ReflectionUtils.MethodFilter in project today-infrastructure by TAKETODAY.
the class BridgeMethodResolver method findBridgedMethod.
/**
* Find the original method for the supplied {@link Method bridge Method}.
* <p>It is safe to call this method passing in a non-bridge {@link Method} instance.
* In such a case, the supplied {@link Method} instance is returned directly to the caller.
* Callers are <strong>not</strong> required to check for bridging before calling this method.
*
* @param bridgeMethod the method to introspect
* @return the original method (either the bridged method or the passed-in method
* if no more specific one could be found)
*/
public static Method findBridgedMethod(Method bridgeMethod) {
if (!bridgeMethod.isBridge()) {
return bridgeMethod;
}
Method bridgedMethod = cache.get(bridgeMethod);
if (bridgedMethod == null) {
// Gather all methods with matching name and parameter size.
ArrayList<Method> candidateMethods = new ArrayList<>();
MethodFilter filter = candidateMethod -> isBridgedCandidateFor(candidateMethod, bridgeMethod);
ReflectionUtils.doWithMethods(bridgeMethod.getDeclaringClass(), candidateMethods::add, filter);
if (!candidateMethods.isEmpty()) {
bridgedMethod = candidateMethods.size() == 1 ? candidateMethods.get(0) : searchCandidates(candidateMethods, bridgeMethod);
}
if (bridgedMethod == null) {
// A bridge method was passed in but we couldn't find the bridged method.
// Let's proceed with the passed-in method and hope for the best...
bridgedMethod = bridgeMethod;
}
cache.put(bridgeMethod, bridgedMethod);
}
return bridgedMethod;
}
use of cn.taketoday.util.ReflectionUtils.MethodFilter in project today-infrastructure by TAKETODAY.
the class ReflectionUtilsTest method doWithMethodsUsingUserDeclaredMethodsComposedFilter.
@Test
void doWithMethodsUsingUserDeclaredMethodsComposedFilter() {
ListSavingMethodCallback mc = new ListSavingMethodCallback();
// "q" because both absquatulate() and equals() contain "q"
MethodFilter isSetterMethodOrNameContainsQ = m -> m.getName().startsWith("set") || m.getName().contains("q");
MethodFilter methodFilter = ReflectionUtils.USER_DECLARED_METHODS.and(isSetterMethodOrNameContainsQ);
ReflectionUtils.doWithMethods(TestObject.class, mc, methodFilter);
assertThat(mc.getMethodNames()).containsExactlyInAnyOrder("setName", "setAge", "setSpouse", "absquatulate");
}
Aggregations