0

I have a web application built with Spring and running inside Jboss. I am trying, at runtime, to scan for classes that have a certain annotation. Right now I am using the open source Reflections library

public static List<Class> scanForClassesWithAnnotation(Class<? extends Annotation> annotationClass)
{
    List<Class> classesWithAnnotation = new ArrayList<>();
    for(Package p : Package.getPackages())
    {
        LOGGER.info("scanForClassesWithAnnotation: Examining package " + p.getName());

        try
        {
            List<ClassLoader> classLoadersList = new ArrayList<>();
            classLoadersList.add(ClasspathHelper.contextClassLoader());
            classLoadersList.add(ClasspathHelper.staticClassLoader());

            Reflections reflections = new Reflections(
                new ConfigurationBuilder()
                    .setScanners(new SubTypesScanner(false), new ResourcesScanner())
                    .setUrls(ClasspathHelper.forClassLoader(classLoadersList.toArray(new ClassLoader[classLoadersList.size()])))
                    .filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix(p.getName()))));

            Set<Class<?>> classes = reflections.getSubTypesOf(Object.class);

            LOGGER.info("\tscanForClassesWithAnnotation: found classes " + JsonUtils.objectToJson(classes));

            for(Class foundClass : classes)
            {
                if(foundClass.isAnnotationPresent(annotationClass))
                {
                    classesWithAnnotation.add(foundClass);
                }
            }
        } catch (Exception ignored) {}
    }
    return classesWithAnnotation;
}

When this code executes I see many of these such messages in the log:

given scan urls are empty. set urls in the configuration

This code works in a standalone Maven project, so I know it works in general. But I think the problem is with the fact that the code is running in a servlet context. How do I fix this? How do I scan for classes with an annotation in a Spring webapp? Thanks in advance.

David Williams
  • 8,388
  • 23
  • 83
  • 171

2 Answers2

0

Start playing with

public class Scanner {

    private static final Log log = LogFactory.getLog(Scanner.class);

    public static void main(String[] args) throws IOException {
        SimpleMetadataReaderFactory metadataFactory = new SimpleMetadataReaderFactory();
        ResourcePatternResolver scaner = new PathMatchingResourcePatternResolver();
        Resource[] resources = scaner.getResources("classpath*:/base.package/**/*.class");
        for (Resource r : resources) {
            log.info("Scanning [" + r.getDescription() + "]");
            MetadataReader metadataReader = metadataFactory.getMetadataReader(r);
            if (metadataReader.getAnnotationMetadata().isAnnotated("org.example.Annotation"))
                log.info("found annotation in [" + r.getDescription() + "]");

        }
    }
Jose Luis Martin
  • 10,459
  • 1
  • 37
  • 38
0

This works

    public static List<Class> scanForClassesWithAnnotation2(Class<? extends Annotation> annotationClass)
{
    List<Class> classesWithAnnotation = new ArrayList<>();

    ClassPathScanningCandidateComponentProvider scanner =
        new ClassPathScanningCandidateComponentProvider(false);

    scanner.addIncludeFilter(new AnnotationTypeFilter(annotationClass));

    for (BeanDefinition bd : scanner.findCandidateComponents("com.example"))
    {
        try
        {
            classesWithAnnotation.add(Class.forName(bd.getBeanClassName()));
        } catch (ClassNotFoundException e)
        {
            LOGGER.error("Could not create class from class name");
        }
    }

    return classesWithAnnotation;
}
David Williams
  • 8,388
  • 23
  • 83
  • 171