0

There is an annotation @MarkerAnnotation. It can be added directly to method. or to any other annotation, like

@MarkerAnnotation
@Interface CustomAnnotation {...}

and this @CustomAnnotation can also be added directly to methods. That's the standard way many frameworks let users add their own annotation (e.g. spring).

Now, given a class, I want to find all methods marked directly or indirectly with @MarkerAnnotation. For every method I also want to find the associated @MarkerAnnotation and/or @CustomAnnotation. Is there any tool that I can use or I have to do it manually?

3 Answers 3

2

Spring has an AnnotationUtils class in their core project that does what you want. The javadoc for findAnnotation:

Find a single Annotation of annotationType on the supplied Method, traversing its super methods (i.e., from superclasses and interfaces) if the annotation is not directly present on the given method itself. Correctly handles bridge Methods generated by the compiler.

Meta-annotations will be searched if the annotation is not directly present on the method.

Annotations on methods are not inherited by default, so we need to handle this explicitly.

Sign up to request clarification or add additional context in comments.

Comments

0

For a generic approach, you should use the annotation processor facility of javac, which requires you to write an annotation processor which uses the APIs in packages javax.annotation.processing, and javax.lang.model and its subpackages. This code will run inside javac. (There is an older tool, apt, which is separate from javac, but apt is deprecated in Java 7.)

In particular, when you visit each Element, you can call getAnnotationMirrors on it. Then, for each of those annotations, call getAnnotationType().asElement() to get the Element for the annotation type. You may need to use recursion to find the indirect annotations, if there is more than one level of indirection possible.

Comments

0

for example like this...

public class Test{
         @MarkerAnnotation
         public void TestMethod1(){

         }

         @CustomAnnotation
         @MarkerAnnotation
         public void TestMethod2(){

         }

    }

you can parse like this..

public class AnnotationTest{

         public static void main(String[] args){
              Method[] methods = Test.class.getDeclaredMethods();
              for(Method method: methods){
                   Annotation[] annotations = method.getAnnotations();
               for(Annotation annotation: annotations){
                   if(annotation instanceof MarkerAnnotation)
                           System.out.println(method.getName() +" annotated with MarkerAnnotation");
                      if(annotation instanceof CustomAnnotation)
                           System.out.println(method.getName() +" annotated with CustomAnnotation");
                   }
              }
         }
    }

and if you want check CustomAnnotation have MarkerAnnotation, then do like this..

    if(CustomAnnotation.class.isAnnotation()){
        Annotation[] annotations = CustomAnnotation.class.getAnnotations();
        if(annotations[0] instanceof MarkerAnnotation){
            System.out.println("CustomAnnotation have MarkerAnnotation");
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.