Java Utililty Methods Reflection Annotation

List of utility methods to do Reflection Annotation

Description

The list of methods to do Reflection Annotation are organized into topic(s).

Method

TgetAnnotation(final AnnotatedElement anElement, final Class annotationClass)
get Annotation
return anElement != null ? (T) anElement.getAnnotation(annotationClass) : null;
TgetAnnotation(final Method method, final Class annotationClass)
get Annotation
final T annotation = method.getAnnotation(annotationClass);
if (annotation != null) {
    return annotation;
final Class<?> declaringClass = method.getDeclaringClass();
Class<?> currentSuperClass = declaringClass.getSuperclass();
while (currentSuperClass != null) {
    try {
...
TgetAnnotation(@Nonnull Annotation[] annotations, @Nonnull Class annotation)
Retrieve the annotation of given type from array of annotations
if (annotations == null || annotations.length == 0) {
    return null;
for (Annotation a : annotations) {
    if (annotation.isAssignableFrom(a.getClass())) {
        return (T) a;
return null;
AgetAnnotation(@Nonnull Class cls, @Nonnull Class annotation)
get Annotation
return annotation.cast(cls.getAnnotation(annotation));
TgetAnnotation(AnnotatedElement ae, Class annotationType)
Get a single Annotation of annotationType from the supplied Method, Constructor or Field.
T ann = ae.getAnnotation(annotationType);
if (ann == null) {
    for (Annotation metaAnn : ae.getAnnotations()) {
        ann = metaAnn.annotationType().getAnnotation(annotationType);
        if (ann != null) {
            break;
return ann;
ObjectgetAnnotation(AnnotatedElement aobj, Class aClass)
Get annotation of an object via reflection
for (Object a : aobj.getAnnotations()) {
    if (isAnnotationInstance(aClass, a))
        return a;
return null;
AnnotationgetAnnotation(AnnotatedElement element, Class annotation)
get Annotation
return element.getAnnotation(annotation);
AnnotationgetAnnotation(AnnotatedElement element, String annotationTypeName)
get Annotation
Class<?> annotationType = null; 
try {
    annotationType = Class.forName(annotationTypeName);
} catch (Exception ex) {
    throw new IllegalArgumentException(ex);
return element.getAnnotation(annotationType.asSubclass(Annotation.class));
AnnotationgetAnnotation(AnnotatedElement target, String annotationType)
get Annotation
for (Annotation annotation : target.getAnnotations()) {
    if (annotationType.equals(annotation.annotationType().getName())) {
        return annotation;
return null;
TgetAnnotation(Annotation ann, Class annotationType)
get Annotation
if (annotationType.isInstance(ann)) {
    return (T) ann;
return ann.annotationType().getAnnotation(annotationType);