Find Annotated Method : Annotation « Reflection « Java






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;    
  }
}

   
    
    
    
  








Related examples in the same category

1.Uses reflection to display the annotation associated with a method.
2.Get annotation by annotation class
3.Show all annotations for a class and a method.
4.default values in an annotation.
5.Does a method have an annotation
6.Get Annotation Parameter
7.A better concise toString method for annotation types
8.Find Annotated Fields
9.Get default annotation value
10.Get annotation value
11.Is Field Annotation Present
12.Get Annotated Declared Fields