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

ClassgetAnnotationClass(Annotation annotation)
Resolve the impl's class.
return annotation.annotationType();
ClassgetAnnotationClass(Class clazz, Class annotation)
Loads annotation class, using class loader of the specified class.
ClassLoader clazzClassLoader = clazz.getClassLoader();
ClassLoader annotationClassLoader = annotation.getClassLoader();
if (null != clazzClassLoader && !clazzClassLoader.equals(annotationClassLoader)) {
    try {
        return (Class<T>) clazzClassLoader.loadClass(annotation.getName());
    } catch (ClassNotFoundException e) {
return annotation;
TgetAnnotationClass(Class entityClass, Class annotationClass)
Return a specific annotation class if exist in class, otherwise return false
return hasAnnotationClass(entityClass, annotationClass) ? entityClass.getAnnotation(annotationClass) : null;
ClassgetAnnotationClass(String name)
get Annotation Class
try {
    return (Class<? extends Annotation>) Class.forName(name);
} catch (ClassNotFoundException e) {
    return null;
AnnotationgetAnnotationDeep(Annotation from, Class toFind)
This method is looking for the annotation class toFind from the annoFrom annotation instance.
if (from.annotationType().equals(toFind)) {
    return from;
} else {
    for (Annotation anno : from.annotationType().getAnnotations()) {
        if (!anno.annotationType().getPackage().getName().startsWith(JAVA_LANG)) {
            return getAnnotationDeep(anno, toFind);
return null;
TgetAnnotationDefault(Class annotationClass, String element)
get Annotation Default
Method method = null;
try {
    method = annotationClass.getMethod(element, (Class[]) null);
} catch (NoSuchMethodException e) {
    return null;
return ((T) method.getDefaultValue());
MapgetAnnotationDefaultMap(Class annotationClass)
get Annotation Default Map
return Stream.of(annotationClass.getDeclaredMethods()).filter(m -> m.getDefaultValue() != null)
        .collect(Collectors.toMap(m -> m.getName(), m -> m.getDefaultValue()));
TgetAnnotationElementValue(AnnotatedElement annotatedElement, String annotationClassName, String annotationElementName, Class annotationElementType)
get Annotation Element Value
final Annotation annotation = getAnnotation(annotatedElement, annotationClassName);
return getAnnotationElementValue(annotation, annotationElementName, annotationElementType);
ObjectgetAnnotationField(Annotation annotation, String field)
Access to the value of a field in an annotation using reflection.
Class<?> annotClass = annotation.getClass();
try {
    Method getValue = annotClass.getMethod(field);
    return getValue.invoke(annotation);
} catch (Exception e) {
    e.printStackTrace();
    return null;
ListgetAnnotationFields(Class claz, Class annotationType)
get fields list of a type, and all the fields returned contains the given annotation
List<Field> result = new ArrayList<Field>();
if (claz != null) {
    Field[] fields = claz.getDeclaredFields();
    for (Field field : fields) {
        if (field.getAnnotation(annotationType) != null) {
            result.add(field);
    result.addAll(getAnnotationFields(claz.getSuperclass(), annotationType));
return result;