Example usage for org.springframework.util ReflectionUtils doWithMethods

List of usage examples for org.springframework.util ReflectionUtils doWithMethods

Introduction

In this page you can find the example usage for org.springframework.util ReflectionUtils doWithMethods.

Prototype

public static void doWithMethods(Class<?> clazz, MethodCallback mc) 

Source Link

Document

Perform the given callback operation on all matching methods of the given class and superclasses.

Usage

From source file:org.springframework.statemachine.processor.StateMachineAnnotationPostProcessor.java

@Override
public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException {
    Assert.notNull(beanFactory, "BeanFactory must not be null");
    final Class<?> beanClass = getBeanClass(bean);

    if (AnnotationUtils.findAnnotation(beanClass, WithStateMachine.class) == null) {
        // we only post-process beans having WithStateMachine
        // in it or as a meta annotation
        return bean;
    }/*  w  ww.  j  av a2  s .  c om*/

    ReflectionUtils.doWithMethods(beanClass, new ReflectionUtils.MethodCallback() {

        @SuppressWarnings({ "unchecked", "rawtypes" })
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            for (Class<? extends Annotation> ppa : postProcessors.keySet()) {
                Annotation metaAnnotation = AnnotationUtils.findAnnotation(method, ppa);
                if (metaAnnotation == null) {
                    continue;
                }
                for (Annotation a : AnnotationUtils.getAnnotations(method)) {
                    MethodAnnotationPostProcessor postProcessor = metaAnnotation != null
                            ? postProcessors.get(metaAnnotation.annotationType())
                            : null;
                    if (postProcessor != null && shouldCreateHandler(a)) {
                        Object result = postProcessor.postProcess(beanClass, bean, beanName, method,
                                metaAnnotation, a);
                        if (result != null && result instanceof StateMachineHandler) {
                            String endpointBeanName = generateBeanName(beanName, method, a.annotationType());

                            if (result instanceof BeanNameAware) {
                                ((BeanNameAware) result).setBeanName(endpointBeanName);
                            }
                            beanFactory.registerSingleton(endpointBeanName, result);
                            if (result instanceof BeanFactoryAware) {
                                ((BeanFactoryAware) result).setBeanFactory(beanFactory);
                            }
                            if (result instanceof InitializingBean) {
                                try {
                                    ((InitializingBean) result).afterPropertiesSet();
                                } catch (Exception e) {
                                    throw new BeanInitializationException(
                                            "failed to initialize annotated component", e);
                                }
                            }
                            if (result instanceof Lifecycle) {
                                lifecycles.add((Lifecycle) result);
                                if (result instanceof SmartLifecycle
                                        && ((SmartLifecycle) result).isAutoStartup()) {
                                    ((SmartLifecycle) result).start();
                                }
                            }
                            if (result instanceof ApplicationListener) {
                                listeners.add((ApplicationListener) result);
                            }
                        }
                    }
                }
            }
        }
    });
    return bean;
}

From source file:org.springframework.yarn.config.annotation.SpringYarnAnnotationPostProcessor.java

@Override
public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException {
    Assert.notNull(beanFactory, "BeanFactory must not be null");
    final Class<?> beanClass = getBeanClass(bean);

    if (!isStereotype(beanClass)) {
        // we only post-process stereotype components
        return bean;
    }//w ww .java2  s  . c  om

    ReflectionUtils.doWithMethods(beanClass, new ReflectionUtils.MethodCallback() {

        @SuppressWarnings({ "unchecked", "rawtypes" })
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            Annotation[] annotations = AnnotationUtils.getAnnotations(method);

            for (Annotation annotation : annotations) {
                MethodAnnotationPostProcessor postProcessor = postProcessors.get(annotation.annotationType());

                if (postProcessor != null && shouldCreateHandler(annotation)) {
                    Object result = postProcessor.postProcess(bean, beanName, method, annotation);

                    if (result != null && result instanceof ContainerHandler) {
                        String endpointBeanName = generateBeanName(beanName, method,
                                annotation.annotationType());

                        if (result instanceof BeanNameAware) {
                            ((BeanNameAware) result).setBeanName(endpointBeanName);
                        }
                        beanFactory.registerSingleton(endpointBeanName, result);
                        if (result instanceof BeanFactoryAware) {
                            ((BeanFactoryAware) result).setBeanFactory(beanFactory);
                        }
                        if (result instanceof InitializingBean) {
                            try {
                                ((InitializingBean) result).afterPropertiesSet();
                            } catch (Exception e) {
                                throw new BeanInitializationException(
                                        "failed to initialize annotated component", e);
                            }
                        }
                        if (result instanceof Lifecycle) {
                            lifecycles.add((Lifecycle) result);
                            if (result instanceof SmartLifecycle && ((SmartLifecycle) result).isAutoStartup()) {
                                ((SmartLifecycle) result).start();
                            }
                        }
                        if (result instanceof ApplicationListener) {
                            listeners.add((ApplicationListener) result);
                        }
                    }
                }
            }
        }
    });
    return bean;
}