Search in sources :

Example 6 with MethodFilter

use of org.springframework.util.ReflectionUtils.MethodFilter in project spring-framework by spring-projects.

the class ReflectionUtilsTests 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 : Test(org.junit.jupiter.api.Test) List(java.util.List) Modifier(java.lang.reflect.Modifier) TestObject(org.springframework.tests.sample.objects.TestObject) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) MethodFilter(org.springframework.util.ReflectionUtils.MethodFilter) Field(java.lang.reflect.Field) Method(java.lang.reflect.Method) ConnectException(java.rmi.ConnectException) RemoteException(java.rmi.RemoteException) ArrayList(java.util.ArrayList) MethodFilter(org.springframework.util.ReflectionUtils.MethodFilter) Test(org.junit.jupiter.api.Test)

Example 7 with MethodFilter

use of org.springframework.util.ReflectionUtils.MethodFilter in project alien4cloud by alien4cloud.

the class ChildContextAspectsManager method postProcessAfterInitialization.

@Override
public Object postProcessAfterInitialization(final Object bean, final String id) throws BeansException {
    if (log.isTraceEnabled()) {
        log.trace("post processing bean with id [ {} ] of type [ {} ]", id, bean.getClass().toString());
    }
    if (AopUtils.isAopProxy(bean)) {
        log.debug("Spring is already managing proxy for bean of class {}.", bean.getClass().toString());
        return bean;
    }
    if (AnnotationUtils.findAnnotation(bean.getClass(), Overridable.class) != null) {
        // the bean is annotated as Overridable candidate
        log.info("The bean with id [ {} ] of type [ {} ] is candidate to be overridden by plugin child contexts", id, bean.getClass().toString());
        registerProxyCandidate(bean, id);
    } else {
        // let's look for annotation in methods
        ReflectionUtils.doWithMethods(bean.getClass(), new MethodCallback() {

            @Override
            public void doWith(Method m) throws IllegalArgumentException, IllegalAccessException {
                log.info("The method [ {} ] of bean [ {} ] is candidate to be overridden by plugin child contexts", m.toString(), id);
                registerProxyCandidate(bean, id);
            }
        }, new MethodFilter() {

            @Override
            public boolean matches(Method m) {
                // we search for public methods annotated as Overridable
                return (m.getModifiers() & Modifier.PUBLIC) == Modifier.PUBLIC && AnnotationUtils.findAnnotation(m, Overridable.class) != null;
            }
        });
    }
    // if the bean is a candidate, then return the proxy
    ProxyRegistry proxyRegistry = overridableCandidates.get(bean);
    if (proxyRegistry != null) {
        return proxyRegistry.proxy;
    } else {
        return bean;
    }
}
Also used : MethodFilter(org.springframework.util.ReflectionUtils.MethodFilter) Method(java.lang.reflect.Method) MethodCallback(org.springframework.util.ReflectionUtils.MethodCallback)

Aggregations

Method (java.lang.reflect.Method)7 MethodFilter (org.springframework.util.ReflectionUtils.MethodFilter)7 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 Arrays (java.util.Arrays)2 HashMap (java.util.HashMap)2 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)2 Nullable (org.springframework.lang.Nullable)2 ReflectionUtils (org.springframework.util.ReflectionUtils)2 HandlerMethod (org.springframework.web.method.HandlerMethod)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 Modifier (java.lang.reflect.Modifier)1 Type (java.lang.reflect.Type)1 ConnectException (java.rmi.ConnectException)1 RemoteException (java.rmi.RemoteException)1 Set (java.util.Set)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1