Example usage for org.springframework.core.annotation AnnotationUtils getAnnotations

List of usage examples for org.springframework.core.annotation AnnotationUtils getAnnotations

Introduction

In this page you can find the example usage for org.springframework.core.annotation AnnotationUtils getAnnotations.

Prototype

@Deprecated
@Nullable
public static Annotation[] getAnnotations(Method method) 

Source Link

Document

Get all Annotation Annotations that are present on the supplied Method .

Usage

From source file:org.springframework.integration.config.annotation.MessagingAnnotationPostProcessor.java

public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException {
    Assert.notNull(this.beanFactory, "BeanFactory must not be null");
    final Class<?> beanClass = this.getBeanClass(bean);
    if (!this.isStereotype(beanClass)) {
        // we only post-process stereotype components
        return bean;
    }// ww  w  .  j a  v a  2  s  .  c o  m
    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 && shouldCreateEndpoint(annotation)) {
                    Object result = postProcessor.postProcess(bean, beanName, method, annotation);
                    if (result != null && result instanceof AbstractEndpoint) {
                        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;
}

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 www. jav a  2 s. 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;
}

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. ja v a2  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;
}