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

AgetAnnotation(final Class annotatedClass, final Class annotationClass)
get Annotation
return annotatedClass.getAnnotation(annotationClass);
TgetAnnotation(final Class type, final Class annotation)
get Annotation
T res = null;
if (!Modifier.isAbstract(type.getModifiers())) {
    Class<?> supertype = type;
    while (supertype != null && Object.class != supertype) {
        if (supertype.isAnnotationPresent(annotation)) {
            res = supertype.getAnnotation(annotation);
            break;
        supertype = supertype.getSuperclass();
return res;
ANNgetAnnotation(final Class reference, final AccessibleObject obj)
get Annotation
final Annotation ann = obj.getAnnotation(reference);
return ann == null ? null : (ANN) ann;
ANNOTATIONgetAnnotation(final Class annotationClass, final AnnotatedElement... elements)
Returns the first element which has an annotation of the given type declared.
for (final AnnotatedElement element : elements) {
    final ANNOTATION annotation = element.getAnnotation(annotationClass);
    if (annotation != null) {
        return annotation;
return null;
AgetAnnotation(final Member member, final Class annotation)
Returns the annotation instance if the member has the annotation.
if (member instanceof Field) {
    return ((Field) member).getAnnotation(annotation);
if (member instanceof Method) {
    return ((Method) member).getAnnotation(annotation);
throw new IllegalArgumentException("Member is neither field nor method: " + member);
TgetAnnotation(final Method method, final Class annotationClass)
get Annotation
T annotation = method.getAnnotation(annotationClass);
if (annotation != null)
    return annotation;
final Class<?> declaringClass = method.getDeclaringClass();
Class<?> superClass = declaringClass.getSuperclass();
while (superClass != null) {
    try {
        final Method superClassMethod = superClass.getDeclaredMethod(method.getName(),
...
TgetAnnotation(final Object obj, final Class annoType)
get Annotation
final Class<?> type = obj instanceof Class<?> ? (Class<?>) obj : obj.getClass();
return type.getAnnotation(annoType);
AgetAnnotation(final Object object, final Class annotationClass)
Get the first annotation of the declared type from this object.
if (object == null)
    throw new IllegalArgumentException("object cannot be null in getAnnotation()");
if (annotationClass == null)
    throw new IllegalArgumentException("annotationClass cannot be null in getAnnotation()");
final Class<? extends Object> clazz = object.getClass();
A result = null;
for (Class<?> c = clazz; c != null; c = c.getSuperclass()) {
    result = c.getAnnotation(annotationClass);
...
TgetAnnotation(Member m, Class annotationClass)
get Annotation
if (m == null || annotationClass == null)
    return null;
if (m instanceof Constructor)
    return ((Constructor<?>) m).getAnnotation(annotationClass);
else
    return ((Method) m).getAnnotation(annotationClass);
AnnotationgetAnnotation(Method m, Class annotationClass)
get Annotation
return m.getAnnotation(annotationClass);