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:
- The custom annotation is in a jar file.
- The interface that is annotated is in current project.
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.