Search in sources :

Example 1 with AnnotationMetadata

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

the class ConfigurationClassParser method isChainedImportOnStack.

private boolean isChainedImportOnStack(ConfigurationClass configClass) {
    if (this.importStack.contains(configClass)) {
        String configClassName = configClass.getMetadata().getClassName();
        AnnotationMetadata importingClass = this.importStack.getImportingClassFor(configClassName);
        while (importingClass != null) {
            if (configClassName.equals(importingClass.getClassName())) {
                return true;
            }
            importingClass = this.importStack.getImportingClassFor(importingClass.getClassName());
        }
    }
    return false;
}
Also used : AnnotationMetadata(org.springframework.core.type.AnnotationMetadata) StandardAnnotationMetadata(org.springframework.core.type.StandardAnnotationMetadata)

Example 2 with AnnotationMetadata

use of org.springframework.core.type.AnnotationMetadata 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 3 with AnnotationMetadata

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

the class AnnotationBeanNameGenerator method determineBeanNameFromAnnotation.

/**
	 * Derive a bean name from one of the annotations on the class.
	 * @param annotatedDef the annotation-aware bean definition
	 * @return the bean name, or {@code null} if none is found
	 */
protected String determineBeanNameFromAnnotation(AnnotatedBeanDefinition annotatedDef) {
    AnnotationMetadata amd = annotatedDef.getMetadata();
    Set<String> types = amd.getAnnotationTypes();
    String beanName = null;
    for (String type : types) {
        AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor(amd, type);
        if (isStereotypeWithNameValue(type, amd.getMetaAnnotationTypes(type), attributes)) {
            Object value = attributes.get("value");
            if (value instanceof String) {
                String strVal = (String) value;
                if (StringUtils.hasLength(strVal)) {
                    if (beanName != null && !strVal.equals(beanName)) {
                        throw new IllegalStateException("Stereotype annotations suggest inconsistent " + "component names: '" + beanName + "' versus '" + strVal + "'");
                    }
                    beanName = strVal;
                }
            }
        }
    }
    return beanName;
}
Also used : AnnotationAttributes(org.springframework.core.annotation.AnnotationAttributes) AnnotationMetadata(org.springframework.core.type.AnnotationMetadata)

Example 4 with AnnotationMetadata

use of org.springframework.core.type.AnnotationMetadata 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 5 with AnnotationMetadata

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

the class ImportAwareTests method indirectlyAnnotatedWithImport.

@Test
public void indirectlyAnnotatedWithImport() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(IndirectlyImportingConfig.class);
    ctx.refresh();
    assertNotNull(ctx.getBean("importedConfigBean"));
    ImportedConfig importAwareConfig = ctx.getBean(ImportedConfig.class);
    AnnotationMetadata importMetadata = importAwareConfig.importMetadata;
    assertThat("import metadata was not injected", importMetadata, notNullValue());
    assertThat(importMetadata.getClassName(), is(IndirectlyImportingConfig.class.getName()));
    AnnotationAttributes enableAttribs = AnnotationConfigUtils.attributesFor(importMetadata, EnableImportedConfig.class);
    String foo = enableAttribs.getString("foo");
    assertThat(foo, is("xyz"));
}
Also used : AnnotationAttributes(org.springframework.core.annotation.AnnotationAttributes) AnnotationMetadata(org.springframework.core.type.AnnotationMetadata) StandardAnnotationMetadata(org.springframework.core.type.StandardAnnotationMetadata) Test(org.junit.Test)

Aggregations

AnnotationMetadata (org.springframework.core.type.AnnotationMetadata)20 Test (org.junit.Test)13 StandardAnnotationMetadata (org.springframework.core.type.StandardAnnotationMetadata)7 FreeMarkerAutoConfiguration (org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration)5 AnnotationAttributes (org.springframework.core.annotation.AnnotationAttributes)3 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 ThymeleafAutoConfiguration (org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration)2 MethodMetadata (org.springframework.core.type.MethodMetadata)2 MetadataReader (org.springframework.core.type.classreading.MetadataReader)2 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)1 AnnotatedGenericBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition)1 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)1 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)1 Bean (org.springframework.context.annotation.Bean)1 Configuration (org.springframework.context.annotation.Configuration)1 ConfigurationPhase (org.springframework.context.annotation.ConfigurationCondition.ConfigurationPhase)1 NestedIOException (org.springframework.core.NestedIOException)1