Mega Code Archive

 
Categories / Java / Reflection
 

Find Annotated Method

import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; public class Util{   public static Method findAnnotatedMethod(Class<?> clazz, Class<? extends Annotation> annotationClass) {     for (Method method : clazz.getMethods())       if( method.isAnnotationPresent(annotationClass))         return(method);     return(null);       }      public static List<Method> findAnnotatedMethods(Class<?> clazz, Class<? extends Annotation> annotationClass) {     Method[] methods = clazz.getMethods();     List<Method> annotatedMethods = new ArrayList<Method>(methods.length);     for (Method method : methods) {       if( method.isAnnotationPresent(annotationClass)){         annotatedMethods.add(method);       }     }     return annotatedMethods;       } }