Java Reflection Annotation getAnnotationMemberType(Annotation annotation, String memberName)

Here you can find the source of getAnnotationMemberType(Annotation annotation, String memberName)

Description

return the type of an annotation member, or null if the member is not found in the annotation

License

Open Source License

Parameter

Parameter Description
annotation a parameter
memberName a parameter

Declaration

public static Class<?> getAnnotationMemberType(Annotation annotation, String memberName) 

Method Source Code

//package com.java2s;
/**//from w  w  w .ja v a 2s. com
 * EKO : A Peer To Peer Application
 * Copyright (c) J?r?me Bonnet 2008-2009
 * All Rights Reserved
 * 
 *  Unless you own a different specific licence for this progran, 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 
 * A moins que vous ne disposer d'une licence diff?rente pour ce programme, Ce programme est libre, vous pouvez le redistribuer et/ou le modifier selon les termes de 
 * la Licence Publique G?n?rale GNU publi?e par la Free Software Foundation (version 3).
 * 
 * Ce programme est distribu? car potentiellement utile, mais SANS AUCUNE GARANTIE, ni explicite ni implicite, y compris les garanties de commercialisation ou d'adaptation 
 * dans un but sp?cifique. Reportez-vous ? la Licence Publique G?n?rale GNU pour plus de d?tails.
 * 
 * Vous devez avoir re?u une copie de la Licence Publique G?n?rale GNU en m?me temps que ce programme ; si ce n'est pas le cas, ?crivez ? la Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, ?tats-Unis. 
 */

import java.lang.annotation.Annotation;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Main {
    private static Map<Class<? extends Annotation>, Map<String, Method>> reflexionCache = new ConcurrentHashMap<Class<? extends Annotation>, Map<String, Method>>();

    /**
     * return the type of an annotation member, or null if the member is not found in the annotation
     * @param annotation
     * @param memberName
     * @return
     */
    public static Class<?> getAnnotationMemberType(Annotation annotation, String memberName) {
        Class<? extends Annotation> annotationType = annotation.annotationType();
        return getAnnotationMemberType(annotationType, memberName);
    }

    public static Class<?> getAnnotationMemberType(Class<? extends Annotation> annotationType, String memberName) {
        Map<String, Method> annotationMethods = getAnnotationMethods(annotationType);
        Method memberMethod = annotationMethods.get(memberName);
        if (memberMethod == null)
            return null;
        return memberMethod.getReturnType();
    }

    private static Map<String, Method> getAnnotationMethods(Class<? extends Annotation> annotationType) {
        Map<String, Method> annotationMethods = reflexionCache.get(annotationType);
        if (annotationMethods == null) { //Note about thread-safety: there may be 2 threads building this map but I don't care
            annotationMethods = new HashMap<String, Method>();
            for (Method method : annotationType.getMethods()) {
                if (method.getParameterTypes().length == 0 && method.getReturnType().equals(Void.class) == false) {
                    String methName = method.getName();
                    if (methName.equals("equals") || methName.equals("toString") || methName.equals("hashCode")
                            || methName.equals("annotationType")) {
                        //do nothing, this is stupid, but I prefer to write the if expression this way, it seems clearer to me
                    } else {
                        annotationMethods.put(methName, method);
                    }
                }
            }
            reflexionCache.put(annotationType, annotationMethods);
        }
        return annotationMethods;
    }
}

Related

  1. getAnnotationInClass( final Class annotationClass, final Class clazz)
  2. getAnnotationInHeirarchy(Class annotationClass, Class aClass)
  3. getAnnotationInherited(Class type, Class annotationClass)
  4. getAnnotationInstances(Class clazz, Class annotationClass)
  5. getAnnotationMemberDefaults(Annotation annotation)
  6. getAnnotationMetaAnnotated( AnnotatedElement annotatedElementClass, Class metaAnnotationToFind)
  7. getAnnotationMethods(Class annotationType)
  8. getAnnotationOfField(Field field, Class clazz)
  9. getAnnotationOuterIndecies(Class annotationClass, Annotation[][] annotations)