@@ -17,8 +17,11 @@ package org.springframework.data.util;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
@@ -28,6 +31,7 @@ import org.springframework.context.annotation.ClassPathScanningCandidateComponen
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.core.type.filter.AnnotationTypeFilter;
|
||||
import org.springframework.core.type.filter.TypeFilter;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
@@ -46,6 +50,10 @@ public class AnnotatedTypeScanner implements ResourceLoaderAware, EnvironmentAwa
|
||||
private @Nullable ResourceLoader resourceLoader;
|
||||
private @Nullable Environment environment;
|
||||
|
||||
private Consumer<ClassNotFoundException> classNotFoundAction = ex -> {
|
||||
throw new IllegalStateException(ex);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new {@link AnnotatedTypeScanner} for the given annotation types.
|
||||
*
|
||||
@@ -64,9 +72,18 @@ public class AnnotatedTypeScanner implements ResourceLoaderAware, EnvironmentAwa
|
||||
*/
|
||||
@SafeVarargs
|
||||
public AnnotatedTypeScanner(boolean considerInterfaces, Class<? extends Annotation>... annotationTypes) {
|
||||
this(considerInterfaces, Arrays.asList(annotationTypes));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param considerInterfaces
|
||||
* @param annotationTypes
|
||||
* @since 3.0
|
||||
*/
|
||||
public AnnotatedTypeScanner(boolean considerInterfaces, Collection<Class<? extends Annotation>> annotationTypes) {
|
||||
|
||||
this.annotationTypess = Arrays.asList(annotationTypes);
|
||||
this.considerInterfaces = considerInterfaces;
|
||||
this.annotationTypess = annotationTypes;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -79,11 +96,15 @@ public class AnnotatedTypeScanner implements ResourceLoaderAware, EnvironmentAwa
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
void setClassNotFoundAction(Consumer<ClassNotFoundException> classNotFoundAction) {
|
||||
this.classNotFoundAction = classNotFoundAction;
|
||||
}
|
||||
|
||||
public Set<Class<?>> findTypes(String... basePackages) {
|
||||
return findTypes(Arrays.asList(basePackages));
|
||||
}
|
||||
|
||||
public Set<Class<?>> findTypes(Iterable<String> basePackages) {
|
||||
Set<Class<?>> findTypes(Iterable<String> basePackages, Collection<TypeFilter> filters) {
|
||||
|
||||
ClassPathScanningCandidateComponentProvider provider = new InterfaceAwareScanner(considerInterfaces);
|
||||
|
||||
@@ -95,9 +116,7 @@ public class AnnotatedTypeScanner implements ResourceLoaderAware, EnvironmentAwa
|
||||
provider.setEnvironment(environment);
|
||||
}
|
||||
|
||||
for (Class<? extends Annotation> annotationType : annotationTypess) {
|
||||
provider.addIncludeFilter(new AnnotationTypeFilter(annotationType, true, considerInterfaces));
|
||||
}
|
||||
filters.forEach(provider::addIncludeFilter);
|
||||
|
||||
Set<Class<?>> types = new HashSet<>();
|
||||
|
||||
@@ -112,13 +131,13 @@ public class AnnotatedTypeScanner implements ResourceLoaderAware, EnvironmentAwa
|
||||
|
||||
if (beanClassName == null) {
|
||||
throw new IllegalStateException(
|
||||
String.format("Unable to obtain bean class name from bean definition %s", definition));
|
||||
String.format("Unable to obtain bean class name from bean definition %s!", definition));
|
||||
}
|
||||
|
||||
try {
|
||||
types.add(ClassUtils.forName(beanClassName, classLoader));
|
||||
} catch (ClassNotFoundException o_O) {
|
||||
throw new IllegalStateException(o_O);
|
||||
classNotFoundAction.accept(o_O);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -126,6 +145,10 @@ public class AnnotatedTypeScanner implements ResourceLoaderAware, EnvironmentAwa
|
||||
return types;
|
||||
}
|
||||
|
||||
public Set<Class<?>> findTypes(Iterable<String> basePackages) {
|
||||
return findTypes(basePackages, Streamable.of(annotationTypess).stream().map(annotation -> new AnnotationTypeFilter(annotation, true, considerInterfaces)).collect(Collectors.toSet()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom extension of {@link ClassPathScanningCandidateComponentProvider} to make sure interfaces to not get dropped
|
||||
* from scanning results.
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Copyright 2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.util;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.StandardEnvironment;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.core.type.filter.AnnotationTypeFilter;
|
||||
import org.springframework.core.type.filter.TypeFilter;
|
||||
|
||||
/**
|
||||
* A caching {@link TypeScanner} implementation delegating to {@link AnnotatedTypeScanner}.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @since 3.0
|
||||
*/
|
||||
class DelegatingTypeScanner implements TypeScanner {
|
||||
|
||||
private final ResourceLoader resourceLoader;
|
||||
private final Environment environment;
|
||||
|
||||
private Collection<String> packageNames;
|
||||
private Collection<TypeFilter> includeFilters;
|
||||
|
||||
private Consumer<ClassNotFoundException> classNotFoundAction;
|
||||
|
||||
private Lazy<Set<Class<?>>> scanResult = Lazy.of(this::collect);
|
||||
|
||||
DelegatingTypeScanner(ResourceLoader resourceLoader) {
|
||||
this(new StandardEnvironment(), resourceLoader);
|
||||
}
|
||||
|
||||
DelegatingTypeScanner(Environment environment, ResourceLoader resourceLoader) {
|
||||
|
||||
this(environment, resourceLoader, Collections.emptyList(),
|
||||
Collections.singleton((metadataReader, readerFactory) -> true), error -> {});
|
||||
}
|
||||
|
||||
DelegatingTypeScanner(Environment environment, ResourceLoader resourceLoader, Collection<String> packageNames,
|
||||
Collection<TypeFilter> includeFilters, Consumer<ClassNotFoundException> classNotFoundAction) {
|
||||
|
||||
this.environment = environment;
|
||||
this.resourceLoader = resourceLoader;
|
||||
this.packageNames = new ArrayList<>(packageNames);
|
||||
this.includeFilters = new ArrayList<>(includeFilters);
|
||||
this.classNotFoundAction = classNotFoundAction;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeScanner scanPackages(Collection<String> packageNames) {
|
||||
return new DelegatingTypeScanner(environment, resourceLoader, packageNames, includeFilters, classNotFoundAction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeScanner forTypesAnnotatedWith(Collection<Class<? extends Annotation>> annotations) {
|
||||
|
||||
return new DelegatingTypeScanner(environment, resourceLoader, packageNames,
|
||||
annotations.stream().map(DelegatingTypeScanner::annotationFilter).collect(Collectors.toSet()),
|
||||
classNotFoundAction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeScanner onClassNotFound(Consumer<ClassNotFoundException> action) {
|
||||
return new DelegatingTypeScanner(environment, resourceLoader, packageNames, includeFilters, action);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Class<?>> collectAsSet() {
|
||||
return scanResult.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEach(Consumer<Class<?>> action) {
|
||||
collectAsSet().forEach(action);
|
||||
}
|
||||
|
||||
private Set<Class<?>> collect() {
|
||||
|
||||
AnnotatedTypeScanner annotatedTypeScanner = new AnnotatedTypeScanner(false, Collections.emptyList());
|
||||
annotatedTypeScanner.setResourceLoader(resourceLoader);
|
||||
annotatedTypeScanner.setEnvironment(environment);
|
||||
if (classNotFoundAction != null) {
|
||||
annotatedTypeScanner.setClassNotFoundAction(classNotFoundAction);
|
||||
}
|
||||
return annotatedTypeScanner.findTypes(packageNames, includeFilters);
|
||||
}
|
||||
|
||||
private static AnnotationTypeFilter annotationFilter(Class<? extends Annotation> annotation) {
|
||||
return new AnnotationTypeFilter(annotation, true, false);
|
||||
}
|
||||
}
|
||||
139
src/main/java/org/springframework/data/util/TypeScanner.java
Normal file
139
src/main/java/org/springframework/data/util/TypeScanner.java
Normal file
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* Copyright 2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.util;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A scanner that searches the classpath for matching types within given target packages.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @since 3.0
|
||||
*/
|
||||
public interface TypeScanner {
|
||||
|
||||
/**
|
||||
* Create a new {@link TypeScanner} using the given {@link ClassLoader}.
|
||||
*
|
||||
* @param classLoader must not be {@literal null}.
|
||||
* @return new instance of {@link TypeScanner}.
|
||||
*/
|
||||
static TypeScanner typeScanner(ClassLoader classLoader) {
|
||||
|
||||
Assert.notNull(classLoader, "ClassLoader must not be null!");
|
||||
return typeScanner(new DefaultResourceLoader(classLoader));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link TypeScanner} using the given {@link ResourceLoader}.
|
||||
*
|
||||
* @param resourceLoader must not be {@literal null}.
|
||||
* @return new instance of {@link TypeScanner}.
|
||||
*/
|
||||
static TypeScanner typeScanner(ResourceLoader resourceLoader) {
|
||||
|
||||
Assert.notNull(resourceLoader, "ResourceLoader must not be null!");
|
||||
return new DelegatingTypeScanner(resourceLoader);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link TypeScanner} using the given {@link ApplicationContext}.
|
||||
*
|
||||
* @param context must not be {@literal null}.
|
||||
* @return new instance of {@link TypeScanner}.
|
||||
*/
|
||||
static TypeScanner typeScanner(ApplicationContext context) {
|
||||
|
||||
Assert.notNull(context, "Context must not be null!");
|
||||
return new DelegatingTypeScanner(context.getEnvironment(), context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects the {@link String names} of packages to scan.
|
||||
*
|
||||
* @param packageNames array of {@link String package names} to scan; Must not be {@literal null}.
|
||||
* @return new instance of {@link TypeScanner}.
|
||||
* @see #scanPackages(Collection)
|
||||
*/
|
||||
default TypeScanner scanPackages(String... packageNames) {
|
||||
return scanPackages(Arrays.asList(packageNames));
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects the {@link String names} of packages to scan.
|
||||
*
|
||||
* @param packageNames {@link Collection} of {@link String package names} to scan.
|
||||
* @return new instance of {@link TypeScanner}.
|
||||
* @see #scanPackages(String...)
|
||||
*/
|
||||
TypeScanner scanPackages(Collection<String> packageNames);
|
||||
|
||||
/**
|
||||
* Define annotations identifying types to include in the scan result.
|
||||
*
|
||||
* @param annotations must not be {@literal null}.
|
||||
* @return new instance of {@link TypeScanner}.
|
||||
* @see #forTypesAnnotatedWith(Collection)
|
||||
*/
|
||||
default TypeScanner forTypesAnnotatedWith(Class<? extends Annotation>... annotations) {
|
||||
return forTypesAnnotatedWith(Arrays.asList(annotations));
|
||||
}
|
||||
|
||||
/**
|
||||
* Define annotations identifying types to include in the scan result.
|
||||
*
|
||||
* @param annotations must not be {@literal null}.
|
||||
* @return new instance of {@link TypeScanner}.
|
||||
*/
|
||||
TypeScanner forTypesAnnotatedWith(Collection<Class<? extends Annotation>> annotations);
|
||||
|
||||
/**
|
||||
* Define what happens in the case of a {@link ClassNotFoundException}.
|
||||
*
|
||||
* @param action must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
TypeScanner onClassNotFound(Consumer<ClassNotFoundException> action);
|
||||
|
||||
/**
|
||||
* Obtain the scan result.
|
||||
*
|
||||
* @return never {@literal null}.
|
||||
*/
|
||||
default Set<Class<?>> collectAsSet() {
|
||||
|
||||
LinkedHashSet<Class<?>> result = new LinkedHashSet<>();
|
||||
forEach(result::add);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the given action for each element found while scanning.
|
||||
*
|
||||
* @param action must not be {@literal null}.
|
||||
*/
|
||||
void forEach(Consumer<Class<?>> action);
|
||||
}
|
||||
Reference in New Issue
Block a user