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:org.cloudfoundry.identity.uaa.login.test.ProfileActiveUtils.java

public static boolean isTestEnabledInThisEnvironment(Class<?> testClass) {
    IfProfileActive ifProfileActive = AnnotationUtils.findAnnotation(testClass, IfProfileActive.class);
    UnlessProfileActive unlessProfileActive = AnnotationUtils.findAnnotation(testClass,
            UnlessProfileActive.class);
    return isTestEnabledInThisEnvironment(ProfileValueUtils.retrieveProfileValueSource(testClass),
            ifProfileActive, unlessProfileActive);
}

From source file:com.vladmihalcea.util.ReflectionUtils.java

public static <T extends Annotation> T getAnnotation(ProceedingJoinPoint pjp, Class<T> annotationClass)
        throws NoSuchMethodException {
    MethodSignature signature = (MethodSignature) pjp.getSignature();
    Method method = signature.getMethod();
    T annotation = AnnotationUtils.findAnnotation(method, annotationClass);

    if (annotation != null) {
        return annotation;
    }/*from w  w w .  j  ava  2s.c  o  m*/

    Class[] argClasses = new Class[pjp.getArgs().length];
    for (int i = 0; i < pjp.getArgs().length; i++) {
        argClasses[i] = pjp.getArgs()[i].getClass();
    }
    method = pjp.getTarget().getClass().getMethod(pjp.getSignature().getName(), argClasses);
    return AnnotationUtils.findAnnotation(method, annotationClass);
}

From source file:com.xemantic.tadedon.guice.lifecycle.jsr250.Jsr250Utils.java

public static void invoke(Class<? extends Annotation> jsr250Annotation, Iterable<Object> objects) {
    for (Object object : objects) {
        Method[] methods = object.getClass().getMethods();
        for (Method method : methods) {
            if (AnnotationUtils.findAnnotation(method, jsr250Annotation) != null) {
                try {
                    method.invoke(object);
                } catch (IllegalArgumentException e) {
                    handleException(jsr250Annotation, method, e);
                } catch (IllegalAccessException e) {
                    handleException(jsr250Annotation, method, e);
                } catch (InvocationTargetException e) {
                    handleException(jsr250Annotation, method, e);
                }//from w w w.  j a  v  a 2s.  c o  m
            }
        }
    }
}

From source file:cf.spring.servicebroker.AccessorUtils.java

/**
 * Finds a method annotated with the specified annotation. This method can
 * be defined in the specified class, or one of its parents.
 *
 * @return the matching method, or <tt>null</tt> if any
 *///w  ww . j  a v a  2s  .c om
public static <T extends Annotation> Method findMethodWithAnnotation(Class<?> clazz, Class<T> annotationType) {
    Method annotatedMethod = null;
    for (Method method : clazz.getDeclaredMethods()) {
        T annotation = AnnotationUtils.findAnnotation(method, annotationType);
        if (annotation != null) {
            if (annotatedMethod != null) {
                throw new BeanCreationException("Only ONE method with @" + annotationType.getName()
                        + " is allowed on " + clazz.getName() + ".");
            }
            annotatedMethod = method;
        }
    }

    if ((annotatedMethod != null) || clazz.equals(Object.class)) {
        return annotatedMethod;
    } else {
        return findMethodWithAnnotation(clazz.getSuperclass(), annotationType);
    }
}

From source file:pl.bristleback.server.bristle.conf.resolver.action.ActionResolvingUtils.java

public static String resolveClientActionName(Method actionMethod, boolean validate) {
    ClientAction actionAnnotation = AnnotationUtils.findAnnotation(actionMethod, ClientAction.class);
    if (actionAnnotation == null || StringUtils.isBlank(actionAnnotation.value())) {
        return actionMethod.getName();
    }/*from   ww  w . java2s .c  om*/
    if (validate) {
        validateActionNameFromAnnotationValue(actionAnnotation.value());
    }
    return actionAnnotation.value();
}

