Java Reflection Method Annotation getMethodsAnnotatedWith(Class owner, Class annotationClass, boolean includeSuper)

Here you can find the source of getMethodsAnnotatedWith(Class owner, Class annotationClass, boolean includeSuper)

Description

get Methods Annotated With

License

Open Source License

Declaration

public static List<Method> getMethodsAnnotatedWith(Class owner, Class<? extends Annotation> annotationClass,
            boolean includeSuper) 

Method Source Code


//package com.java2s;
/*//from w ww.ja v a 2 s. c  o  m
 * Bristleback Websocket Framework - Copyright (c) 2010-2013 http://bristleback.pl
 * ---------------------------------------------------------------------------
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation; either version 3 of the License, or (at your
 * option) any later version.
 * This library 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.
 * You should have received a copy of the GNU Lesser General Public License along
 * with this program; if not, see <http://www.gnu.org/licenses/lgpl.html>.
 * ---------------------------------------------------------------------------
 */

import java.lang.annotation.Annotation;

import java.lang.reflect.Method;

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static List<Method> getMethodsAnnotatedWith(Class owner, Class<? extends Annotation> annotationClass,
            boolean includeSuper) {
        List<Method> selectedMethods = new ArrayList<Method>();
        Method[] allMethods = owner.getMethods();
        for (Method method : allMethods) {
            if (!includeSuper && isMethodInherited(owner, method)) {
                continue;
            }
            if (method.isAnnotationPresent(annotationClass)) {
                selectedMethods.add(method);
            }
        }

        return selectedMethods;
    }

    private static boolean isMethodInherited(Class clazz, Method method) {
        return method.getDeclaringClass() != clazz;
    }
}

Related

  1. getMethods(Class clazz, Class annotation)
  2. getMethods(final Class clazz, final Class annotation)
  3. getMethods(final Class t, final Class a)
  4. getMethodsAnnotated(Class anno, Class holder)
  5. getMethodsAnnotatedBy(Class clazz, Class annotClass)
  6. getMethodsAnnotatedWith(Class clazz, Class annotClazz)
  7. getMethodsAnnotatedWith(Class clazz, Class annotation)
  8. getMethodsAnnotatedWith(final Class type, final Class annotation)
  9. getMethodsInAnnotation(Annotation anno)