count Methods With Annotation - Java java.lang.annotation

Java examples for java.lang.annotation:Method Annotation

Description

count Methods With Annotation

Demo Code


//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();
    }/*from w  w w  .  j  a v a 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;
    }
}

Related Tutorials