Search in sources :

Example 1 with MetadataReaderFactory

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

the class AnnotationTypeFilterTests method testInheritedAnnotationFromInterfaceDoesNotMatch.

@Test
public void testInheritedAnnotationFromInterfaceDoesNotMatch() throws Exception {
    MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
    String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeSubClassOfSomeComponentInterface";
    MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);
    AnnotationTypeFilter filter = new AnnotationTypeFilter(InheritedAnnotation.class);
    // Must fail as annotation on interfaces should not be considered a match
    assertFalse(filter.match(metadataReader, metadataReaderFactory));
    ClassloadingAssertions.assertClassNotLoaded(classUnderTest);
}
Also used : AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) MetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory) SimpleMetadataReaderFactory(org.springframework.core.type.classreading.SimpleMetadataReaderFactory) SimpleMetadataReaderFactory(org.springframework.core.type.classreading.SimpleMetadataReaderFactory) MetadataReader(org.springframework.core.type.classreading.MetadataReader) Test(org.junit.Test)

Example 2 with MetadataReaderFactory

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

the class AnnotationTypeFilterTests method testInheritedAnnotationFromBaseClassDoesMatch.

@Test
public void testInheritedAnnotationFromBaseClassDoesMatch() throws Exception {
    MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
    String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeSubClassOfSomeComponent";
    MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);
    AnnotationTypeFilter filter = new AnnotationTypeFilter(InheritedAnnotation.class);
    assertTrue(filter.match(metadataReader, metadataReaderFactory));
    ClassloadingAssertions.assertClassNotLoaded(classUnderTest);
}
Also used : AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) MetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory) SimpleMetadataReaderFactory(org.springframework.core.type.classreading.SimpleMetadataReaderFactory) SimpleMetadataReaderFactory(org.springframework.core.type.classreading.SimpleMetadataReaderFactory) MetadataReader(org.springframework.core.type.classreading.MetadataReader) Test(org.junit.Test)

Example 3 with MetadataReaderFactory

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

the class AnnotationTypeFilterTests method testNonAnnotatedClassDoesntMatch.

@Test
public void testNonAnnotatedClassDoesntMatch() throws Exception {
    MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
    String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeNonCandidateClass";
    MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);
    AnnotationTypeFilter filter = new AnnotationTypeFilter(Component.class);
    assertFalse(filter.match(metadataReader, metadataReaderFactory));
    ClassloadingAssertions.assertClassNotLoaded(classUnderTest);
}
Also used : AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) MetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory) SimpleMetadataReaderFactory(org.springframework.core.type.classreading.SimpleMetadataReaderFactory) SimpleMetadataReaderFactory(org.springframework.core.type.classreading.SimpleMetadataReaderFactory) MetadataReader(org.springframework.core.type.classreading.MetadataReader) Test(org.junit.Test)

Example 4 with MetadataReaderFactory

use of org.springframework.core.type.classreading.MetadataReaderFactory in project spring-boot by spring-projects.

the class FilterAnnotationsTests method match.

private boolean match(FilterAnnotations filterAnnotations, Class<?> type) throws IOException {
    MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
    MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(type.getName());
    return filterAnnotations.anyMatches(metadataReader, metadataReaderFactory);
}
Also used : MetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory) SimpleMetadataReaderFactory(org.springframework.core.type.classreading.SimpleMetadataReaderFactory) SimpleMetadataReaderFactory(org.springframework.core.type.classreading.SimpleMetadataReaderFactory) MetadataReader(org.springframework.core.type.classreading.MetadataReader)

Example 5 with MetadataReaderFactory

use of org.springframework.core.type.classreading.MetadataReaderFactory in project spring-boot by spring-projects.

the class TypeExcludeFiltersContextCustomizerFactoryTests method getContextCustomizerShouldAddExcludeFilters.

@Test
public void getContextCustomizerShouldAddExcludeFilters() throws Exception {
    ContextCustomizer customizer = this.factory.createContextCustomizer(WithExcludeFilters.class, null);
    customizer.customizeContext(this.context, this.mergedContextConfiguration);
    this.context.refresh();
    TypeExcludeFilter filter = this.context.getBean(TypeExcludeFilter.class);
    MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
    MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(NoAnnotation.class.getName());
    assertThat(filter.match(metadataReader, metadataReaderFactory)).isFalse();
    metadataReader = metadataReaderFactory.getMetadataReader(SimpleExclude.class.getName());
    assertThat(filter.match(metadataReader, metadataReaderFactory)).isTrue();
    metadataReader = metadataReaderFactory.getMetadataReader(TestClassAwareExclude.class.getName());
    assertThat(filter.match(metadataReader, metadataReaderFactory)).isTrue();
}
Also used : MetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory) SimpleMetadataReaderFactory(org.springframework.core.type.classreading.SimpleMetadataReaderFactory) SimpleMetadataReaderFactory(org.springframework.core.type.classreading.SimpleMetadataReaderFactory) ContextCustomizer(org.springframework.test.context.ContextCustomizer) TypeExcludeFilter(org.springframework.boot.context.TypeExcludeFilter) MetadataReader(org.springframework.core.type.classreading.MetadataReader) Test(org.junit.Test)

Aggregations

MetadataReaderFactory (org.springframework.core.type.classreading.MetadataReaderFactory)46 MetadataReader (org.springframework.core.type.classreading.MetadataReader)42 SimpleMetadataReaderFactory (org.springframework.core.type.classreading.SimpleMetadataReaderFactory)31 Test (org.junit.jupiter.api.Test)20 CachingMetadataReaderFactory (org.springframework.core.type.classreading.CachingMetadataReaderFactory)14 AnnotationTypeFilter (org.springframework.core.type.filter.AnnotationTypeFilter)14 Test (org.junit.Test)9 Resource (org.springframework.core.io.Resource)9 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)8 ResourcePatternResolver (org.springframework.core.io.support.ResourcePatternResolver)7 IOException (java.io.IOException)5 AssignableTypeFilter (org.springframework.core.type.filter.AssignableTypeFilter)4 HashSet (java.util.HashSet)3 Optional (java.util.Optional)3 RequiredArgsConstructor (lombok.RequiredArgsConstructor)3 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)3 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)3 Environment (org.springframework.core.env.Environment)3 ResourceLoader (org.springframework.core.io.ResourceLoader)3 TypeFilter (org.springframework.core.type.filter.TypeFilter)3