Skip to content

Instantly share code, notes, and snippets.

@jrichardsz
Forked from skempken/Detector.java
Last active November 28, 2023 15:26
Show Gist options
  • Save jrichardsz/a34480c1bcc31c45da730c48c4f41331 to your computer and use it in GitHub Desktop.
Save jrichardsz/a34480c1bcc31c45da730c48c4f41331 to your computer and use it in GitHub Desktop.
Find all annotated classes in a package using Spring
import java.lang.annotation.Annotation;
import java.util.LinkedList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.web.context.support.StandardServletEnvironment;
public class ClassScanner {
private static final Logger logger = LoggerFactory.getLogger(ClassScanner.class);
public static Class<?>[] findAllAnnotatedClassesInPackage(String packageName,
Class<? extends Annotation> clazz) {
final List<Class<?>> result = new LinkedList<Class<?>>();
final ClassPathScanningCandidateComponentProvider provider =
new ClassPathScanningCandidateComponentProvider(true, new StandardServletEnvironment());
provider.addIncludeFilter(new AnnotationTypeFilter(clazz));
for (BeanDefinition beanDefinition : provider.findCandidateComponents(packageName)) {
try {
result.add(Class.forName(beanDefinition.getBeanClassName()));
} catch (ClassNotFoundException e) {
logger.warn("Could not resolve class object for bean definition", e);
}
}
return result.toArray(new Class<?>[result.size()]);
}
}
Class<?>[] classes = ClassScanner
.findAllAnnotatedClassesInPackage("org.acme.model.entities", Entity.class);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment