10

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.

ibrahimyilmaz
  • 18,331
  • 13
  • 61
  • 80
  • Have you tried [java.lang.annotation.Inherited](http://docs.oracle.com/javase/7/docs/api/java/lang/annotation/Inherited.html) ? – superbob Jun 02 '14 at 12:18
  • @superbob doesn;t work for interfaces – NimChimpsky Jun 02 '14 at 12:19
  • Take a look at http://stackoverflow.com/questions/4745798/why-java-classes-do-not-inherit-annotations-from-implemented-interfaces and http://stackoverflow.com/questions/12428858/annotation-is-not-inherited-from-interface-method – superbob Jun 02 '14 at 12:22

3 Answers3

15

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.

superbob
  • 1,628
  • 13
  • 24
  • 1
    @ibrahimyilmaz, read the Javadoc for AnnotationUtils.findAnnotation. – alexvinall Jun 02 '14 at 14:24
  • Example added @ibrahimyilmaz, also consider getting the method as described [here](http://stackoverflow.com/questions/5139984/java-getting-the-currently-executing-method-corresponding-object#19773921), it seems cleaner to me. – superbob Jun 02 '14 at 15:26
  • I know how to use method:) I cant import spring core library into my android project. I added this my gradle conf. 'org.springframework:spring-core:4.0.5.RELEASE' – ibrahimyilmaz Jun 03 '14 at 06:31
  • @ibrahimyilmaz, try to fix your gradle issue, if you can't ask the an appropriate question. Then see if your initial question is solved. – superbob Jun 04 '14 at 09:41
3

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.

Community
  • 1
  • 1
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
2

you can't inherit annotations.

But a framework that uses an annotation can check to see if annotation is present on superclass

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311