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

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

Introduction

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

Prototype

@Nullable
public static <A extends Annotation> A findAnnotation(Class<?> clazz, @Nullable Class<A> annotationType) 

Source Link

Document

Find a single Annotation of annotationType on the supplied Class , traversing its interfaces, annotations, and superclasses if the annotation is not directly present on the given class itself.

Usage

From source file:dstrelec.nats.annotation.NatsListenerAnnotationBeanPostProcessor.java

@Override
public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException {
    if (beanName.equals("myListener")) {
        System.out.println(beanName);
    }/*from   www  .j  av a2  s.c om*/
    if (!this.nonAnnotatedClasses.contains(bean.getClass())) {
        Class<?> targetClass = AopUtils.getTargetClass(bean);
        Collection<NatsListener> classLevelListeners = findListenerAnnotations(targetClass);
        final boolean hasClassLevelListeners = classLevelListeners.size() > 0;
        final List<Method> multiMethods = new ArrayList<>();
        Map<Method, Set<NatsListener>> annotatedMethods = MethodIntrospector.selectMethods(targetClass,
                (MethodIntrospector.MetadataLookup<Set<NatsListener>>) method -> {
                    Set<NatsListener> listenerMethods = findListenerAnnotations(method);
                    return (!listenerMethods.isEmpty() ? listenerMethods : null);
                });
        if (hasClassLevelListeners) {
            Set<Method> methodsWithHandler = MethodIntrospector.selectMethods(targetClass,
                    (ReflectionUtils.MethodFilter) method -> AnnotationUtils.findAnnotation(method,
                            NatsHandler.class) != null);
            multiMethods.addAll(methodsWithHandler);
        }
        if (annotatedMethods.isEmpty()) {
            this.nonAnnotatedClasses.add(bean.getClass());
            if (this.logger.isTraceEnabled()) {
                this.logger.trace("No @NatsListener annotations found on bean type: " + bean.getClass());
            }
        } else {
            // Non-empty set of methods
            for (Map.Entry<Method, Set<NatsListener>> entry : annotatedMethods.entrySet()) {
                Method method = entry.getKey();
                for (NatsListener listener : entry.getValue()) {
                    processNatsListener(listener, method, bean, beanName);
                }
            }
            if (this.logger.isDebugEnabled()) {
                this.logger.debug(annotatedMethods.size() + " @NatsListener methods processed on bean '"
                        + beanName + "': " + annotatedMethods);
            }
        }
        if (hasClassLevelListeners) {
            processMultiMethodListeners(classLevelListeners, multiMethods, bean, beanName);
        }
    }
    return bean;
}

From source file:de.escalon.hypermedia.spring.AffordanceBuilderFactory.java

private static RequestMethod getHttpMethod(Method method) {
    RequestMapping methodRequestMapping = AnnotationUtils.findAnnotation(method, RequestMapping.class);
    RequestMethod requestMethod;/*from ww w. ja  va2s .  c  om*/
    if (methodRequestMapping != null) {
        RequestMethod[] methods = methodRequestMapping.method();
        if (methods.length == 0) {
            requestMethod = RequestMethod.GET;
        } else {
            requestMethod = methods[0];
        }
    } else {
        requestMethod = RequestMethod.GET; // default
    }
    return requestMethod;
}

From source file:dstrelec.nats.annotation.NatsListenerAnnotationBeanPostProcessor.java

private Collection<NatsListener> findListenerAnnotations(Class<?> clazz) {
    Set<NatsListener> listeners = new HashSet<>();
    NatsListener ann = AnnotationUtils.findAnnotation(clazz, NatsListener.class);
    if (ann != null) {
        listeners.add(ann);//from   w  ww  . j  av  a2  s. c om
    }
    NatsListeners anns = AnnotationUtils.findAnnotation(clazz, NatsListeners.class);
    if (anns != null) {
        listeners.addAll(Arrays.asList(anns.value()));
    }
    return listeners;
}

From source file:dstrelec.nats.annotation.NatsListenerAnnotationBeanPostProcessor.java

private Set<NatsListener> findListenerAnnotations(Method method) {
    Set<NatsListener> listeners = new HashSet<>();
    NatsListener ann = AnnotationUtils.findAnnotation(method, NatsListener.class);
    if (ann != null) {
        listeners.add(ann);/*from w  w  w .j a va 2  s  .  co  m*/
    }
    NatsListeners anns = AnnotationUtils.findAnnotation(method, NatsListeners.class);
    if (anns != null) {
        listeners.addAll(Arrays.asList(anns.value()));
    }
    return listeners;
}

From source file:de.tolina.common.validation.AnnotationValidation.java