From source file:com.xemantic.tadedon.guice.persistence.PersistenceUtils.java

/**
 * Checks {@link Transactional#readOnly()} property for given {@code method}.
 * <p>//from ww  w.  java  2s.c  om
 * Algorithm starts with method annotation checking also overridden methods
 * from supertypes. If no annotation is found on the {@code method}, annotation
 * of declaring class and it's supertypes will be used. Absence of any
 * {@code Transactional} annotation will cause {@link IllegalArgumentException}.
 *
 * @param method the method starting transaction.
 * @return {@code true} if method should be run in read-only transaction, {@code false}
 *          otherwise.
 * @throws IllegalArgumentException if no {@link Transactional} annotation is found
 *          either on the {@code method}}, or on declaring class.
 */
public static boolean isTransactionReadOnly(Method method) {
    final Transactional methodTransactional = AnnotationUtils.findAnnotation(method, Transactional.class);
    final boolean readOnly;
    if (methodTransactional != null) {
        readOnly = methodTransactional.readOnly();
    } else {
        final Class<?> klass = method.getDeclaringClass();
        final Transactional classTransactional = klass.getAnnotation(Transactional.class);
        checkNotNull(classTransactional,
                "Either method or declaring class should be annotated @Transacetional, method: %s", method);
        readOnly = classTransactional.readOnly();
    }
    return readOnly;
}

From source file:com.github.wnameless.factorextractor.FactorExtractor.java

/**
 * Returns a Map{@literal <String, Object>} which is extracted from annotated
 * methods of input objects./*from   ww w .j  ava 2  s. c o  m*/
 * 
 * @param objects
 *          an array of objects
 * @return a Map{@literal <String, Object>}
 */
public static Map<String, Object> extract(Object... objects) {
    for (Object o : objects) {
        if (o == null)
            throw new NullPointerException("Null object is not allowed");
    }

    Map<String, Object> factors = new HashMap<String, Object>();
    for (Object o : objects) {
        Class<?> testClass = o.getClass();
        for (Method m : testClass.getMethods()) {
            Factor factor = AnnotationUtils.findAnnotation(m, Factor.class);
            if (factor != null) {
                try {
                    factors.put(factor.value(), m.invoke(o));
                } catch (InvocationTargetException wrappedExc) {
                    Throwable exc = wrappedExc.getCause();
                    Logger.getLogger(FactorExtractor.class.getName()).log(Level.SEVERE, m + " failed: " + exc,
                            wrappedExc);
                } catch (Exception exc) {
                    Logger.getLogger(FactorExtractor.class.getName()).log(Level.SEVERE,
                            "INVALID @Factor: " + m + exc, exc);
                }
            }
        }
    }

    return factors;
}

From source file:iterator.Reflection.java

public static <A extends Annotation> A findTypeAnnotation(Class<?> clazz, Class<A> annotationClass) {
    return AnnotationUtils.findAnnotation(clazz, annotationClass);
}

From source file:springfox.documentation.builders.RequestHandlerSelectors.java

/**
 * Predicate that matches RequestHandler with handlers methods annotated with given annotation
 *
 * @param annotation - annotation to check
 * @return this//from  ww w .ja  v  a  2s . com
 */
public static Predicate<RequestHandler> withMethodAnnotation(final Class<? extends Annotation> annotation) {
    return new Predicate<RequestHandler>() {
        @Override
        public boolean apply(RequestHandler input) {
            return null != AnnotationUtils.findAnnotation(input.getHandlerMethod().getMethod(), annotation);
        }
    };
}

From source file:com.stitchgalaxy.sg_manager_web.GlobalDefaultExceptionHandler.java

@ExceptionHandler(value = Exception.class)
public ModelAndView defaultErrorHandler(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  www  .j  av a2s . co  m*/

    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    e.printStackTrace(pw);

    ModelAndView mav = new ModelAndView("error");
    mav.addObject("stack_trace", sw.toString());

    return mav;
}