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:com.xemantic.tadedon.guice.matcher.Annotations.java

/**
 * Finds if the {@code element} is annotated with the {@code annotationType}.
 * If the {@code element} is instance of {@link Class} or {@link Method},
 * the {@link AnnotationUtils#findAnnotation(Class, Class)} or
 * {@link AnnotationUtils#findAnnotation(Method, Class)} method will be used respectively. Thus it can
 * also find annotations from extended classes and implemented interfaces.
 *
 * @param element the element//from w w w  . ja va2  s  .  c  o m
 * @param annotationType the annotation type.
 * @return annotation instance or {@code null} if no annotation is found.
 * @throws IllegalArgumentException if the {@code element} is not instance of {@link Class} or {@link Method}.
 */
public static Annotation findAnnotation(AnnotatedElement element, Class<? extends Annotation> annotationType) {
    final Annotation annotation;
    if (element instanceof Class<?>) {
        annotation = AnnotationUtils.findAnnotation((Class<?>) element, annotationType);
    } else if (element instanceof Method) {
        annotation = AnnotationUtils.findAnnotation((Method) element, annotationType);
    } else {
        annotation = element.getAnnotation(annotationType);
    }
    return annotation;
}

From source file:com.ravens.filters.GlobalDefaultExceptionHandler.java

@ExceptionHandler(value = Exception.class)
public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
    // If the exception is annotated with @ResponseStatus rethrow it and let
    // the framework handle it - like the OrderNotFoundException example
    // at the start of this post.
    // AnnotationUtils is a Spring Framework utility class.
    if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null)
        throw e;// www.  j a v a  2s  .co m

    // Otherwise setup and send the user to a default error-view.
    ModelAndView mav = new ModelAndView();
    mav.addObject("exception", e);
    mav.addObject("url", req.getRequestURL());
    mav.setViewName(DEFAULT_ERROR_VIEW);

    e.printStackTrace();
    return mav;
}

From source file:rashjz.info.com.az.config.AdviceController.java

@ExceptionHandler(value = Exception.class)
public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
    // If the exception is annotated with @ResponseStatus rethrow it and let
    // the framework handle it - like the OrderNotFoundException example
    // at the start of this post.
    // AnnotationUtils is a Spring Framework utility class.
    if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) {
        throw e;/*from ww w  .j  a  va  2  s. c  o m*/
    }

    // Otherwise setup and send the user to a default error-view.
    ModelAndView mav = new ModelAndView();
    mav.addObject("exception", e);
    mav.addObject("url", req.getRequestURL());
    mav.setViewName(DEFAULT_ERROR_VIEW);
    return mav;

}

From source file:com.tyro.oss.pact.spring4.pact.provider.ConsumerFilter.java

public boolean shouldExcludePact(Class<?> clazz) {
    PactDefinition pactDefinition = AnnotationUtils.findAnnotation(clazz, PactDefinition.class);
    boolean pactShouldBeIncluded = isBlank(consumerToInclude)
            || Objects.equals(consumerFrom(pactDefinition), consumerToInclude);
    return !pactShouldBeIncluded;
}

From source file:com.yoho.core.trace.DefaultSpanNamer.java

@Override
public String name(Object object, String defaultValue) {
    SpanName annotation = AnnotationUtils.findAnnotation(object.getClass(), SpanName.class);
    String spanName = annotation != null ? annotation.value() : object.toString();
    // If there is no overridden toString method we'll put a constant value
    if (isDefaultToString(object, spanName)) {
        return defaultValue;
    }//from   w w w  .j  a va  2  s.  com
    return spanName;
}

From source file:com.iflytek.edu.cloud.frame.spring.rest.ServiceMethodHandlerMapping.java

@Override
protected boolean isHandler(Class<?> beanType) {
    return ((AnnotationUtils.findAnnotation(beanType, Controller.class) != null)
            || (AnnotationUtils.findAnnotation(beanType, ServiceMethod.class) != null));
}

From source file:com.jjw.cloudymvc.web.mvc.ElementHandlerMapping.java

@Override
protected RequestCondition<?> getCustomTypeCondition(Class<?> handlerType) {
    CloudElement cloudElementAnnotation = AnnotationUtils.findAnnotation(handlerType, CloudElement.class);

    return createElementCondition(cloudElementAnnotation);
}

From source file:com.xemantic.tadedon.guice.matcher.TypeLiteralAnnotationMatcher.java

/** {@inheritDoc} */
@Override/*from   www  .j a  v  a2  s .  co  m*/
public boolean matches(TypeLiteral<?> element) {
    Annotation annotation = AnnotationUtils.findAnnotation(element.getRawType(), m_annotation.annotationType());
    return ((annotation != null) && m_annotation.equals(annotation));
}

From source file:com.xemantic.tadedon.guice.matcher.TypeLiteralAnnotationTypeMatcher.java

/** {@inheritDoc} */
@Override/*w  ww. j a  v a2s  .com*/
public boolean matches(TypeLiteral<?> element) {
    return (AnnotationUtils.findAnnotation(element.getRawType(), m_annotationType) != null);
}

From source file:com.excilys.ebi.bank.service.impl.AnnotationScannerImpl.java

@Override
@Cacheable(cacheName = IConstants.Cache.ANNOTATION_SCANNER_CACHE, keyGenerator = @KeyGenerator(name = "StringCacheKeyGenerator"))
public <A extends Annotation> A getMethodOrTypeAnnotation(Class<A> annotationType, Method method,
        Class<?> type) {//from   www.j a v  a 2s .  co  m

    A annotation = AnnotationUtils.findAnnotation(method, annotationType);
    if (annotation == null) {
        annotation = AnnotationUtils.findAnnotation(type, annotationType);
    }

    return annotation;
}