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

ListgetAnnotations(Object instance)
get Annotations
return new ArrayList<Annotation>(Arrays.asList(instance.getClass().getAnnotations()));
Annotation[]getAnnotations(Set fields, Set methods)
get annotations by given fields and methods
List<Annotation> annotations = new LinkedList<Annotation>();
if (fields != null) {
    for (Field field : fields) {
        Annotation[] annos = field.getAnnotations();
        if (annos != null) {
            for (Annotation annotation : annos) {
                annotations.add(annotation);
if (methods != null) {
    for (Method method : methods) {
        Annotation[] annos = method.getAnnotations();
        if (annos != null) {
            for (Annotation annotation : annos) {
                annotations.add(annotation);
return annotations.toArray(new Annotation[annotations.size()]);
Annotation[]getAnnotationsOf(Class annoClass)
get Annotations Of
return annoClass.getAnnotations();
CollectiongetAnnotationsRecursive(Class clazz)
get Annotations Recursive
Collection<Annotation> allAnnotations = new HashSet<>();
getAllAnnotations(clazz, allAnnotations);
return allAnnotations;
ListgetAnnotationsRecursive(final Class type, final Class annotationType)
get Annotations Recursive
List<T> annotations = new ArrayList<T>();
Class<?> current = type;
while (!Object.class.equals(current)) {
    T annotation = current.getAnnotation(annotationType);
    if (annotation != null) {
        annotations.add(annotation);
    current = current.getSuperclass();
...
VgetAnnotationValue(@Nonnull Class cls, @Nonnull Class annotation, V def)
get Annotation Value
final A a = getAnnotation(cls, annotation);
return a != null ? get(a, "value") : def;
StringgetAnnotationValue(Annotation annotation, String attributeName)
get Annotation Value
String value = null;
if (annotation != null) {
    try {
        value = (String) annotation.annotationType().getMethod(attributeName).invoke(annotation);
    } catch (Exception ignore) {
        ignore.printStackTrace();
return value;
TgetAnnotationValue(Annotation annotation, String value, Class expectedType)
Return a value from an annotation.
Class<? extends Annotation> annotationType = annotation.annotationType();
Method valueMethod;
try {
    valueMethod = annotationType.getDeclaredMethod(value);
} catch (NoSuchMethodException e) {
    throw new IllegalStateException(
            "Cannot resolve required method '" + value + "()' for '" + annotationType + "'.");
Object elementValue;
try {
    elementValue = valueMethod.invoke(annotation);
} catch (ReflectiveOperationException e) {
    throw new IllegalStateException("Cannot invoke method value() for " + annotationType);
return elementValue != null ? expectedType.cast(elementValue) : null;
ObjectgetAnnotationValue(Annotation annotation, String valueName)
get Annotation Value
try {
    return annotation.annotationType().getMethod(valueName).invoke(annotation);
} catch (Exception e) {
    throw new RuntimeException(e);
ObjectgetAnnotationValue(Class anno, Field field, String paramName)
get Annotation Value
Object value = null;
if (hasAnnotation(anno, field) && (paramName != null && !paramName.isEmpty())) {
    value = callMethodNamed(paramName, field.getAnnotation(anno));
return value;