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

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

Introduction

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

Prototype

@Nullable
public static <A extends Annotation> A getAnnotation(Method method, Class<A> annotationType) 

Source Link

Document

Get a single Annotation of annotationType from the supplied Method , where the annotation is either present or meta-present on the method.

Usage

From source file:org.sarons.spring4me.web.servlet.DispatcherServlet.java

private void prepareFlashMapForWidget(HttpServletRequest request, Object handler) {
    if (handler instanceof HandlerMethod) {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        ///*from   w w  w. j a  v  a 2 s  . c o  m*/
        Widget widget = AnnotationUtils.getAnnotation(handlerMethod.getBeanType(), Widget.class);
        if (widget != null) {
            //
            String widgetName = widget.value();
            if (!StringUtils.hasText(widgetName)) {
                String targetWidget = handlerMethod.getBeanType().getSimpleName();
                widgetName = StringUtils.uncapitalize(targetWidget);
            }
            //
            FlashMap flashMap = RequestContextUtils.getOutputFlashMap(request);
            flashMap.setTargetRequestPath(widgetName);
            FlashMapManager flashMapManager = RequestContextUtils.getFlashMapManager(request);
            flashMapManager.saveOutputFlashMap(flashMap, request, null);
        }
    }
}

From source file:springfox.documentation.swagger.schema.ApiModelProperties.java

public static Optional<ApiModelProperty> findApiModePropertyAnnotation(AnnotatedElement annotated) {
    return Optional.fromNullable(AnnotationUtils.getAnnotation(annotated, ApiModelProperty.class));
}

From source file:com.watchrabbit.executor.spring.ExecutorAnnotationBeanPostProcessor.java

private Executor findClassAnnotation(Class<?> targetClass) throws IllegalArgumentException {
    return AnnotationUtils.getAnnotation(targetClass, Executor.class);
}

From source file:org.javelin.sws.ext.bind.SweJaxbContextFactory.java

/**
 * <p>Determines mapped classes in the context path and invokes {@link #createContext(Class[], Map)}</p>
 * <p>JAXB-RI uses {@code ObjectFactory} class and/or {@code jaxb.index} file</p>
 * <p>see: com.sun.xml.bind.v2.ContextFactory.createContext(String, ClassLoader, Map<String, Object>)</p>
 * /* www .  ja  v a2  s  . c o  m*/
 * @param contextPath
 * @param classLoader
 * @param properties
 * @return
 * @throws IOException 
 */
public static JAXBContext createContext(String contextPath, ClassLoader classLoader, Map<String, ?> properties)
        throws IOException {
    Assert.notNull(contextPath, "The contextPath should not be null");

    PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(
            classLoader);
    MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resourcePatternResolver);

    List<Class<?>> classes = new LinkedList<Class<?>>();
    // scan the package(s)
    String[] packages = StringUtils.tokenizeToStringArray(contextPath, ":");
    for (String pkg : packages) {
        log.trace("Scanning package: {}", pkg);
        Resource[] resources = resourcePatternResolver
                .getResources(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
                        + ClassUtils.convertClassNameToResourcePath(pkg) + "/*.class");
        for (Resource classResource : resources) {
            MetadataReader mdReader = metadataReaderFactory.getMetadataReader(classResource);
            Class<?> cls = ClassUtils.resolveClassName(mdReader.getClassMetadata().getClassName(), classLoader);
            if (cls.getSimpleName().equals("package-info")) {
                XmlSchema xmlSchema = AnnotationUtils.getAnnotation(cls.getPackage(), XmlSchema.class);
                String namespace = xmlSchema == null || xmlSchema.namespace() == null
                        ? NamespaceUtils.packageNameToNamespace(cls.getPackage())
                        : xmlSchema.namespace();
                log.trace(" - found package-info: {}, namespace: {}", cls.getPackage().getName(), namespace);
            } else {
                log.trace(" - found class: {}", mdReader.getClassMetadata().getClassName());
                classes.add(cls);
            }
        }
    }

    return createContext(classes.toArray(new Class[0]), properties);
}

