Example usage for org.springframework.core.type StandardAnnotationMetadata StandardAnnotationMetadata

List of usage examples for org.springframework.core.type StandardAnnotationMetadata StandardAnnotationMetadata

Introduction

In this page you can find the example usage for org.springframework.core.type StandardAnnotationMetadata StandardAnnotationMetadata.

Prototype

@Deprecated
public StandardAnnotationMetadata(Class<?> introspectedClass, boolean nestedAnnotationsAsMap) 

Source Link

Document

Create a new StandardAnnotationMetadata wrapper for the given Class, providing the option to return any nested annotations or annotation arrays in the form of org.springframework.core.annotation.AnnotationAttributes instead of actual Annotation instances.

Usage

From source file:com.github.yulechen.springannotation.test.ConfigurationClassUtils.java

/**
 * Check whether the given bean definition is a candidate for a
 * configuration class (or a nested component class declared within a
 * configuration/component class, to be auto-registered as well), and mark
 * it accordingly./*from   w ww .  java2  s.  co m*/
 * 
 * @param beanDef
 *            the bean definition to check
 * @param metadataReaderFactory
 *            the current factory in use by the caller
 * @return whether the candidate qualifies as (any kind of) configuration
 *         class
 */
public static boolean checkConfigurationClassCandidate(BeanDefinition beanDef,
        MetadataReaderFactory metadataReaderFactory) {
    String className = beanDef.getBeanClassName();
    if (className == null) {
        return false;
    }

    AnnotationMetadata metadata;
    if (beanDef instanceof AnnotatedBeanDefinition
            && className.equals(((AnnotatedBeanDefinition) beanDef).getMetadata().getClassName())) {
        // Can reuse the pre-parsed metadata from the given
        // BeanDefinition...
        metadata = ((AnnotatedBeanDefinition) beanDef).getMetadata();
    } else if (beanDef instanceof AbstractBeanDefinition && ((AbstractBeanDefinition) beanDef).hasBeanClass()) {
        // Check already loaded Class if present...
        // since we possibly can't even load the class file for this Class.
        Class<?> beanClass = ((AbstractBeanDefinition) beanDef).getBeanClass();
        metadata = new StandardAnnotationMetadata(beanClass, true);
    } else {
        try {
            MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(className);
            metadata = metadataReader.getAnnotationMetadata();
        } catch (IOException ex) {
            if (logger.isDebugEnabled()) {
                logger.debug(
                        "Could not find class file for introspecting configuration annotations: " + className,
                        ex);
            }
            return false;
        }
    }

    if (isFullConfigurationCandidate(metadata)) {
        beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_FULL);
    } else if (isLiteConfigurationCandidate(metadata)) {
        beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_LITE);
    } else {
        return false;
    }

    // It's a full or lite configuration candidate... Let's determine the
    // order value, if any.
    Map<String, Object> orderAttributes = metadata.getAnnotationAttributes(Order.class.getName());
    if (orderAttributes != null) {
        beanDef.setAttribute(ORDER_ATTRIBUTE, orderAttributes.get(AnnotationUtils.VALUE));
    }

    return true;
}

From source file:org.springframework.boot.autoconfigure.ComponentScanDetector.java

private AnnotationMetadata getMetadata(BeanDefinition beanDefinition) {
    if (beanDefinition instanceof AbstractBeanDefinition
            && ((AbstractBeanDefinition) beanDefinition).hasBeanClass()) {
        Class<?> beanClass = ((AbstractBeanDefinition) beanDefinition).getBeanClass();
        if (Enhancer.isEnhanced(beanClass)) {
            beanClass = beanClass.getSuperclass();
        }/*w  w  w  . j  a  v  a 2  s .c o m*/
        return new StandardAnnotationMetadata(beanClass, true);
    }
    String className = beanDefinition.getBeanClassName();
    if (className != null) {
        try {
            MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(className);
            return metadataReader.getAnnotationMetadata();
        } catch (IOException ex) {
            if (this.logger.isDebugEnabled()) {
                this.logger.debug(
                        "Could not find class file for introspecting @ComponentScan classes: " + className, ex);
            }
        }
    }
    return null;
}

From source file:org.springframework.boot.autoconfigure.data.AbstractRepositoryConfigurationSourceSupport.java

private AnnotationRepositoryConfigurationSource getConfigurationSource() {
    StandardAnnotationMetadata metadata = new StandardAnnotationMetadata(getConfiguration(), true);
    AnnotationRepositoryConfigurationSource configurationSource = new AnnotationRepositoryConfigurationSource(
            metadata, getAnnotation(), this.environment) {

        @Override/*from w ww.j a v  a  2s  .c  o m*/
        public java.lang.Iterable<String> getBasePackages() {
            return AbstractRepositoryConfigurationSourceSupport.this.getBasePackages();
        };
    };
    return configurationSource;
}

From source file:org.springframework.context.annotation.ConfigurationClassUtils.java

/**
 * Check whether the given bean definition is a candidate for a configuration class
 * (or a nested component class declared within a configuration/component class,
 * to be auto-registered as well), and mark it accordingly.
 * @param beanDef the bean definition to check
 * @param metadataReaderFactory the current factory in use by the caller
 * @return whether the candidate qualifies as (any kind of) configuration class
 *//*from   www  .java  2s .c  o  m*/
public static boolean checkConfigurationClassCandidate(BeanDefinition beanDef,
        MetadataReaderFactory metadataReaderFactory) {
    String className = beanDef.getBeanClassName();
    if (className == null || beanDef.getFactoryMethodName() != null) {
        return false;
    }

    AnnotationMetadata metadata;
    if (beanDef instanceof AnnotatedBeanDefinition
            && className.equals(((AnnotatedBeanDefinition) beanDef).getMetadata().getClassName())) {
        // Can reuse the pre-parsed metadata from the given BeanDefinition...
        metadata = ((AnnotatedBeanDefinition) beanDef).getMetadata();
    } else if (beanDef instanceof AbstractBeanDefinition && ((AbstractBeanDefinition) beanDef).hasBeanClass()) {
        // Check already loaded Class if present...
        // since we possibly can't even load the class file for this Class.
        Class<?> beanClass = ((AbstractBeanDefinition) beanDef).getBeanClass();
        metadata = new StandardAnnotationMetadata(beanClass, true);
    } else {
        try {
            MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(className);
            metadata = metadataReader.getAnnotationMetadata();
        } catch (IOException ex) {
            if (logger.isDebugEnabled()) {
                logger.debug(
                        "Could not find class file for introspecting configuration annotations: " + className,
                        ex);
            }
            return false;
        }
    }

    if (isFullConfigurationCandidate(metadata)) {
        beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_FULL);
    } else if (isLiteConfigurationCandidate(metadata)) {
        beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_LITE);
    } else {
        return false;
    }

    // It's a full or lite configuration candidate... Let's determine the order value, if any.
    Integer order = getOrder(metadata);
    if (order != null) {
        beanDef.setAttribute(ORDER_ATTRIBUTE, order);
    }

    return true;
}