org.wicketstuff.rest.utils.reflection
Class ReflectionUtils

java.lang.Object
  extended by org.wicketstuff.rest.utils.reflection.ReflectionUtils

public class ReflectionUtils
extends Object

Utility methods to work with reflection entities

Author:
andrea del bene

Constructor Summary
ReflectionUtils()
           
 
Method Summary
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.
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>
T
findMethodParameterAnnotation(Method ownerMethod, int paramIndex, Class<T> targetAnnotation)
          Return the specified annotation for the method parameter at a given position (see paramIndex).
static
<T> T
getAnnotationField(Annotation annotation, String fieldName, T defaultValue)
          Get the value of an annotation field.
static Annotation getAnnotationParam(int i, Method method)
          Check if a parameter is annotated with an AnnotatedParam annotation.
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectionUtils

public ReflectionUtils()
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)
Check if a parameter is annotated with an AnnotatedParam annotation.

Parameters:
i - method parameter index.
method - the method the parameter belongs to.
Returns:
true if such an annotation is found, false 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 (see paramIndex).

Parameters:
ownerMethod - the parameter's method
paramIndex - the parameter index
targetAnnotation - 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 class
name - method name
parameterTypes - 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 object
name - method name
parameterTypes - 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 annotation
fieldName - the field name
defaultValue - 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.


Copyright © 2013–2014. All rights reserved.