Skip to content

Commit

Permalink
Add annotation inspection method
Browse files Browse the repository at this point in the history
  • Loading branch information
rrighi committed Nov 20, 2018
1 parent 312df14 commit bb38bbd
Showing 1 changed file with 26 additions and 0 deletions.
Expand Up @@ -120,6 +120,32 @@ public static Class<?> getClassWithAnnotation(Class<?> cls, Class<? extends Anno
return null;
}

/**
* Get whether the class is annotated with given <code>annotation</code>, checking any superclass and implemented
* interface.
* @param cls Class to inspect (not null)
* @param annotation Annotation to look for (not null)
* @return whether the class is annotated with given <code>annotation</code>
*/
public static boolean hasAnnotation(Class<?> cls, Class<? extends Annotation> annotation) {
ObjectUtils.argumentNotNull(cls, "Class must be not null");
ObjectUtils.argumentNotNull(annotation, "Annotation must be not null");

if (cls.isAnnotationPresent(annotation)) {
return true;
}
for (Class<?> intf : cls.getInterfaces()) {
if (intf.isAnnotationPresent(annotation)) {
return true;
}
}
Class<?> superClass = cls.getSuperclass();
if (superClass != null && superClass != Object.class) {
return hasAnnotation(superClass, annotation);
}
return false;
}

/**
* Get a single annotation of given <code>annotationType</code> present in given <code>element</code>, including any
* meta-annotation and supporting repeatable annotations.
Expand Down

0 comments on commit bb38bbd

Please sign in to comment.