Java Reflection Annotation getAnnotationAttributes(Annotation annotation)

Here you can find the source of getAnnotationAttributes(Annotation annotation)

Description

get Annotation Attributes

License

Apache License

Declaration

public static Map<String, Object> getAnnotationAttributes(Annotation annotation) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import java.util.HashMap;
import java.util.Map;

public class Main {
    public static Map<String, Object> getAnnotationAttributes(Annotation annotation) {
        return getAnnotationAttributes(annotation, false);
    }//from  w ww  . ja  v a 2 s  . com

    public static Map<String, Object> getAnnotationAttributes(Annotation annotation, boolean classValuesAsString) {
        Map<String, Object> attrs = new HashMap<String, Object>();
        Method[] methods = annotation.annotationType().getDeclaredMethods();
        for (Method method : methods) {
            if (method.getParameterTypes().length == 0 && method.getReturnType() != void.class) {
                try {
                    Object value = method.invoke(annotation);
                    if (classValuesAsString) {
                        if (value instanceof Class) {
                            value = ((Class) value).getName();
                        } else if (value instanceof Class[]) {
                            Class[] clazzArray = (Class[]) value;
                            String[] newValue = new String[clazzArray.length];
                            for (int i = 0; i < clazzArray.length; i++) {
                                newValue[i] = clazzArray[i].getName();
                            }
                            value = newValue;
                        }
                    }
                    attrs.put(method.getName(), value);
                } catch (Exception ex) {
                    throw new IllegalStateException("Could not obtain annotation attribute values", ex);
                }
            }
        }
        return attrs;
    }
}

Related

  1. getAnnotation(Object obj, Class annotation)
  2. getAnnotation(ProceedingJoinPoint pjp, Class annotationClass)
  3. getAnnotationAttribute(Annotation a, String attrName)
  4. getAnnotationAttribute(final Annotation anno, final String attrName, final Class attrType)
  5. getAnnotationAttributes(Annotation annotation)
  6. getAnnotationAttributes(final Annotation annotation)
  7. getAnnotationAttributeValue(final Annotation annotation, final String attributeName)
  8. getAnnotationByType(AnnotatedElement element, Class annotationType)
  9. getAnnotationByType(List annotations, Class annotationType)