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.springframework.aop.aspectj.annotation.AbstractAspectJAdvisorFactory.java

private boolean hasAspectAnnotation(Class<?> clazz) {
    return (AnnotationUtils.findAnnotation(clazz, Aspect.class) != null);
}

From source file:org.springframework.aop.aspectj.annotation.AbstractAspectJAdvisorFactory.java

@Nullable
private static <A extends Annotation> AspectJAnnotation<A> findAnnotation(Method method, Class<A> toLookFor) {
    A result = AnnotationUtils.findAnnotation(method, toLookFor);
    if (result != null) {
        return new AspectJAnnotation<>(result);
    } else {//  w  w w.j ava2  s. c o m
        return null;
    }
}

From source file:org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter.java

protected void registerEndpoint(String beanName, Endpoint<?> endpoint) {
    @SuppressWarnings("rawtypes")
    Class<? extends Endpoint> type = endpoint.getClass();
    if (AnnotationUtils.findAnnotation(type, ManagedResource.class) != null) {
        // Already managed
        return;//from w  w  w .  ja  va2 s .c  om
    }
    if (type.isMemberClass()
            && AnnotationUtils.findAnnotation(type.getEnclosingClass(), ManagedResource.class) != null) {
        // Nested class with @ManagedResource in parent
        return;
    }
    try {
        registerBeanNameOrInstance(getEndpointMBean(beanName, endpoint), beanName);
    } catch (MBeanExportException ex) {
        logger.error("Could not register MBean for endpoint [" + beanName + "]", ex);
    }
}

From source file:org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetrics.java

private Stream<Timed> findTimedAnnotations(AnnotatedElement element) {
    Timed timed = AnnotationUtils.findAnnotation(element, Timed.class);
    if (timed != null) {
        return Stream.of(timed);
    }/*from  w  ww.j ava2s .c  o  m*/
    TimedSet ts = AnnotationUtils.findAnnotation(element, TimedSet.class);
    if (ts != null) {
        return Arrays.stream(ts.value());
    }
    return Stream.empty();
}

From source file:org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration.java

private void resolveApplicationPath() {
    if (StringUtils.hasLength(this.jersey.getApplicationPath())) {
        this.path = parseApplicationPath(this.jersey.getApplicationPath());
    } else {//  www.  ja  v  a  2 s.co m
        this.path = findApplicationPath(
                AnnotationUtils.findAnnotation(this.config.getClass(), ApplicationPath.class));
    }
}

From source file:org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.java

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    ConfigurationProperties annotation = AnnotationUtils.findAnnotation(bean.getClass(),
            ConfigurationProperties.class);
    if (annotation != null) {
        postProcessBeforeInitialization(bean, beanName, annotation);
    }//from   www  . j a v  a 2  s. com
    annotation = this.beans.findFactoryAnnotation(beanName, ConfigurationProperties.class);
    if (annotation != null) {
        postProcessBeforeInitialization(bean, beanName, annotation);
    }
    return bean;
}

From source file:org.springframework.boot.context.web.SpringBootServletInitializer.java

protected WebApplicationContext createRootApplicationContext(ServletContext servletContext) {
    SpringApplicationBuilder builder = createSpringApplicationBuilder();
    builder.main(getClass());/*from w w  w  . j av  a2s  .c om*/
    ApplicationContext parent = getExistingRootWebApplicationContext(servletContext);
    if (parent != null) {
        this.logger.info("Root context already created (using as parent).");
        servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, null);
        builder.initializers(new ParentContextApplicationContextInitializer(parent));
    }
    builder.initializers(new ServletContextApplicationContextInitializer(servletContext));
    builder.contextClass(AnnotationConfigEmbeddedWebApplicationContext.class);
    builder = configure(builder);
    SpringApplication application = builder.build();
    if (application.getSources().isEmpty()
            && AnnotationUtils.findAnnotation(getClass(), Configuration.class) != null) {
        application.getSources().add(getClass());
    }
    Assert.state(application.getSources().size() > 0,
            "No SpringApplication sources have been defined. Either override the "
                    + "configure method or add an @Configuration annotation");
    // Ensure error pages are registered
    if (this.registerErrorPageFilter) {
        application.getSources().add(ErrorPageFilter.class);
    }
    return run(application);
}

