Java Reflection Method Annotation getMethodAnnotatedWith(Class toIntrospect, Class c)

Here you can find the source of getMethodAnnotatedWith(Class toIntrospect, Class c)

Description

get Method Annotated With

License

Open Source License

Declaration

public static List<Method> getMethodAnnotatedWith(Class<?> toIntrospect, Class<? extends Annotation> c) 

Method Source Code

//package com.java2s;
/**// ww w  . ja v  a  2s. c  o m
 * EKO : A Peer To Peer Application
 * Copyright (c) J?r?me Bonnet 2008-2009
 * All Rights Reserved
 * 
 *  Unless you own a different specific license for this program, 
 *  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.lang.reflect.Modifier;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    public static List<Method> getMethodAnnotatedWith(Class<?> toIntrospect, Class<? extends Annotation> c) {
        ArrayList<Method> result = new ArrayList<Method>();
        Map<String, Method> allM = new HashMap<String, Method>();
        Class<?> currentToIntrospect = toIntrospect;
        while (currentToIntrospect != null) {
            for (Method me : currentToIntrospect.getMethods()) {
                if (Modifier.isPublic(me.getModifiers())) {
                    //TODO: two different method (different parameters) with the same name will obfuscate themselves 
                    if (allM.containsKey(me.getName()) == false)
                        allM.put(me.getName(), me);
                }
            }
            currentToIntrospect = currentToIntrospect.getSuperclass();
        }
        for (Method method : allM.values()) {
            Annotation ann = method.getAnnotation(c);
            if (ann != null) {
                result.add(method);
            }
        }
        return result;
    }
}

Related

  1. getMethod(Class annotationClass, Class aClass)
  2. getMethod(Class compClassOrSuper, Class annotation)
  3. getMethodAnnotation(Class a, Method m)
  4. getMethodAnnotation(final Method method, final Class annoClass)
  5. getMethodAnnotation(final Method method, final Class annotationType)
  6. getMethodAnnotation(Method method, Class annotation)