org.hibernate.validator.util
Class ReflectionHelper

java.lang.Object
  extended by org.hibernate.validator.util.ReflectionHelper

public final class ReflectionHelper
extends Object

Some reflection utility methods. Where necessary calls will be performed as PrivilegedAction which is necessary for situations where a security manager is in place.

Author:
Hardy Ferentschik, Gunnar Morling, Kevin Pollet - SERLI - (kevin.pollet@serli.com)

Method Summary
static Class<?> boxedType(Type primitiveType)
          Returns the autoboxed type of a primitive type.
static Set<Class<?>> computeAllImplementedInterfaces(Class<?> clazz)
          Get all interfaces a class directly implements.
static List<Class<?>> computeClassHierarchy(Class<?> clazz, boolean includeInterfaces)
          Get all superclasses and optionally interfaces recursively.
static boolean containsDeclaredField(Class<?> clazz, String fieldName)
          Checks whether the specified class contains a declared field with the given name.
static boolean containsMethodWithPropertyName(Class<?> clazz, String property)
          Checks whether the specified class contains a method for the specified property.
static
<T> T
getAnnotationParameter(Annotation annotation, String parameterName, Class<T> type)
           
static ClassLoader getClassLoaderFromClass(Class<?> clazz)
           
static ClassLoader getClassLoaderFromContext()
           
static
<T> Constructor<T>
getConstructor(Class<T> clazz, Class<?>... params)
           
static Field getDeclaredField(Class<?> clazz, String fieldName)
          Returns the declared field with the specified name or null if it does not exist.
static Field[] getDeclaredFields(Class<?> clazz)
          Returns the fields of the specified class.
static Method getDeclaredMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
          Returns the declared method with the specified name and parameter types or null if it does not exist.
static Method[] getDeclaredMethods(Class<?> clazz)
          Returns the declared methods of the specified class.
static Type getIndexedType(Type type)
          Determines the type of elements of an Iterable, array or the value of a Map.
static Object getIndexedValue(Object value, Integer index)
          Tries to retrieve the indexed value from the specified object.
static Object getMappedValue(Object value, Object key)
          Tries to retrieve the mapped value from the specified object.
static Member getMember(Class<?> clazz, String property, ElementType elementType)
          Returns the member with the given name and type.
static Method getMethod(Class<?> clazz, String methodName)
          Returns the method with the specified name or null if it does not exist.
static Method getMethodFromPropertyName(Class<?> clazz, String methodName)
          Returns the method with the specified property name or null if it does not exist.
static Method[] getMethods(Class<?> clazz)
          Returns the methods of the specified class (include inherited methods).
static String getPropertyName(Member member)
          Process bean properties getter by applying the JavaBean naming conventions.
static Class<?> getType(Member member)
          Returns the type of the field of return type of a method.
static Object getValue(Member member, Object object)
           
static boolean haveSameSignature(Method method1, Method method2)
          Checks, whether the given methods have the same signature, which is the case if they have the same name, parameter count and types.
static boolean isGetterMethod(Method member)
          Checks whether the given method's name is a valid JavaBeans getter name, meaning it starts with "is" or "has".
static boolean isIterable(Type type)
           
static boolean isList(Type type)
           
static boolean isMap(Type type)
           
static Class<?> loadClass(String className, Class<?> caller)
           
static
<T> T
newConstructorInstance(Constructor<T> constructor, Object... initArgs)
           
static
<T> T
newInstance(Class<T> clazz, String message)
           
static boolean propertyExists(Class<?> clazz, String property, ElementType elementType)
          Checks whether the property with the specified name and type exists on the given class.
static void setAccessibility(Member member)
           
static Type typeOf(Member member)
           
static Type typeOf(Method method, int parameterIndex)
          Returns the type of the parameter of the given method with the given parameter index.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getClassLoaderFromContext

public static ClassLoader getClassLoaderFromContext()

getClassLoaderFromClass

public static ClassLoader getClassLoaderFromClass(Class<?> clazz)

loadClass

public static Class<?> loadClass(String className,
                                 Class<?> caller)

