find Annotated Method - Java java.lang.annotation

Java examples for java.lang.annotation:Method Annotation

Description

find Annotated Method

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 void main(String[] argv) throws Exception {
        Object instance = "java2s.com";
        Class annotatedClass = String.class;
        System.out.println(findAnnotatedMethod(instance, annotatedClass));
    }//from   w ww  .ja v a2s.c om

    static public <T extends Annotation> List<Method> findAnnotatedMethod(
            Object instance, Class<T> annotatedClass) throws Exception {
        Method[] method = instance.getClass().getMethods();
        List<Method> holder = new ArrayList<Method>();
        for (Method selMethod : method) {
            T annotation = selMethod.getAnnotation(annotatedClass);
            if (annotation != null) {
                holder.add(selMethod);
            }
        }
        return holder;
    }
}

Related Tutorials