Java Reflection Annotation getAnnotationAttributes(final Annotation annotation)

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

Description

Retrieve the given annotation's attributes as a Map.

License

Apache License

Parameter

Parameter Description
annotation the annotation to retrieve the attributes for

Return

the Map of annotation attributes, with attribute names as keys and corresponding attribute values as values

Declaration

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

Method Source Code


//package com.java2s;
/*/*  ww  w  .j a v  a 2s.  co  m*/
 * Copyright 2002-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

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

public class Main {
    /**
     * Retrieve the given annotation's attributes as a Map.
     *
     * @param annotation the annotation to retrieve the attributes for
     *
     * @return the Map of annotation attributes, with attribute names as keys
     *         and corresponding attribute values as values
     */
    public static Map<String, Object> getAnnotationAttributes(final Annotation annotation) {
        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) {
                    throw new IllegalStateException("Could not obtain annotation attribute values", ex);
                }
            }
        }
        return attrs;
    }
}

Related

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