Java Annotation countMethodsWithAnnotation(Class<?> klass, Class<? extends Annotation> annotation)

PreviousNext

//package com.java2s;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static int countMethodsWithAnnotation(Class<?> klass, Class<? extends Annotation> annotation) {
        return getMethodsWithAnnotation(klass, annotation).size();
    }//    w w w  .    d e m   o   2  s  .c   o   m

    public static List<Method> getMethodsWithAnnotation(Class<?> klass, Class<? extends Annotation> annotation) {
        List<Method> methodList = new ArrayList<Method>();

        for (Method m : klass.getMethods()) {
            if (m.isAnnotationPresent(annotation)) {
                methodList.add(m);
            }
        }

        return methodList;
    }
}
PreviousNext

Related