Search in sources :

Example 1 with MethodFilter

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");
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Setter(lombok.Setter) BeanInstantiator(cn.taketoday.beans.support.BeanInstantiator) Getter(lombok.Getter) PropertyAccessor(cn.taketoday.core.reflect.PropertyAccessor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) MethodFilter(cn.taketoday.util.ReflectionUtils.MethodFilter) ArrayList(java.util.ArrayList) SetterMethod(cn.taketoday.core.reflect.SetterMethod) GetterMethod(cn.taketoday.core.reflect.GetterMethod) ReflectionException(cn.taketoday.core.reflect.ReflectionException) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TestObject(cn.taketoday.context.objects.TestObject) Method(java.lang.reflect.Method) ConnectException(java.rmi.ConnectException) Collection(java.util.Collection) Field(java.lang.reflect.Field) InvocationTargetException(java.lang.reflect.InvocationTargetException) RemoteException(java.rmi.RemoteException) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) List(java.util.List) Modifier(java.lang.reflect.Modifier) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) MethodFilter(cn.taketoday.util.ReflectionUtils.MethodFilter) Test(org.junit.jupiter.api.Test)

Example 2 with MethodFilter

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();
}
Also used : MethodFilter(cn.taketoday.util.ReflectionUtils.MethodFilter) SetterMethod(cn.taketoday.core.reflect.SetterMethod) GetterMethod(cn.taketoday.core.reflect.GetterMethod) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 3 with MethodFilter

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;
}
Also used : Arrays(java.util.Arrays) Nullable(cn.taketoday.lang.Nullable) Type(java.lang.reflect.Type) ConcurrentReferenceHashMap(cn.taketoday.util.ConcurrentReferenceHashMap) ReflectionUtils(cn.taketoday.util.ReflectionUtils) ClassUtils(cn.taketoday.util.ClassUtils) MethodFilter(cn.taketoday.util.ReflectionUtils.MethodFilter) Method(java.lang.reflect.Method) ArrayList(java.util.ArrayList) MethodFilter(cn.taketoday.util.ReflectionUtils.MethodFilter) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method)

Example 4 with MethodFilter

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;
}
Also used : Arrays(java.util.Arrays) Nullable(cn.taketoday.lang.Nullable) Type(java.lang.reflect.Type) ConcurrentReferenceHashMap(cn.taketoday.util.ConcurrentReferenceHashMap) ReflectionUtils(cn.taketoday.util.ReflectionUtils) ClassUtils(cn.taketoday.util.ClassUtils) MethodFilter(cn.taketoday.util.ReflectionUtils.MethodFilter) Method(java.lang.reflect.Method) ArrayList(java.util.ArrayList) MethodFilter(cn.taketoday.util.ReflectionUtils.MethodFilter) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method)

Example 5 with MethodFilter

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");
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Setter(lombok.Setter) BeanInstantiator(cn.taketoday.beans.support.BeanInstantiator) Getter(lombok.Getter) PropertyAccessor(cn.taketoday.core.reflect.PropertyAccessor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) MethodFilter(cn.taketoday.util.ReflectionUtils.MethodFilter) ArrayList(java.util.ArrayList) SetterMethod(cn.taketoday.core.reflect.SetterMethod) GetterMethod(cn.taketoday.core.reflect.GetterMethod) ReflectionException(cn.taketoday.core.reflect.ReflectionException) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TestObject(cn.taketoday.context.objects.TestObject) Method(java.lang.reflect.Method) ConnectException(java.rmi.ConnectException) Collection(java.util.Collection) Field(java.lang.reflect.Field) InvocationTargetException(java.lang.reflect.InvocationTargetException) RemoteException(java.rmi.RemoteException) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) List(java.util.List) Modifier(java.lang.reflect.Modifier) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) MethodFilter(cn.taketoday.util.ReflectionUtils.MethodFilter) Test(org.junit.jupiter.api.Test)

Aggregations

MethodFilter (cn.taketoday.util.ReflectionUtils.MethodFilter)6 Method (java.lang.reflect.Method)6 GetterMethod (cn.taketoday.core.reflect.GetterMethod)4 SetterMethod (cn.taketoday.core.reflect.SetterMethod)4 ArrayList (java.util.ArrayList)4 Test (org.junit.jupiter.api.Test)4 BeanInstantiator (cn.taketoday.beans.support.BeanInstantiator)2 TestObject (cn.taketoday.context.objects.TestObject)2 PropertyAccessor (cn.taketoday.core.reflect.PropertyAccessor)2 ReflectionException (cn.taketoday.core.reflect.ReflectionException)2 Nullable (cn.taketoday.lang.Nullable)2 ClassUtils (cn.taketoday.util.ClassUtils)2 ConcurrentReferenceHashMap (cn.taketoday.util.ConcurrentReferenceHashMap)2 ReflectionUtils (cn.taketoday.util.ReflectionUtils)2 Field (java.lang.reflect.Field)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Modifier (java.lang.reflect.Modifier)2 Type (java.lang.reflect.Type)2 ConnectException (java.rmi.ConnectException)2 RemoteException (java.rmi.RemoteException)2