Search in sources :

Example 1 with MethodCallback

use of org.springframework.util.ReflectionUtils.MethodCallback in project netty-socketio by mrniko.

the class SpringAnnotationScanner method postProcessBeforeInitialization.

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    final AtomicBoolean add = new AtomicBoolean();
    ReflectionUtils.doWithMethods(bean.getClass(), new MethodCallback() {

        @Override
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            add.set(true);
        }
    }, new MethodFilter() {

        @Override
        public boolean matches(Method method) {
            for (Class<? extends Annotation> annotationClass : annotations) {
                if (method.isAnnotationPresent(annotationClass)) {
                    return true;
                }
            }
            return false;
        }
    });
    if (add.get()) {
        originalBeanClass = bean.getClass();
    }
    return bean;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MethodFilter(org.springframework.util.ReflectionUtils.MethodFilter) Method(java.lang.reflect.Method) MethodCallback(org.springframework.util.ReflectionUtils.MethodCallback) Annotation(java.lang.annotation.Annotation)

Example 2 with MethodCallback

use of org.springframework.util.ReflectionUtils.MethodCallback in project spring-boot by spring-projects.

the class TestRestTemplateTests method restOperationsAreAvailable.

@Test
public void restOperationsAreAvailable() throws Exception {
    RestTemplate delegate = mock(RestTemplate.class);
    given(delegate.getUriTemplateHandler()).willReturn(new DefaultUriBuilderFactory());
    final TestRestTemplate restTemplate = new TestRestTemplate(delegate);
    ReflectionUtils.doWithMethods(RestOperations.class, new MethodCallback() {

        @Override
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            Method equivalent = ReflectionUtils.findMethod(TestRestTemplate.class, method.getName(), method.getParameterTypes());
            assertThat(equivalent).as("Method %s not found", method).isNotNull();
            assertThat(Modifier.isPublic(equivalent.getModifiers())).as("Method %s should have been public", equivalent).isTrue();
            try {
                equivalent.invoke(restTemplate, mockArguments(method.getParameterTypes()));
            } catch (Exception ex) {
                throw new IllegalStateException(ex);
            }
        }

        private Object[] mockArguments(Class<?>[] parameterTypes) throws Exception {
            Object[] arguments = new Object[parameterTypes.length];
            for (int i = 0; i < parameterTypes.length; i++) {
                arguments[i] = mockArgument(parameterTypes[i]);
            }
            return arguments;
        }

        @SuppressWarnings("rawtypes")
        private Object mockArgument(Class<?> type) throws Exception {
            if (String.class.equals(type)) {
                return "String";
            }
            if (Object[].class.equals(type)) {
                return new Object[0];
            }
            if (URI.class.equals(type)) {
                return new URI("http://localhost");
            }
            if (HttpMethod.class.equals(type)) {
                return HttpMethod.GET;
            }
            if (Class.class.equals(type)) {
                return Object.class;
            }
            if (RequestEntity.class.equals(type)) {
                return new RequestEntity(HttpMethod.GET, new URI("http://localhost"));
            }
            return mock(type);
        }
    }, new ReflectionUtils.MethodFilter() {

        @Override
        public boolean matches(Method method) {
            return Modifier.isPublic(method.getModifiers());
        }
    });
}
Also used : Method(java.lang.reflect.Method) HttpMethod(org.springframework.http.HttpMethod) URI(java.net.URI) IOException(java.io.IOException) RestTemplate(org.springframework.web.client.RestTemplate) RequestEntity(org.springframework.http.RequestEntity) ReflectionUtils(org.springframework.util.ReflectionUtils) DefaultUriBuilderFactory(org.springframework.web.util.DefaultUriBuilderFactory) MethodCallback(org.springframework.util.ReflectionUtils.MethodCallback) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 3 with MethodCallback

use of org.springframework.util.ReflectionUtils.MethodCallback in project spring-boot by spring-projects.

the class ConfigurationBeanFactoryMetaData method findFactoryMethod.

private Method findFactoryMethod(String beanName) {
    if (!this.beans.containsKey(beanName)) {
        return null;
    }
    final AtomicReference<Method> found = new AtomicReference<>(null);
    MetaData meta = this.beans.get(beanName);
    final String factory = meta.getMethod();
    Class<?> type = this.beanFactory.getType(meta.getBean());
    ReflectionUtils.doWithMethods(type, new MethodCallback() {

        @Override
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            if (method.getName().equals(factory)) {
                found.compareAndSet(null, method);
            }
        }
    });
    return found.get();
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) Method(java.lang.reflect.Method) MethodCallback(org.springframework.util.ReflectionUtils.MethodCallback)

Aggregations

Method (java.lang.reflect.Method)3 MethodCallback (org.springframework.util.ReflectionUtils.MethodCallback)3 IOException (java.io.IOException)1 Annotation (java.lang.annotation.Annotation)1 URI (java.net.URI)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.junit.Test)1 HttpMethod (org.springframework.http.HttpMethod)1 RequestEntity (org.springframework.http.RequestEntity)1 ReflectionUtils (org.springframework.util.ReflectionUtils)1 MethodFilter (org.springframework.util.ReflectionUtils.MethodFilter)1 RestTemplate (org.springframework.web.client.RestTemplate)1 DefaultUriBuilderFactory (org.springframework.web.util.DefaultUriBuilderFactory)1