Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get annotations of interface or abstract class methods in Java

Tags:

java

I have an interface like this:

public interface IFoo{
@AnnotationTest(param="test")
String invoke();
}

and I implement this like this:

public class Foo implements IFoo{
@Override
public String invoke(){
  Method method = new Object() {
        }.getClass().getEnclosingMethod();
  AnnotationTest ann = method.getAnnotation(AnnotationTest.class);
  if(ann == null){
    System.out.printl("Parent method's annotation is unreachable...")
}

}

}

If it is possible to reach parent's annotation, I want to learn the way of it.

Any help or idea will be appreciated.

like image 549
ibrahimyilmaz Avatar asked Jun 02 '14 12:06

ibrahimyilmaz


People also ask

Can we annotate interface in Java?

Annotation is defined like a ordinary Java interface, but with an '@' preceding the interface keyword (i.e., @interface ). You can declare methods inside an annotation definition (just like declaring abstract method inside an interface). These methods are called elements instead.

Are annotations inherited from interface?

The annotation can be overridden in case the child class has the annotation. Because there is no multiple inheritance in Java, annotations on interfaces cannot be inherited.

How do you check if a class has an annotation?

The isAnnotation() method is used to check whether a class object is an annotation. The isAnnotation() method has no parameters and returns a boolean value. If the return value is true , then the class object is an annotation. If the return value is false , then the class object is not an annotation.

How do you calculate implementation annotation?

The only way to look for ALL implementations which hold the specific interface is to call on Person i.e. Person. class. getAnnotations() . This unfortunately only yields the annotations which are declared on the Person interface.


2 Answers

There is no direct way to get it. If you really need, you have to manually loop over getInterfaces() to find if any implemented interface has the annotation. If you want to search for (eventually abstract) superclasses and the annotation is not @Inherited, you can again iterate the superclass chain until finding Object (*).

But beware, as following post states, there are good reasons for this not to be directly implemented in Java : Why java classes do not inherit annotations from implemented interfaces?

(*) If the annotation is @Inherited it is automatically searched on superclasses, but not on interfaces.

like image 154
Serge Ballesta Avatar answered Sep 28 '22 05:09

Serge Ballesta


You can use Spring AnnotationUtils.findAnnotation to read annotations from interfaces.

Example :

Interface I.java

public interface I {
    @SomeAnnotation
    void theMethod();
}

Implementing class A.java

public class A implements I {
    public void theMethod() {
        Method method = new Object() {}.getClass().getEnclosingMethod();
        SomeAnnotation ann = AnnotationUtils.findAnnotation(method, AnnotationTest.class);
    }
}

It obviously requires to include in your project (and import) Spring framework classes.

like image 21
superbob Avatar answered Sep 28 '22 05:09

superbob