2

I have an annotation defined as below:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CustomAnnotation {
    String message();
}

I am using that on an interface (not it's methods) as below:

@CustomAnnotation (message = "Testing")
public interface SomeInterface {
}

How do I get hold of the annotation on the above interface? Calling clazz.getAnnotations(), clazz.getDeclaredAnnotations() and AnnotationUtils.findAnnotation() from Spring framework, but none of them pickups the annotation. All above methods returns null.

Is it really possible to get annotations from an interface or not?

EDIT 1:

  1. The custom annotation is in a jar file.
  2. The interface that is annotated is in current project.
  3. Below is the code that scans for all the interfaces that is annotated with custom annotation:

    Reflections reflections = new Reflections("bacePackage");

    Set<Class<? extends BaseInterface>> classes = reflections.getSubTypesOf(BaseInterface.class);
    for (Class<? extends BaseInterface> aClass : classes) {
        for (final Annotation annotation : aClass.getDeclaredAnnotations()) {
            if(annotation.annotationType() == CustomAnnotation.class) {
                CustomAnnotation customAnnotation = (CustomAnnotation) annotation;
                System.out.println(customAnnotation.message());
            }
        }
    }
    

If the code that scans for the annotation is in current project, then it works correctly. But if that code is in the jar file, then it doesn't read the annotation.

Mubin
  • 4,192
  • 3
  • 26
  • 45
  • As far as I remember you have to get `beanDefinition` and only after this call methods for retrieving annotations. – Vladyslav Nikolaiev Jan 10 '18 at 14:45
  • look [here](https://stackoverflow.com/questions/14236424/how-can-i-find-all-beans-with-the-custom-annotation-foo) – Vladyslav Nikolaiev Jan 10 '18 at 14:45
  • All of this methods should work, unless the annotation is somehow removed from the interface in runtime but that is only possible with some code instrumentation. – kaos Jan 10 '18 at 14:48
  • Can You show more code? It might be that Spring returns some kind of proxy around Your interface that is missing this annotation. Then You can try with clazz.getSuperClass().getAnnotation(CustomAnnotation .clazz). – kaos Jan 10 '18 at 14:51
  • `SomeInterface.class.getAnnotations()[0].getAnnotationType()` will give `CustomAnnotation` – pvpkiran Jan 10 '18 at 14:55
  • What is `clazz`? The class of the object implementing the interface? If yes, you're looking at the wrong place ;-) – Lothar Jan 10 '18 at 14:59
  • It works fine for me: `SomeInterface.class.getAnnotations()` and `SomeInterface.class.getDeclaredAnnotations()` return an array with 1 element (a `$Proxy1` object). And `AnnotationUtils.findAnnotation(SomeInterface.class, CustomAnnotation.class)` returns non-null (a `$Proxy1` object). – Thomas Fritsch Jan 10 '18 at 15:00
  • You should get the annotation from `SomeInterface.class` directly, not from any subtype. – Dean Xu Jan 11 '18 at 00:29

2 Answers2

0

Both methods should work. My guess is that Spring is wrapping this in a proxy which is missing the annotation. You can try to discover the annotation on super class e.g. clazz.getSuperClass().getAnnotation(...). Or try putting @Inherited on Your custom annotation.

kaos
  • 1,598
  • 11
  • 15
  • There is no super class involved. The annotation is placed on an interface, and that interface doesn't have any implementations. – Mubin Jan 10 '18 at 15:27
0

SomeInterface.class.getAnnotations()[0].getAnnotationType().getName()
will give you fully qualified name.

I have used [0] since I know there is only one Annotation. You might have to change it or traverse all the annotations if there are many

pvpkiran
  • 25,582
  • 8
  • 87
  • 134