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.jspringbot.spring.KeywordUtils.java

/**
 * Builds mapping of robot keywords to actual bean names in spring context.
 *
 * @param context Spring application context.
 * @return mapping of robot keywords to bean names
 *//* www .j av  a 2  s  .  c  o  m*/
public static Map<String, String> getKeywordMap(ApplicationContext context) {
    Map<String, String> keywordToBeanMap = new HashMap<String, String>();

    // Retrieve beans implementing the Keyword interface.
    String[] beanNames = context.getBeanNamesForType(Keyword.class);

    for (String beanName : beanNames) {
        Object bean = context.getBean(beanName);

        // Retrieve keyword information
        KeywordInfo keywordInfo = AnnotationUtils.findAnnotation(bean.getClass(), KeywordInfo.class);

        // Set keyword name as specified in the keyword info, or if information is not available, use bean name.
        String keywordName = keywordInfo != null ? keywordInfo.name() : beanName;

        if (keywordToBeanMap.put(keywordName, beanName) != null) {
            // If map already contains the keyword name, throw an exception. Keywords should be unique.
            throw new RuntimeException("Multiple definitions for keyword '" + keywordName + "' exists.");
        }
    }

    return keywordToBeanMap;
}

From source file:com.sg.rest.controllers.RestErrorHandler.java

@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody/* w w w.j  a v  a2 s  . c o  m*/
public ServerException processException(Exception ex) throws Exception {

    ServerException dto = new ServerException();
    LOGGER.error("Rest exception occured " + dto.getEventRef().getId() + ": ", ex);

    if (AnnotationUtils.findAnnotation(ex.getClass(), ResponseStatus.class) != null) {
        throw ex;
    }

    return dto;
}

From source file:com.fengduo.spark.commons.component.ComponentController.java

@ExceptionHandler(Throwable.class)
public ModelAndView handleIOException(Throwable e) throws Throwable {

    if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) {
        throw e;//from w w w  . ja  v  a  2 s  .c o m
    }

    if (request == null && response == null) {
        throw e;
    }

    if (request == null && response != null) {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        OutputStream out = response.getOutputStream();
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(out, "utf-8"));
        pw.println("{\"code\":1,\"msg\":\",?!\",\"data\":\"\"}");
        pw.flush();
        pw.close();
    }

    ModelAndView mav = new ModelAndView();
    // if (InvokeTypeTools.isAjax(request)) {
    // return createJsonMav("server exceptin or error", ResultCode.ERROR, e.getMessage());
    // }

    mav.addObject("exception", e.getCause() == null ? StringUtils.EMPTY : e.getCause().toString());
    mav.addObject("msg", StringUtils.isEmpty(e.getMessage()) ? e.toString() : e.getMessage());
    mav.addObject("stackTrace", e.getStackTrace().toString());
    if (request.getRequestURI() != null) {
        mav.addObject("url", request.getRequestURI().toString());
    }
    mav.setViewName("error");
    return mav;
}

From source file:com.dianping.squirrel.client.spring.StoreInterceptor.java

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    Method method = invocation.getMethod();
    Store store = AnnotationUtils.findAnnotation(method, Store.class);
    if (store == null) {
        Class<? extends Object> targetClazz = invocation.getThis().getClass();
        method = MethodUtils.getAccessibleMethod(targetClazz, method.getName(), method.getParameterTypes());
        store = AnnotationUtils.findAnnotation(method, Store.class);
    }/*from   www  .ja va  2 s  .  c o m*/
    if (store != null) {

        StoreOperation operation = store.operation();

        StoreKey storeKey = StoreAnnotationUtils.getStoreKey(method, invocation.getArguments());

        if (operation == StoreOperation.SetAndGet) {
            Object storedItem = storeClient.get(storeKey);

            if (storedItem != null) {
                return storedItem;
            }

            Object item = invocation.proceed();
            // TODO: consider create an null object instead of null
            if (item != null) {
                storeClient.add(storeKey, item);
            }

            return item;
        } else if (operation == StoreOperation.Update || operation == StoreOperation.Remove) {
            storeClient.delete(storeKey);
            return invocation.proceed();
        }
    }
    return invocation.proceed();
}

