Retrieve the given annotation's attributes as a Map, preserving all attribute types as-is. - Java java.lang.annotation

Java examples for java.lang.annotation:Annotation Type

Description

Retrieve the given annotation's attributes as a Map, preserving all attribute types as-is.

Demo Code

/*/* w w  w  . j  a va 2  s .  co  m*/
 * Copyright 2002-2010 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.
 */
//package com.java2s;
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, preserving all attribute types as-is.
     * @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(
            Annotation annotation) {
        return getAnnotationAttributes(annotation, false);
    }

    /**
     * Retrieve the given annotation's attributes as a Map.
     * @param annotation the annotation to retrieve the attributes for
     * @param classValuesAsString whether to turn Class references into Strings (for compatibility with
     * {@link org.springframework.core.type.AnnotationMetadata} or to preserve them as Class references
     * @return the Map of annotation attributes, with attribute names as keys and
     * corresponding attribute values as values
     */
    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 Tutorials