How to scan classes for annotations?

49,678

Solution 1

You can use the Reflections library by giving it the package and Annotation you are looking for.

Reflections reflections = new Reflections("my.project.prefix");
Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(Controller.class);

for (Class<?> controller : annotated) {
    RequestMapping request = controller.getAnnotation(RequestMapping.class);
    String mapping = request.name();
}

Of course placing all your servlets in the same package makes this a little easier. Also you might want to look for the classes that have the RequestMapping annotation instead, since that is the one you are looking to get a value from.

Solution 2

Scanning for annotations is very difficult. You actually have to process all classpath locations and try to find files that correspond to Java classes (*.class).

I strongly suggest to use a framework that provides such functionality. You could for example have a look at Scannotation.

Solution 3

If you are using Spring,

It has something called a AnnotatedTypeScanner class.
This class internally uses

ClassPathScanningCandidateComponentProvider

This class has the code for actual scanning of the classpath resources. It does this by using the class metadata available at runtime.

One can simply extend this class or use the same class for scanning. Below is the constructor definition.

   /**
     * Creates a new {@link AnnotatedTypeScanner} for the given annotation types.
     * 
     * @param considerInterfaces whether to consider interfaces as well.
     * @param annotationTypes the annotations to scan for.
     */
    public AnnotatedTypeScanner(boolean considerInterfaces, Class<? extends Annotation>... annotationTypes) {

        this.annotationTypess = Arrays.asList(annotationTypes);
        this.considerInterfaces = considerInterfaces;
    }

Solution 4

Try corn-cps

List<Class<?>> classes = CPScanner.scanClasses(new PackageNameFilter("net.sf.corn.cps.*"),new ClassFilter().appendAnnotation(Controller.class));
for(Class<?> clazz: classes){
   if(clazz.isAnnotationPresent(RequestMapping.class){
     //This is what you want
   }
}

Maven module dependency :

<dependency>
    <groupId>net.sf.corn</groupId>
    <artifactId>corn-cps</artifactId>
    <version>1.0.1</version>
</dependency>

visit the site https://sites.google.com/site/javacornproject/corn-cps for more information

Share:
49,678
loyalflow
Author by

loyalflow

it is all about you!

Updated on September 18, 2020

Comments

  • loyalflow
    loyalflow almost 4 years

    I have a plain jane servlets web application, and some of my classes have the following annotations:

    @Controller
    @RequestMapping(name = "/blog/")
    public class TestController {
    ..
    
    }
    

    Now when my servlet applications starts up, I would like to get a list of all classes that have the @Controller annotation, and then get the value of the @RequestMapping annotation and insert it in a dictionary.

    How can I do this?

    I'm using Guice and Guava also, but not sure if that has any annotation related helpers.

  • loyalflow
    loyalflow over 11 years
    I can specify the package, that's not a problem or limitation.
  • maaartinus
    maaartinus over 11 years
    Very difficult??? I'd say it's very simple when you know you CLASSPATH (what you normally do). But I agree that using a good tool is a good idea.
  • chkal
    chkal over 11 years
    It is very difficult if you want to do it efficiently. Especially if you don't want to create Class instances for all classes on the classpath.
  • chkal
    chkal over 11 years
    The question was how to "get a list of all classes that have the @Controller annotation". Checking individual classes for the annotation is a completely different thing.