Example usage for org.springframework.statemachine.processor MethodAnnotationPostProcessor postProcess

List of usage examples for org.springframework.statemachine.processor MethodAnnotationPostProcessor postProcess

Introduction

In this page you can find the example usage for org.springframework.statemachine.processor MethodAnnotationPostProcessor postProcess.

Prototype

Object postProcess(Class<?> beanClass, Object bean, String beanName, Method method, T metaAnnotation,
        Annotation annotation);

Source Link

Document

Post process a bean.

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;
    }//from  w  ww.  j a  v  a2s . c  o m

    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;
}