getConstructor

public static <T> Constructor<T> getConstructor(Class<T> clazz,
                                                Class<?>... params)

newInstance

public static <T> T newInstance(Class<T> clazz,
                                String message)

newConstructorInstance

public static <T> T newConstructorInstance(Constructor<T> constructor,
                                           Object... initArgs)

getAnnotationParameter

public static <T> T getAnnotationParameter(Annotation annotation,
                                           String parameterName,
                                           Class<T> type)

getPropertyName

public static String getPropertyName(Member member)
Process bean properties getter by applying the JavaBean naming conventions.

Parameters:
member - the member for which to get the property name.
Returns:
The bean method name with the "is" or "get" prefix stripped off, null the method name id not according to the JavaBeans standard.

isGetterMethod

public static boolean isGetterMethod(Method member)
Checks whether the given method's name is a valid JavaBeans getter name, meaning it starts with "is" or "has".

Parameters:
member - The method of interest.
Returns:
True, if the given method is a JavaBeans getter method, false otherwise.

propertyExists

public static boolean propertyExists(Class<?> clazz,
                                     String property,
                                     ElementType elementType)
Checks whether the property with the specified name and type exists on the given class.

Parameters:
clazz - The class to check for the property. Cannot be null.
property - The property name without 'is', 'get' or 'has'. Cannot be null or empty.
elementType - The element type. Either ElementType.FIELD or ElementType METHOD.
Returns:
true is the property and can be access via the specified type, false otherwise.

getMember

public static Member getMember(Class<?> clazz,
                               String property,
                               ElementType elementType)
Returns the member with the given name and type.

Parameters:
clazz - The class from which to retrieve the member. Cannot be null.
property - The property name without 'is', 'get' or 'has'. Cannot be null or empty.
elementType - The element type. Either ElementType.FIELD or ElementType METHOD.
Returns:
the member which matching the name and type or null if no such member exists.

getType

public static Class<?> getType(Member member)
Returns the type of the field of return type of a method.

Parameters:
member - the member for which to get the type.
Returns:
Returns the type of the field of return type of a method.

typeOf

public static Type typeOf(Member member)
Parameters:
member - The Member instance for which to retrieve the type.
Returns:
Returns the Type of the given Field or Method.
Throws:
IllegalArgumentException - in case member is not a Field or Method.

typeOf

public static Type typeOf(Method method,
                          int parameterIndex)
Returns the type of the parameter of the given method with the given parameter index.

Parameters:
method - The method of interest.
parameterIndex - The index of the parameter for which the type should be returned.
Returns:
The erased type.

getValue

public static Object getValue(Member member,
                              Object object)

setAccessibility

public static void setAccessibility(Member member)

getIndexedType

public static Type getIndexedType(Type type)
Determines the type of elements of an Iterable, array or the value of a Map.

Parameters:
type - the type to inspect
Returns:
Returns the type of elements of an Iterable, array or the value of a Map. null is returned in case the type is not indexable (in the context of JSR 303).

isIterable

public static boolean isIterable(Type type)
Parameters:
type - the type to check.
Returns:
Returns true if type is a iterable type, false otherwise.

isMap

public static boolean isMap(Type type)
Parameters:
type - the type to check.
Returns:
Returns true if type is implementing Map, false otherwise.

isList

public static boolean isList(Type type)
Parameters:
type - the type to check.
Returns:
Returns true if type is implementing List, false otherwise.

getIndexedValue

public static Object getIndexedValue(Object value,
                                     Integer index)
Tries to retrieve the indexed value from the specified object.

Parameters:
value - The object from which to retrieve the indexed value. The object has to be non null and either a collection or array.
index - The index. The index does not have to be numerical. value could also be a map in which case the index could also be a string key.
Returns:
The indexed value or null if value is null or not a collection or array. null is also returned in case the index does not exist.

getMappedValue

public static Object getMappedValue(Object value,
                                    Object key)
Tries to retrieve the mapped value from the specified object.

Parameters:
value - The object from which to retrieve the mapped value. The object has to be non null and must implement the @{code Map} interface.
key - The map key. index.
Returns:
The mapped value or null if value is null or not implementing @{code Map}.