From source file:org.springframework.boot.web.servlet.support.SpringBootServletInitializer.java

protected WebApplicationContext createRootApplicationContext(ServletContext servletContext) {
    SpringApplicationBuilder builder = createSpringApplicationBuilder();
    builder.main(getClass());/* w ww.  j av a  2 s  . c om*/
    ApplicationContext parent = getExistingRootWebApplicationContext(servletContext);
    if (parent != null) {
        this.logger.info("Root context already created (using as parent).");
        servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, null);
        builder.initializers(new ParentContextApplicationContextInitializer(parent));
    }
    builder.initializers(new ServletContextApplicationContextInitializer(servletContext));
    builder.contextClass(AnnotationConfigServletWebServerApplicationContext.class);
    builder = configure(builder);
    builder.listeners(new WebEnvironmentPropertySourceInitializer(servletContext));
    SpringApplication application = builder.build();
    if (application.getAllSources().isEmpty()
            && AnnotationUtils.findAnnotation(getClass(), Configuration.class) != null) {
        application.addPrimarySources(Collections.singleton(getClass()));
    }
    Assert.state(!application.getAllSources().isEmpty(),
            "No SpringApplication sources have been defined. Either override the "
                    + "configure method or add an @Configuration annotation");
    // Ensure error pages are registered
    if (this.registerErrorPageFilter) {
        application.addPrimarySources(Collections.singleton(ErrorPageFilterConfiguration.class));
    }
    return run(application);
}

From source file:org.springframework.boot.web.support.SpringBootServletInitializer.java

protected WebApplicationContext createRootApplicationContext(ServletContext servletContext) {
    SpringApplicationBuilder builder = createSpringApplicationBuilder();
    builder.main(getClass());/*  w w w . ja v  a  2 s.c o  m*/
    ApplicationContext parent = getExistingRootWebApplicationContext(servletContext);
    if (parent != null) {
        this.logger.info("Root context already created (using as parent).");
        servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, null);
        builder.initializers(new ParentContextApplicationContextInitializer(parent));
    }
    builder.initializers(new ServletContextApplicationContextInitializer(servletContext));
    builder.listeners(new ServletContextApplicationListener(servletContext));
    builder.contextClass(AnnotationConfigEmbeddedWebApplicationContext.class);
    builder = configure(builder);
    SpringApplication application = builder.build();
    if (application.getSources().isEmpty()
            && AnnotationUtils.findAnnotation(getClass(), Configuration.class) != null) {
        application.getSources().add(getClass());
    }
    Assert.state(!application.getSources().isEmpty(),
            "No SpringApplication sources have been defined. Either override the "
                    + "configure method or add an @Configuration annotation");
    // Ensure error pages are registered
    if (this.registerErrorPageFilter) {
        application.getSources().add(ErrorPageFilterConfiguration.class);
    }
    return run(application);
}

From source file:org.springframework.cloud.sleuth.annotation.SleuthAnnotationUtils.java

/**
 * Searches for an annotation either on a method or inside the method parameters
 *///from  w w  w  . j  a  v a2  s  .  c  o m
static <T extends Annotation> T findAnnotation(Method method, Class<T> clazz) {
    T annotation = AnnotationUtils.findAnnotation(method, clazz);
    if (annotation == null) {
        try {
            annotation = AnnotationUtils.findAnnotation(
                    method.getDeclaringClass().getMethod(method.getName(), method.getParameterTypes()), clazz);
        } catch (NoSuchMethodException | SecurityException e) {
            if (log.isDebugEnabled()) {
                log.debug("Exception occurred while tyring to find the annotation", e);
            }
        }
    }
    return annotation;
}