Search in sources :

Example 1 with StandardAnnotationMetadata

use of org.springframework.core.type.StandardAnnotationMetadata in project spring-framework by spring-projects.

the class ConfigurationClassUtils method checkConfigurationClassCandidate.

/**
	 * 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
	 */
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;
}
Also used : Order(org.springframework.core.annotation.Order) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) StandardAnnotationMetadata(org.springframework.core.type.StandardAnnotationMetadata) MetadataReader(org.springframework.core.type.classreading.MetadataReader) IOException(java.io.IOException) AnnotationMetadata(org.springframework.core.type.AnnotationMetadata) StandardAnnotationMetadata(org.springframework.core.type.StandardAnnotationMetadata)

Example 2 with StandardAnnotationMetadata

use of org.springframework.core.type.StandardAnnotationMetadata in project spring-framework by spring-projects.

the class ImportAwareTests method metadataFromImportsTwoThenOne.

@Test
public void metadataFromImportsTwoThenOne() {
    AnnotationMetadata importMetadata = new AnnotationConfigApplicationContext(ConfigurationTwo.class, ConfigurationOne.class).getBean(MetadataHolder.class).importMetadata;
    assertEquals(ConfigurationOne.class, ((StandardAnnotationMetadata) importMetadata).getIntrospectedClass());
}
Also used : AnnotationMetadata(org.springframework.core.type.AnnotationMetadata) StandardAnnotationMetadata(org.springframework.core.type.StandardAnnotationMetadata) Test(org.junit.Test)

Example 3 with StandardAnnotationMetadata

use of org.springframework.core.type.StandardAnnotationMetadata in project spring-framework by spring-projects.

the class ImportAwareTests method metadataFromImportsOneThenTwo.

@Test
public void metadataFromImportsOneThenTwo() {
    AnnotationMetadata importMetadata = new AnnotationConfigApplicationContext(ConfigurationOne.class, ConfigurationTwo.class).getBean(MetadataHolder.class).importMetadata;
    assertEquals(ConfigurationOne.class, ((StandardAnnotationMetadata) importMetadata).getIntrospectedClass());
}
Also used : AnnotationMetadata(org.springframework.core.type.AnnotationMetadata) StandardAnnotationMetadata(org.springframework.core.type.StandardAnnotationMetadata) Test(org.junit.Test)

Example 4 with StandardAnnotationMetadata

use of org.springframework.core.type.StandardAnnotationMetadata in project spring-framework by spring-projects.

the class ConfigurationClassParser method retrieveBeanMethodMetadata.

/**
	 * Retrieve the metadata for all <code>@Bean</code> methods.
	 */
private Set<MethodMetadata> retrieveBeanMethodMetadata(SourceClass sourceClass) {
    AnnotationMetadata original = sourceClass.getMetadata();
    Set<MethodMetadata> beanMethods = original.getAnnotatedMethods(Bean.class.getName());
    if (beanMethods.size() > 1 && original instanceof StandardAnnotationMetadata) {
        // order, even between different runs of the same application on the same JVM.
        try {
            AnnotationMetadata asm = this.metadataReaderFactory.getMetadataReader(original.getClassName()).getAnnotationMetadata();
            Set<MethodMetadata> asmMethods = asm.getAnnotatedMethods(Bean.class.getName());
            if (asmMethods.size() >= beanMethods.size()) {
                Set<MethodMetadata> selectedMethods = new LinkedHashSet<>(asmMethods.size());
                for (MethodMetadata asmMethod : asmMethods) {
                    for (MethodMetadata beanMethod : beanMethods) {
                        if (beanMethod.getMethodName().equals(asmMethod.getMethodName())) {
                            selectedMethods.add(beanMethod);
                            break;
                        }
                    }
                }
                if (selectedMethods.size() == beanMethods.size()) {
                    // All reflection-detected methods found in ASM method set -> proceed
                    beanMethods = selectedMethods;
                }
            }
        } catch (IOException ex) {
            logger.debug("Failed to read class file via ASM for determining @Bean method order", ex);
        // No worries, let's continue with the reflection metadata we started with...
        }
    }
    return beanMethods;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) StandardAnnotationMetadata(org.springframework.core.type.StandardAnnotationMetadata) MethodMetadata(org.springframework.core.type.MethodMetadata) IOException(java.io.IOException) NestedIOException(org.springframework.core.NestedIOException) AnnotationMetadata(org.springframework.core.type.AnnotationMetadata) StandardAnnotationMetadata(org.springframework.core.type.StandardAnnotationMetadata)

Aggregations

AnnotationMetadata (org.springframework.core.type.AnnotationMetadata)4 StandardAnnotationMetadata (org.springframework.core.type.StandardAnnotationMetadata)4 IOException (java.io.IOException)2 Test (org.junit.Test)2 LinkedHashSet (java.util.LinkedHashSet)1 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)1 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)1 NestedIOException (org.springframework.core.NestedIOException)1 Order (org.springframework.core.annotation.Order)1 MethodMetadata (org.springframework.core.type.MethodMetadata)1 MetadataReader (org.springframework.core.type.classreading.MetadataReader)1