Java Reflection Annotation getAnnotationMethods(Class annotationType)

Here you can find the source of getAnnotationMethods(Class annotationType)

Description

get Annotation Methods

License

Open Source License

Declaration

private static Map<String, Method> getAnnotationMethods(Class<? extends Annotation> annotationType) 

Method Source Code

//package com.java2s;
/**/*  www . jav  a  2s . co  m*/
 * 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>>();

    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. getAnnotationInherited(Class type, Class annotationClass)
  2. getAnnotationInstances(Class clazz, Class annotationClass)
  3. getAnnotationMemberDefaults(Annotation annotation)
  4. getAnnotationMemberType(Annotation annotation, String memberName)
  5. getAnnotationMetaAnnotated( AnnotatedElement annotatedElementClass, Class metaAnnotationToFind)
  6. getAnnotationOfField(Field field, Class clazz)
  7. getAnnotationOuterIndecies(Class annotationClass, Annotation[][] annotations)
  8. getAnnotationPropertyValue(Annotation a, String annotationPropertyName)
  9. getAnnotationRecursive(Class cls, Class annotationClass)