From source file:org.jdal.aop.config.SerializableAnnotationBeanPostProcessor.java

/**
 * Lookup for {@link SerializableProxy} annotation on fields or methods and 
 * replace value with a Serializable proxy.
 *///  ww w  . j a  v a2  s.  c o  m
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

    List<AnnotatedElement> elements = AnnotatedElementAccessor.findAnnotatedElements(SerializableProxy.class,
            bean.getClass());

    for (AnnotatedElement element : elements) {
        ResolvableType type = getResolvableType(element);
        Object value = AnnotatedElementAccessor.getValue(element, bean);
        if (value != null && !(value instanceof SerializableAopProxy)) {
            SerializableProxy ann = AnnotationUtils.getAnnotation(element, SerializableProxy.class);
            boolean proxyTargetClass = !type.resolve().isInterface() || ann.proxyTargetClass();
            Object proxy = getProxy(value, proxyTargetClass, ann.useCache(), getDependencyDescriptor(element),
                    beanName);
            if (proxy != null)
                AnnotatedElementAccessor.setValue(element, bean, proxy);
        }
    }

    return bean;
}

From source file:com.watchrabbit.executor.spring.ExecutorAnnotationBeanPostProcessor.java

private Map<Method, Executor> findAnnotatedMethods(Class<?> targetClass) throws IllegalArgumentException {
    Map<Method, Executor> annotatedMethods = new HashMap<>();
    ReflectionUtils.doWithMethods(targetClass, (Method method) -> {
        Executor annotation = AnnotationUtils.getAnnotation(method, Executor.class);
        if (annotation != null) {
            annotatedMethods.put(method, annotation);
        }//  w ww . j a va 2 s.com
    });
    return annotatedMethods;
}

From source file:com.expedia.seiso.domain.meta.ItemMetaImpl.java

@Override
public Method getRepositorySearchMethod(String search) {
    val methods = itemRepoInterface.getMethods();
    for (val method : methods) {
        val ann = AnnotationUtils.getAnnotation(method, RestResource.class);
        if (ann != null && search.equals(ann.path())) {
            return method;
        }//from  ww w .  ja v a2 s .c o m
    }
    return null;
}

From source file:org.okj.commons.annotations.ServiceReferenceInjectionBeanPostProcessor.java

private void injectServices(final Object bean, final String beanName) {
    ReflectionUtils.doWithMethods(bean.getClass(), new ReflectionUtils.MethodCallback() {

        public void doWith(Method method) {
            ServiceReference s = AnnotationUtils.getAnnotation(method, ServiceReference.class);
            if (s != null && method.getParameterTypes().length == 1) {
                try {
                    if (logger.isDebugEnabled())
                        logger.debug("Processing annotation [" + s + "] for [" + bean.getClass().getName() + "."
                                + method.getName() + "()] on bean [" + beanName + "]");
                    method.invoke(bean, getServiceImporter(s, method, beanName).getObject());
                } catch (Exception e) {
                    throw new IllegalArgumentException("Error processing service annotation", e);
                }/*from   ww  w  .j  a va 2  s.co m*/
            }
        }
    });
}

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

protected void validate(Message<?> message, MethodParameter parameter, Object target) {
    if (this.validator == null) {
        return;/*from w w  w.ja  v a  2 s.c  o  m*/
    }
    for (Annotation ann : parameter.getParameterAnnotations()) {
        Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
        if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
            Object hints = validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann);
            Object[] validationHints = hints instanceof Object[] ? (Object[]) hints : new Object[] { hints };
            BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(target,
                    getParameterName(parameter));
            if (!ObjectUtils.isEmpty(validationHints) && this.validator instanceof SmartValidator) {
                ((SmartValidator) this.validator).validate(target, bindingResult, validationHints);
            } else {
                this.validator.validate(target, bindingResult);
            }
            if (bindingResult.hasErrors()) {
                throw new MethodArgumentNotValidException(message, parameter, bindingResult);
            }
            break;
        }
    }
}