private List<String> validateAllMethodsOfAnnotationDefinition(@Nonnull final SoftAssertions softly,
        @Nonnull final AnnotationDefinition annotationDefinition, @Nonnull final Annotation annotation) {
    final List<String> validatedMethods = Lists.newArrayList();

    // check all methods defined in annotation definition
    for (final AnnotationDefinition.AnnotationMethodDefinition annotationMethodDefinition : annotationDefinition
            .getAnnotationMethodDefinitions()) {
        final String methodName = annotationMethodDefinition.getMethod();
        final Object[] values = annotationMethodDefinition.getValues();

        Method method = null;/*from   w  w w  . j  a  v a  2  s  .co  m*/
        try {
            method = annotation.annotationType().getMethod(methodName);
        } catch (final NoSuchMethodException e) {
            // noop
        }

        // check that defined method is present in annotation
        softly.assertThat(method).as("Method %s not found.", methodName).isNotNull();

        if (method == null) {
            continue;
        }

        // check that actual method in annotation has defined return types
        final Object methodResult = ReflectionUtils.invokeMethod(method, annotation);
        if (Object[].class.isInstance(methodResult)) {
            // this produces readable descriptions on its own
            // all and only defined values must be returned in defined order
            softly.assertThat((Object[]) methodResult).containsExactlyElementsOf(Arrays.asList(values));
        } else {
            // this produces readable descriptions on its own
            softly.assertThat(methodResult).isEqualTo(values[0]);
        }
        validatedMethods.add(method.getName());
        // check if this annotation's method is an alias
        final AliasFor alias = AnnotationUtils.findAnnotation(method, AliasFor.class);
        if (alias != null) {
            // mark the aliased method as validated
            validatedMethods.add(alias.value());
        }

    }
    return validatedMethods;
}

From source file:org.vaadin.spring.security.GenericVaadinSecurity.java

/**
 * {@inheritDoc}//from  w  ww. jav  a 2  s.c  o m
 */
@Override
public boolean hasAccessToSecuredMethod(Object securedObject, String methodName,
        Class<?>... methodParameterTypes) {

    try {
        final Method method = securedObject.getClass().getMethod(methodName, methodParameterTypes);
        final Secured secured = AnnotationUtils.findAnnotation(method, Secured.class);
        Assert.notNull(secured, "securedObject did not have @Secured annotation");
        return hasAccessToObject(securedObject, secured.value());
    } catch (NoSuchMethodException ex) {
        throw new IllegalArgumentException("Method " + methodName + " does not exist", ex);
    }

}

From source file:ch.rasc.wampspring.method.WampAnnotationMethodMessageHandler.java

private <A extends Annotation> void detectHandlerMethods(String beanName, Class<?> userType,
        final Class<A> annotationType) {

    Set<Method> methods = HandlerMethodSelector.selectMethods(userType, new MethodFilter() {
        @Override/*from ww  w .  j  a  v  a2s.c o  m*/
        public boolean matches(Method method) {
            return AnnotationUtils.findAnnotation(method, annotationType) != null;
        }
    });

    for (Method method : methods) {
        A annotation = AnnotationUtils.findAnnotation(method, annotationType);

        String[] replyTo = (String[]) AnnotationUtils.getValue(annotation, "replyTo");
        Boolean excludeSender = (Boolean) AnnotationUtils.getValue(annotation, "excludeSender");
        Boolean broadcast = (Boolean) AnnotationUtils.getValue(annotation, "broadcast");

        boolean authenticationRequiredClass = AnnotationUtils.findAnnotation(userType,
                WampAuthenticated.class) != null;
        boolean[] authenticationRequiredMethod = (boolean[]) AnnotationUtils.getValue(annotation,
                "authenticated");

        boolean authenticationRequired = false;
        if (authenticationRequiredMethod != null && authenticationRequiredMethod.length == 1) {
            authenticationRequired = authenticationRequiredMethod[0];
        } else if (authenticationRequiredClass || this.authenticationRequiredGlobal) {
            authenticationRequired = true;
        }

        WampHandlerMethod newHandlerMethod = new WampHandlerMethod(beanName, this.applicationContext, method,
                replyTo, broadcast, excludeSender, authenticationRequired);

        String[] destinations = (String[]) AnnotationUtils.getValue(annotation);
        if (destinations.length == 0) {
            // by default use beanName.methodName as destination
            destinations = new String[] { beanName + "." + method.getName() };
        }

        WampMessageMappingInfo mapping = null;

        if (annotationType.equals(WampCallListener.class)) {
            mapping = new WampMessageMappingInfo(WampMessageTypeMessageCondition.CALL,
                    new DestinationPatternsMessageCondition(destinations, this.pathMatcher));
        } else if (annotationType.equals(WampPublishListener.class)) {
            mapping = new WampMessageMappingInfo(WampMessageTypeMessageCondition.PUBLISH,
                    new DestinationPatternsMessageCondition(destinations, this.pathMatcher));
        } else if (annotationType.equals(WampSubscribeListener.class)) {
            mapping = new WampMessageMappingInfo(WampMessageTypeMessageCondition.SUBSCRIBE,
                    new DestinationPatternsMessageCondition(destinations, this.pathMatcher));
        } else if (annotationType.equals(WampUnsubscribeListener.class)) {
            mapping = new WampMessageMappingInfo(WampMessageTypeMessageCondition.UNSUBSCRIBE,
                    new DestinationPatternsMessageCondition(destinations, this.pathMatcher));
        }

        registerHandlerMethod(newHandlerMethod, mapping);

    }
}

From source file:org.agatom.springatom.cmp.wizards.core.AbstractWizardProcessor.java

private Wizard getWizardAnnotation() {
    return AnnotationUtils.findAnnotation(this.getClass(), Wizard.class);
}

From source file:com.taobao.itest.listener.ITestDataSetListener.java

private ITestDataSet findAnnotation(Class<?> testClass, Method testMethod) {
    ITestDataSet annotation = (ITestDataSet) AnnotationUtils.findAnnotation(testMethod, ITestDataSet.class);
    if (annotation == null) {

        annotation = (ITestDataSet) AnnotationUtil.findAnnotation(testClass, ITestDataSet.class);
    }//  w  w  w  .j a va2 s  .  com
    return annotation;
}