Java Reflection Method Annotation getMethods(final Class clazz, final Class annotation)

Here you can find the source of getMethods(final Class clazz, final Class annotation)

Description

Return all method(s) of a specified class that contain the specified annotation.

License

Mozilla Public License

Parameter

Parameter Description
clazz the class to interrogate
annotation the annotation to find

Return

return the list of methods found (an empty list is possible)

Declaration

public static List<Method> getMethods(final Class<?> clazz, final Class<? extends Annotation> annotation) 

Method Source Code

//package com.java2s;
/*// w  w  w . j  av  a 2  s  . c om
 * Copyright (c) 2015 Jeremy Miller
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

import java.lang.annotation.Annotation;

import java.lang.reflect.Method;

import java.util.LinkedList;
import java.util.List;

public class Main {
    /**
     * Return all method(s) of a specified class that contain the specified annotation.
     * @param clazz                    the class to interrogate
     * @param annotation               the annotation to find
     * @return                         return the list of methods found (an empty list is possible)
     */
    public static List<Method> getMethods(final Class<?> clazz, final Class<? extends Annotation> annotation) {
        final List<Method> annotatedMethods = new LinkedList<Method>();

        for (Method method : clazz.getMethods()) {
            if (method.isAnnotationPresent(annotation)) {
                annotatedMethods.add(method);
            }
        }

        return annotatedMethods;
    }
}

Related

  1. getMethodLevelAnnotations(Class clazz, Class annotation)
  2. getMethodName(final Class annoType, final Class klazType)
  3. getMethodOrClassLevelAnnotation(Class annotationClass, Method method, Class clazz)
  4. getMethods(Annotation anno)
  5. getMethods(Class clazz, Class annotation)
  6. getMethods(final Class t, final Class a)
  7. getMethodsAnnotated(Class anno, Class holder)
  8. getMethodsAnnotatedBy(Class clazz, Class annotClass)
  9. getMethodsAnnotatedWith(Class owner, Class annotationClass, boolean includeSuper)