Search in sources :

Example 6 with AnnotationTypeFilter

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

the class ClassPathScanningCandidateComponentProviderTests method testWithAspectAnnotationOnly.

@Test
public void testWithAspectAnnotationOnly() throws Exception {
    ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
    provider.addIncludeFilter(new AnnotationTypeFilter(Aspect.class));
    Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE);
    assertEquals(1, candidates.size());
    assertTrue(containsBeanClass(candidates, ServiceInvocationCounter.class));
}
Also used : ServiceInvocationCounter(example.scannable.ServiceInvocationCounter) AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) Aspect(org.aspectj.lang.annotation.Aspect) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) AnnotatedGenericBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition) Test(org.junit.Test)

Example 7 with AnnotationTypeFilter

use of org.springframework.core.type.filter.AnnotationTypeFilter 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 8 with AnnotationTypeFilter

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

use of org.springframework.core.type.filter.AnnotationTypeFilter 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 10 with AnnotationTypeFilter

use of org.springframework.core.type.filter.AnnotationTypeFilter in project opennms by OpenNMS.

the class JaxbUtils method getClassForElement.

public static Class<?> getClassForElement(final String elementName) {
    if (elementName == null)
        return null;
    final Class<?> existing = m_elementClasses.get(elementName);
    if (existing != null)
        return existing;
    final ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
    scanner.addIncludeFilter(new AnnotationTypeFilter(XmlRootElement.class));
    for (final BeanDefinition bd : scanner.findCandidateComponents("org.opennms")) {
        final String className = bd.getBeanClassName();
        try {
            final Class<?> clazz = Class.forName(className);
            final XmlRootElement annotation = clazz.getAnnotation(XmlRootElement.class);
            if (annotation == null) {
                LOG.warn("Somehow found class {} but it has no @XmlRootElement annotation! Skipping.", className);
                continue;
            }
            if (elementName.equalsIgnoreCase(annotation.name())) {
                LOG.trace("Found class {} for element name {}", className, elementName);
                m_elementClasses.put(elementName, clazz);
                return clazz;
            }
        } catch (final ClassNotFoundException e) {
            LOG.warn("Unable to get class object from class name {}. Skipping.", className, e);
        }
    }
    return null;
}
Also used : AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) ClassPathScanningCandidateComponentProvider(org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Aggregations

AnnotationTypeFilter (org.springframework.core.type.filter.AnnotationTypeFilter)28 Test (org.junit.Test)17 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)11 DefaultNamedComponent (example.scannable.DefaultNamedComponent)7 AnnotatedGenericBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition)7 Component (org.springframework.stereotype.Component)7 DevComponent (example.profilescan.DevComponent)6 ProfileAnnotatedComponent (example.profilescan.ProfileAnnotatedComponent)6 ProfileMetaAnnotatedComponent (example.profilescan.ProfileMetaAnnotatedComponent)6 NamedComponent (example.scannable.NamedComponent)6 MetadataReader (org.springframework.core.type.classreading.MetadataReader)6 MetadataReaderFactory (org.springframework.core.type.classreading.MetadataReaderFactory)6 SimpleMetadataReaderFactory (org.springframework.core.type.classreading.SimpleMetadataReaderFactory)6 AssignableTypeFilter (org.springframework.core.type.filter.AssignableTypeFilter)6 ServiceInvocationCounter (example.scannable.ServiceInvocationCounter)5 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)5 FooService (example.scannable.FooService)4 ClassPathScanningCandidateComponentProvider (org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider)4 AutowiredQualifierFooService (example.scannable.AutowiredQualifierFooService)3 CustomComponent (example.scannable.CustomComponent)3