Class ReflectionUtils
- java.lang.Object
-
- org.wicketstuff.rest.utils.reflection.ReflectionUtils
-
public class ReflectionUtils extends Object
Utility methods to work with reflection entities- Author:
- andrea del bene
-
-
Constructor Summary
Constructors Constructor Description ReflectionUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T extends Annotation>
TfindAnnotation(Annotation[] parameterAnnotations, Class<T> targetAnnotation)
Utility method to find if an annotation type is present in an array of annotations.static Method
findMethod(Class<?> clazz, String name, Class<?>... parameterTypes)
Safely search for a method with a given signature (name + parameter types) on a given class.static <T extends Annotation>
TfindMethodParameterAnnotation(Method ownerMethod, int paramIndex, Class<T> targetAnnotation)
Return the specified annotation for the method parameter at a given position (seeparamIndex
).static <T> T
getAnnotationField(Annotation annotation, String fieldName, T defaultValue)
Get the value of an annotation field.static Annotation
getAnnotationParam(int i, Method method)
Return the annotation (annotated with anAnnotatedParam
) used with a given method parameter.static List<Class<?>>
getElementsClasses(Collection<?> collection)
Extract the list of types of every element of a given collection.static <T> T
invokeMethod(Object target, String name, Class<?>... parameterTypes)
Safely invoke a method with the given signature (name + parameter types) on the given target object.static boolean
isParameterAnnotatedWith(int i, Method method, Class<? extends Annotation> targetAnnotation)
Check if a parameter is annotated with a given annotation.
-
-
-
Method Detail
-
isParameterAnnotatedWith
public static boolean isParameterAnnotatedWith(int i, Method method, Class<? extends Annotation> targetAnnotation)
Check if a parameter is annotated with a given annotation.- Parameters:
i
- method parameter index.method
- the method the parameter belongs to.targetAnnotation
- the annotation type we want to check for.- Returns:
- true if the method parameter is annotated with the given annotation, false otherwise.
-
getAnnotationParam
public static Annotation getAnnotationParam(int i, Method method)
Return the annotation (annotated with anAnnotatedParam
) used with a given method parameter.- Parameters:
i
- method parameter index.method
- the method the parameter belongs to.- Returns:
- the annotation if any is found, null otherwise.
- See Also:
AnnotatedParam
-
findAnnotation
public static <T extends Annotation> T findAnnotation(Annotation[] parameterAnnotations, Class<T> targetAnnotation)
Utility method to find if an annotation type is present in an array of annotations.- Parameters:
parameterAnnotations
- the array of annotations we will look in.targetAnnotation
- the type of annotation we are looking for.- Returns:
- the first occurrence of the targetAnnotation found in the array, null if no occurrence was found.
-
findMethodParameterAnnotation
public static <T extends Annotation> T findMethodParameterAnnotation(Method ownerMethod, int paramIndex, Class<T> targetAnnotation)
Return the specified annotation for the method parameter at a given position (seeparamIndex
).- Parameters:
ownerMethod
- the parameter's methodparamIndex
- the parameter indextargetAnnotation
- the annotation type to search for- Returns:
- return an instance of the given annotation type, or null if the method parameter it's not annotated.
-
findMethod
public static Method findMethod(Class<?> clazz, String name, Class<?>... parameterTypes)
Safely search for a method with a given signature (name + parameter types) on a given class.- Parameters:
clazz
- the target classname
- method nameparameterTypes
- method's parameters types.- Returns:
- the method corresponding to the given signature, null if such a method doesn't exist.
-
invokeMethod
public static <T> T invokeMethod(Object target, String name, Class<?>... parameterTypes)
Safely invoke a method with the given signature (name + parameter types) on the given target object.- Parameters:
target
- the target objectname
- method nameparameterTypes
- method's parameters types.- Returns:
- the value returned by the method invocation.
-
getAnnotationField
public static <T> T getAnnotationField(Annotation annotation, String fieldName, T defaultValue)
Get the value of an annotation field.- Parameters:
annotation
- the target annotationfieldName
- the field namedefaultValue
- the default value to return- Returns:
- the value of the field or the default value if the filed has no value.
-
getElementsClasses
public static List<Class<?>> getElementsClasses(Collection<?> collection)
Extract the list of types of every element of a given collection.- Parameters:
collection
- the collection in input.- Returns:
- the list containing the type of list elements.
-
-