As @assylias's link says, the annotations can't be inherited, but you can use the composition, and search recursively your target annotation like this:
public static class AnnotationUtil {
private static <T extends Annotation> boolean containsAnnotation(Class<? extends Annotation> annotation, Class<T> annotationTypeTarget, Set<Class<? extends Annotation>> revised) {
boolean result = !revised.contains(annotation);
if (result && annotationTypeTarget != annotation) {
Set<Class<? extends Annotation>> nextRevised = new HashSet<>(revised);
nextRevised.add(annotation);
result = Arrays.stream(annotation.getAnnotations()).anyMatch(a -> containsAnnotation(a.annotationType(), annotationTypeTarget, nextRevised));
}
return result;
}
public static <T extends Annotation> boolean containsAnnotation(Class<? extends Annotation> annotation, Class<T> annotationTypeTarget) {
return containsAnnotation(annotation, annotationTypeTarget, Collections.emptySet());
}
public static <T extends Annotation> Map<Class<? extends Annotation>, ? extends Annotation> getAnnotations(Method method, Class<T> annotationTypeTarget) {
return Arrays.stream(method.getAnnotations()).filter(a -> containsAnnotation(a.annotationType(), annotationTypeTarget)).collect(Collectors.toMap(a -> a.annotationType(), Function.identity()));
}
}
If you have:
@Retention(RetentionPolicy.RUNTIME)
@interface Action {
}
@Action
@Retention(RetentionPolicy.RUNTIME)
@interface SpecificAction {
}
@Action
@Retention(RetentionPolicy.RUNTIME)
@interface ParticularAction {
}
public class Foo{
@SpecificAction
@ParticularAction
public void specificMethod() {
// ...
}
}
You can use like this: AnnotationUtil.getAnnotations(specificMethod, Action.class);
And this'll return a Map: {interface foo.ParticularAction=@foo.ParticularAction(), interface foo.SpecificAction=@foo.SpecificAction()}