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;
}
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;
}
Aggregations