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(ProceedingJoinPoint pjp, Class annotationClass)
get Annotation
final MethodSignature methodSignature = ((MethodSignature) pjp.getSignature());
final Annotation[] annotations = methodSignature.getMethod().getAnnotations();
T lookupAnnotation = null;
for (Annotation annotation : annotations) {
    if (annotation.annotationType().equals(annotationClass)) {
        lookupAnnotation = (T) annotation;
        break;
return lookupAnnotation;
ObjectgetAnnotationAttribute(Annotation a, String attrName)
Returns attribute value of given annotation with attrName attribute name.
try {
    Method method = a.annotationType().getMethod(attrName);
    return method.invoke(a);
} catch (NoSuchMethodException e) {
    throw new AssertionError(e);
} catch (SecurityException e) {
    throw new AssertionError(e);
} catch (IllegalAccessException e) {
...
OptionalgetAnnotationAttribute(final Annotation anno, final String attrName, final Class attrType)
get Annotation Attribute
try {
    final Method method = anno.annotationType().getMethod(attrName);
    method.setAccessible(true);
    if (!attrType.equals(method.getReturnType())) {
        return Optional.empty();
    final Object value = method.invoke(anno);
    return Optional.of((T) value);
...
MapgetAnnotationAttributes(Annotation annotation)
get Annotation Attributes
Map attrs = new HashMap();
Method methods[] = annotation.annotationType().getDeclaredMethods();
for (int j = 0; j < methods.length; j++) {
    Method method = methods[j];
    if (method.getParameterTypes().length != 0 || method.getReturnType() == Void.TYPE)
        continue;
    try {
        attrs.put(method.getName(), method.invoke(annotation, new Object[0]));
...
MapgetAnnotationAttributes(Annotation annotation)
get Annotation Attributes
return getAnnotationAttributes(annotation, false);
MapgetAnnotationAttributes(final Annotation annotation)
Retrieve the given annotation's attributes as a Map.
final Map<String, Object> attrs = new HashMap<String, Object>();
final Method[] methods = annotation.annotationType().getDeclaredMethods();
for (int j = 0; j < methods.length; j++) {
    final Method method = methods[j];
    if (method.getParameterTypes().length == 0 && method.getReturnType() != void.class) {
        try {
            attrs.put(method.getName(), method.invoke(annotation));
        } catch (final Exception ex) {
...
StringgetAnnotationAttributeValue(final Annotation annotation, final String attributeName)
Retrieve the value of an Annotation's attribute
String value = null;
if (annotation != null) {
    try {
        value = annotation.annotationType().getMethod(attributeName).invoke(annotation).toString();
    } catch (Exception ex) {
return value;
...
AnnotationgetAnnotationByType(AnnotatedElement element, Class annotationType)
get Annotation By Type
Annotation[] annotations = element.getAnnotations();
for (Annotation annotation : annotations) {
    if (annotation.annotationType().equals(annotationType)) {
        return annotation;
return null;
AnnotationgetAnnotationByType(List annotations, Class annotationType)
get Annotation By Type
if (annotations == null) {
    return null;
return getAnnotationByType(annotations.toArray(NO_ANNOTATIONS), annotationType);
ClassgetAnnotationClass(Annotation a)
get Annotation Class
Class<?>[] interfaces = a.getClass().getInterfaces();
assert interfaces.length == 1 : "Customize annotations with multiple interfaces are not supported: "
        + a.getClass();
return (Class<? extends Annotation>) interfaces[0];