Search in sources :

Example 21 with AnnotationMetadata

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

the class AbstractConfigurationClassTests method findConfigurationClasses.

private Set<AnnotationMetadata> findConfigurationClasses() throws IOException {
    Set<AnnotationMetadata> configurationClasses = new HashSet<>();
    Resource[] resources = this.resolver.getResources("classpath*:" + getClass().getPackage().getName().replace('.', '/') + "/**/*.class");
    for (Resource resource : resources) {
        if (!isTestClass(resource)) {
            MetadataReader metadataReader = new SimpleMetadataReaderFactory().getMetadataReader(resource);
            AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
            if (annotationMetadata.getAnnotationTypes().contains(Configuration.class.getName())) {
                configurationClasses.add(annotationMetadata);
            }
        }
    }
    return configurationClasses;
}
Also used : SimpleMetadataReaderFactory(org.springframework.core.type.classreading.SimpleMetadataReaderFactory) Configuration(org.springframework.context.annotation.Configuration) Resource(org.springframework.core.io.Resource) MetadataReader(org.springframework.core.type.classreading.MetadataReader) AnnotationMetadata(org.springframework.core.type.AnnotationMetadata) HashSet(java.util.HashSet)

Example 22 with AnnotationMetadata

use of org.springframework.core.type.AnnotationMetadata in project cxf by apache.

the class SpringClasspathScanner method findClassesInternal.

protected Map<Class<? extends Annotation>, Collection<Class<?>>> findClassesInternal(Collection<String> basePackages, List<Class<? extends Annotation>> annotations, ClassLoader loader) throws IOException, ClassNotFoundException {
    ResourcePatternResolver resolver = getResolver(loader);
    MetadataReaderFactory factory = new CachingMetadataReaderFactory(resolver);
    final Map<Class<? extends Annotation>, Collection<Class<?>>> classes = new HashMap<Class<? extends Annotation>, Collection<Class<?>>>();
    final Map<Class<? extends Annotation>, Collection<String>> matchingInterfaces = new HashMap<Class<? extends Annotation>, Collection<String>>();
    final Map<String, String[]> nonMatchingClasses = new HashMap<>();
    for (Class<? extends Annotation> annotation : annotations) {
        classes.put(annotation, new HashSet<Class<?>>());
        matchingInterfaces.put(annotation, new HashSet<String>());
    }
    if (basePackages == null || basePackages.isEmpty()) {
        return classes;
    }
    for (final String basePackage : basePackages) {
        final boolean scanAllPackages = basePackage.equals(WILDCARD);
        final String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + (scanAllPackages ? "" : ClassUtils.convertClassNameToResourcePath(basePackage)) + ALL_CLASS_FILES;
        final Resource[] resources = resolver.getResources(packageSearchPath);
        for (final Resource resource : resources) {
            final MetadataReader reader = factory.getMetadataReader(resource);
            final AnnotationMetadata metadata = reader.getAnnotationMetadata();
            if (scanAllPackages && shouldSkip(metadata.getClassName())) {
                continue;
            }
            for (Class<? extends Annotation> annotation : annotations) {
                boolean concreteClass = !metadata.isInterface() && !metadata.isAbstract();
                if (metadata.isAnnotated(annotation.getName())) {
                    if (concreteClass) {
                        classes.get(annotation).add(loadClass(metadata.getClassName(), loader));
                    } else {
                        matchingInterfaces.get(annotation).add(metadata.getClassName());
                    }
                } else if (concreteClass && metadata.getInterfaceNames().length > 0) {
                    nonMatchingClasses.put(metadata.getClassName(), metadata.getInterfaceNames());
                }
            }
        }
    }
    if (!nonMatchingClasses.isEmpty()) {
        for (Map.Entry<Class<? extends Annotation>, Collection<String>> e1 : matchingInterfaces.entrySet()) {
            for (Map.Entry<String, String[]> e2 : nonMatchingClasses.entrySet()) {
                for (String intName : e2.getValue()) {
                    if (e1.getValue().contains(intName)) {
                        classes.get(e1.getKey()).add(loadClass(e2.getKey(), loader));
                        break;
                    }
                }
            }
        }
    }
    for (Map.Entry<Class<? extends Annotation>, Collection<String>> e : matchingInterfaces.entrySet()) {
        if (classes.get(e.getKey()).isEmpty()) {
            for (String intName : e.getValue()) {
                classes.get(e.getKey()).add(loadClass(intName, loader));
            }
        }
    }
    return classes;
}
Also used : PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) MetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory) HashMap(java.util.HashMap) Resource(org.springframework.core.io.Resource) MetadataReader(org.springframework.core.type.classreading.MetadataReader) CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) Annotation(java.lang.annotation.Annotation) AnnotationMetadata(org.springframework.core.type.AnnotationMetadata) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map)

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