From source file:com.dianping.avatar.cache.interceptor.CacheInterceptor.java

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    Method method = invocation.getMethod();
    Cache cache = AnnotationUtils.findAnnotation(method, Cache.class);
    if (cache == null) {
        Class<? extends Object> targetClazz = invocation.getThis().getClass();
        method = MethodUtils.getAccessibleMethod(targetClazz, method.getName(), method.getParameterTypes());
        cache = AnnotationUtils.findAnnotation(method, Cache.class);
    }//from w  w  w  . j  a v a  2s. com
    if (cache != null) {

        CacheOperation operation = cache.operation();

        CacheKey cacheKey = CacheAnnotationUtils.getCacheKey(method, invocation.getArguments());

        if (operation == CacheOperation.SetAndGet) {
            Object cachedItem = cacheService.get(cacheKey);

            if (cachedItem != null) {
                return cachedItem;
            }

            Object item = invocation.proceed();
            // consider create an null object instead of null
            cacheService.add(cacheKey, item);

            return item;
        } else if (operation == CacheOperation.Update || operation == CacheOperation.Remove) {
            cacheService.remove(cacheKey);
            return invocation.proceed();
        }
    }
    return invocation.proceed();
}

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

/** {@inheritDoc} */
@Override/* w  w  w  .  j  av  a2s.  c  o m*/
public boolean matches(TypeLiteral<?> element) {
    Method[] methods = element.getRawType().getMethods();
    boolean matches = false;
    for (Method method : methods) {
        Annotation annotation = AnnotationUtils.findAnnotation(method, m_annotation.annotationType());
        return ((annotation != null) && m_annotation.equals(annotation));
    }
    return matches;
}

From source file:com.fengduo.bee.commons.component.ComponentController.java

@ExceptionHandler(Throwable.class)
public ModelAndView handleIOException(Throwable e) throws Throwable {

    if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) {
        throw e;//w w  w  . ja v  a2 s  .c  om
    }

    if (request == null && response == null) {
        throw e;
    }

    if (request == null && response != null) {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        OutputStream out = response.getOutputStream();
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(out, "utf-8"));
        pw.println("{\"code\":1,\"msg\":\",?!\",\"data\":\"\"}");
        pw.flush();
        pw.close();
    }

    ModelAndView mav = new ModelAndView();
    // if (InvokeTypeTools.isAjax(request)) {
    // return createJsonMav("server exceptin or error", ResultCode.ERROR,
    // e.getMessage());
    // }

    mav.addObject("exception", e.getCause() == null ? StringUtils.EMPTY : e.getCause().toString());
    mav.addObject("msg", StringUtils.isEmpty(e.getMessage()) ? e.toString() : e.getMessage());
    mav.addObject("stackTrace", e.getStackTrace().toString());
    if (request.getRequestURI() != null) {
        mav.addObject("url", request.getRequestURI().toString());
    }
    mav.setViewName("error");
    return mav;
}

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

@Override
protected RequestCondition<?> getCustomMethodCondition(Method method) {
    CloudElementApi cloudElementApiMethodAnnotation = AnnotationUtils.findAnnotation(method,
            CloudElementApi.class);

    return createElementCondition(cloudElementApiMethodAnnotation);
}

From source file:org.jdal.aop.DeclareMixinAspectJAdvisorFactory.java

@Override
public Advisor getAdvisor(Method candidateAdviceMethod, MetadataAwareAspectInstanceFactory aif,
        int declarationOrderInAspect, String aspectName) {

    Advisor advisor = super.getAdvisor(candidateAdviceMethod, aif, declarationOrderInAspect, aspectName);

    // test declare mixin annotation
    if (advisor == null) {

        DeclareMixin declareMixin = AnnotationUtils.findAnnotation(candidateAdviceMethod, DeclareMixin.class);

        if (declareMixin != null) {
            advisor = new DeclareMixinAdvisor(candidateAdviceMethod, aif.getAspectInstance(),
                    declareMixin.value());
        }/* www.ja  va 2  s .c om*/
    }

    return advisor;
}

From source file:com.cassius.spring.assembly.test.common.toolbox.ContextUtil.java

/**
 * Gets spring context name.//from ww  w . j  a va  2s.c om
 *
 * @param testClass the test class
 * @return the spring context name
 */
public static String getSpringContextName(Class<?> testClass) {
    String contextName = DEFAULT_CONTEXT_NAME;
    SpringAssemblyConfigure springAssemblyConfigure = AnnotationUtils.findAnnotation(testClass,
            SpringAssemblyConfigure.class);
    if (springAssemblyConfigure.reuseSpringContext()) {
        return testClass.getName();
    }
    return contextName;
}