Class AnnotationUtil
-
Method Summary
Modifier and TypeMethodDescriptionstatic <A extends Annotation>
voidapplyAnnotationValues
(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.static <A extends Annotation>
AgetAnnotation
(Class<A> annotationType, Class<?> methodClass, String methodName, Class<?>... methodParameterTypes) Introspect the specified method and read the specified annotation, which must exist.static List<Annotation>
getAnnotations
(Class<?> methodClass, String methodName, Class<?>... methodParameterTypes) Introspect the specified method and read all annotations.
-
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 classmethodName
- method namemethodParameterTypes
- method parameter types- Returns:
- list of annotations found
- Throws:
IllegalArgumentException
- if method is not foundRuntimeException
- if method cannot be instrospectedIllegalArgumentException
- 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 formethodClass
- method's declaring classmethodName
- method namemethodParameterTypes
- method parameter types- Returns:
- the annotation found
- Throws:
IllegalArgumentException
- if method is not foundRuntimeException
- if method cannot be instrospectedIllegalArgumentException
- 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 typeint
, and bean has a method likesetFoo(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 typeClass
.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. IfpropertyFinder
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
List
s if necessary.- Type Parameters:
A
- annotation type- Parameters:
bean
- target beanmethodPrefix
- setter method name prefix (typically"set"
)annotation
- annotation with propertiesdefaults
- annotation with default values; if non-null, values inannotation
equal to their default are skippedpropertyFinder
- returns the annotation property name for given setter method with the given bean property name, or null if noneinstantiator
- used to instantiate new instances when annotation property has typeClass
- Throws:
IllegalArgumentException
- if a required parameter is nullIllegalArgumentException
- if a bean property is being set more than onceRuntimeException
- if an unexpected error occurs
-