Class AnnotationUtil

java.lang.Object
org.dellroad.stuff.java.AnnotationUtil

public final class AnnotationUtil extends Object
Annotation utility methods.
  • Method Details

    • getAnnotations

      public static List<Annotation> getAnnotations(Class<?> methodClass, String methodName, Class<?>... methodParameterTypes)
      Introspect the specified method and read all annotations.
      Parameters:
      methodClass - method's declaring class
      methodName - method name
      methodParameterTypes - method parameter types
      Returns:
      list of annotations found
      Throws:
      IllegalArgumentException - if method is not found
      RuntimeException - if method cannot be instrospected
      IllegalArgumentException - if any parameter is null
    • getAnnotation

      public static <A extends Annotation> A getAnnotation(Class<A> annotationType, Class<?> methodClass, String methodName, Class<?>... methodParameterTypes)
      Introspect the specified method and read the specified annotation, which must exist.
      Type Parameters:
      A - annotation type
      Parameters:
      annotationType - annotation to look for
      methodClass - method's declaring class
      methodName - method name
      methodParameterTypes - method parameter types
      Returns:
      the annotation found
      Throws:
      IllegalArgumentException - if method is not found
      RuntimeException - if method cannot be instrospected
      IllegalArgumentException - if any parameter is null
    • applyAnnotationValues

      public static <A extends Annotation> void applyAnnotationValues(Object bean, String methodPrefix, A annotation, A defaults, BiFunction<? super List<Method>,String,String> propertyFinder, Function<? super Class<?>,?> instantiator)
      Read annotation values from the given annotation and apply those values to the corresponding properties of the given bean.

      For example, if the annotation has a foo() property of type int, and bean has a method like setFoo(int), then the annotation value will be used to set the property using the method.

      Properties that are Class instances result in instantiating the given class, unless the property actually has type Class.

      The propertyFinder allows the caller to specify which annotation property should be used for the given setter method(s). In cases where there are multiple methods with the same name, all avilable methods are listed with those taking narrower parameter types listed first. If propertyFinder returns a non-null annotation property name, that value will be set using the first method in the list that accepts it.

      Non-primitive array values are converted into Lists if necessary.

      Type Parameters:
      A - annotation type
      Parameters:
      bean - target bean
      methodPrefix - setter method name prefix (typically "set")
      annotation - annotation with properties
      defaults - annotation with default values; if non-null, values in annotation equal to their default are skipped
      propertyFinder - returns the annotation property name for given setter method with the given bean property name, or null if none
      instantiator - used to instantiate new instances when annotation property has type Class
      Throws:
      IllegalArgumentException - if a required parameter is null
      IllegalArgumentException - if a bean property is being set more than once
      RuntimeException - if an unexpected error occurs