getDeclaredField

public static Field getDeclaredField(Class<?> clazz,
                                     String fieldName)
Returns the declared field with the specified name or null if it does not exist.

Parameters:
clazz - The class to check.
fieldName - The field name.
Returns:
Returns the declared field with the specified name or null if it does not exist.

containsDeclaredField

public static boolean containsDeclaredField(Class<?> clazz,
                                            String fieldName)
Checks whether the specified class contains a declared field with the given name.

Parameters:
clazz - The class to check.
fieldName - The field name.
Returns:
Returns true if the field exists, false otherwise.

getDeclaredFields

public static Field[] getDeclaredFields(Class<?> clazz)
Returns the fields of the specified class.

Parameters:
clazz - The class for which to retrieve the fields.
Returns:
Returns the fields for this class.

getMethodFromPropertyName

public static Method getMethodFromPropertyName(Class<?> clazz,
                                               String methodName)
Returns the method with the specified property name or null if it does not exist. This method will prepend 'is' and 'get' to the property name and capitalize the first letter.

Parameters:
clazz - The class to check.
methodName - The property name.
Returns:
Returns the method with the specified property or null if it does not exist.

containsMethodWithPropertyName

public static boolean containsMethodWithPropertyName(Class<?> clazz,
                                                     String property)
Checks whether the specified class contains a method for the specified property.

Parameters:
clazz - The class to check.
property - The property name.
Returns:
Returns true if the method exists, false otherwise.

getMethod

public static Method getMethod(Class<?> clazz,
                               String methodName)
Returns the method with the specified name or null if it does not exist.

Parameters:
clazz - The class to check.
methodName - The method name.
Returns:
Returns the method with the specified property or null if it does not exist.

getDeclaredMethod

public static Method getDeclaredMethod(Class<?> clazz,
                                       String methodName,
                                       Class<?>... parameterTypes)
Returns the declared method with the specified name and parameter types or null if it does not exist.

Parameters:
clazz - The class to check.
methodName - The method name.
parameterTypes - The method parameter types.
Returns:
Returns the declared method with the specified name or null if it does not exist.

getDeclaredMethods

public static Method[] getDeclaredMethods(Class<?> clazz)
Returns the declared methods of the specified class.

Parameters:
clazz - The class for which to retrieve the methods.
Returns:
Returns the declared methods for this class.

getMethods

public static Method[] getMethods(Class<?> clazz)
Returns the methods of the specified class (include inherited methods).

Parameters:
clazz - The class for which to retrieve the methods.
Returns:
Returns the methods for this class.

haveSameSignature

public static boolean haveSameSignature(Method method1,
                                        Method method2)
Checks, whether the given methods have the same signature, which is the case if they have the same name, parameter count and types.

Parameters:
method1 - A first method.
method2 - A second method.
Returns:
True, if the methods have the same signature, false otherwise.

boxedType

public static Class<?> boxedType(Type primitiveType)
Returns the autoboxed type of a primitive type.

Parameters:
primitiveType - the primitive type
Returns:
the autoboxed type of a primitive type.
Throws:
IllegalArgumentException - in case the parameter primitiveType does not represent a primitive type.

computeClassHierarchy

public static List<Class<?>> computeClassHierarchy(Class<?> clazz,
                                                   boolean includeInterfaces)
Get all superclasses and optionally interfaces recursively.

Parameters:
clazz - The class to start the search with.
includeInterfaces - whether or not to include interfaces
Returns:
List of all super classes and interfaces of clazz. The list contains the class itself! The empty list is returned if clazz is null.

computeAllImplementedInterfaces

public static Set<Class<?>> computeAllImplementedInterfaces(Class<?> clazz)
Get all interfaces a class directly implements.

Parameters:
clazz - The class for which to find the interfaces
Returns:
Set of all interfaces clazz implements. The empty list is returned if clazz does not implement any interfaces or clazz is null


Copyright © 2007-2011 Red Hat Middleware, LLC. All Rights Reserved