Search in sources :

Example 6 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 7 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)

Example 8 with AnnotationMetadata

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

use of org.springframework.core.type.AnnotationMetadata in project leopard by tanhaichao.

the class OptionScanner method getValue.

protected String getValue(BeanDefinition beanDefinition) {
    AnnotationMetadata metadata = ((AnnotatedBeanDefinition) beanDefinition).getMetadata();
    AnnotationAttributes attributes = AnnotationAttributes.fromMap(metadata.getAnnotationAttributes(Option.class.getName(), false));
    return attributes.getString("value");
}
Also used : AnnotationAttributes(org.springframework.core.annotation.AnnotationAttributes) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) AnnotationMetadata(org.springframework.core.type.AnnotationMetadata)

Example 10 with AnnotationMetadata

use of org.springframework.core.type.AnnotationMetadata 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)22 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)4 MetadataReader (org.springframework.core.type.classreading.MetadataReader)3 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)2 ThymeleafAutoConfiguration (org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration)2 Resource (org.springframework.core.io.Resource)2 MethodMetadata (org.springframework.core.type.MethodMetadata)2 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 AnnotatedGenericBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition)1 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)1