org.hibernate.validation.util
Class ReflectionHelper

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

public class ReflectionHelper
extends java.lang.Object

Some reflection utility methods.

Author:
Hardy Ferentschik

Method Summary
static java.lang.Class<?> boxedTyp(java.lang.reflect.Type primitiveType)
          Returns the autoboxed type of a primitive type.
static java.lang.Class<?> classForName(java.lang.String name, java.lang.Class<?> caller)
           
static boolean containsField(java.lang.Class<?> clazz, java.lang.String fieldName)
          Checks whether the specified class contains a field matching the specified name.
static boolean containsMember(java.lang.Class<?> clazz, java.lang.String property)
          Checks whether the specified class contains a field or property matching the given name.
static boolean containsMethod(java.lang.Class<?> clazz, java.lang.String methodName)
          Checks whether the specified class contains a method matching the specified name.
static
<T> T
getAnnotationParameter(java.lang.annotation.Annotation annotation, java.lang.String parameterName, java.lang.Class<T> type)
           
static java.lang.Class<?> getAnnotations(java.lang.reflect.Member member)
          Returns the type of the field of return type of a method.
static java.lang.reflect.Field getField(java.lang.Class<?> clazz, java.lang.String fieldName)
          Returns the field with the specified name or null if it does not exist.
static java.lang.reflect.Type getIndexedType(java.lang.reflect.Type type)
          Determines the type of elements of an Iterable, array or the value of a Map.
static java.lang.Object getIndexedValue(java.lang.Object value, java.lang.Integer index)
          Tries to retrieve the indexed value from the specified object.
static java.lang.Object getMappedValue(java.lang.Object value, java.lang.Object key)
          Tries to retrieve the mapped value from the specified object.
static java.lang.reflect.Method getMethod(java.lang.Class<?> clazz, java.lang.String methodName)
          Returns the method with the specified name or null if it does not exist.
static java.lang.String getPropertyName(java.lang.reflect.Member member)
          Process bean properties getter by applying the JavaBean naming conventions.
static java.lang.Class<?> getType(java.lang.reflect.Member member)
          Returns the type of the field of return type of a method.
static java.lang.Object getValue(java.lang.reflect.Member member, java.lang.Object object)
           
static boolean isIterable(java.lang.reflect.Type type)
           
static boolean isList(java.lang.reflect.Type type)
           
static boolean isMap(java.lang.reflect.Type type)
           
static java.lang.Class<?> loadClass(java.lang.String name, java.lang.Class<?> caller)
           
static void setAccessibility(java.lang.reflect.Member member)
           
static java.lang.reflect.Type typeOf(java.lang.reflect.Member member)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getAnnotationParameter

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

getPropertyName

public static java.lang.String getPropertyName(java.lang.reflect.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.

getType

public static java.lang.Class<?> getType(java.lang.reflect.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.

getAnnotations

public static java.lang.Class<?> getAnnotations(java.lang.reflect.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 java.lang.reflect.Type typeOf(java.lang.reflect.Member member)
Parameters:
member - The Member instance for which to retrieve the type.
Returns:
Retrurns the Type of the given Field or Method.
Throws:
java.lang.IllegalArgumentException - in case member is not a Field or Method.

getValue

public static java.lang.Object getValue(java.lang.reflect.Member member,
                                        java.lang.Object object)

setAccessibility

public static void setAccessibility(java.lang.reflect.Member member)

loadClass

public static java.lang.Class<?> loadClass(java.lang.String name,
                                           java.lang.Class<?> caller)
                                    throws java.lang.ClassNotFoundException
Throws:
java.lang.ClassNotFoundException

getIndexedType

public static java.lang.reflect.Type getIndexedType(java.lang.reflect.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(java.lang.reflect.Type type)
Parameters:
type - the type to check.
Returns:
Returns true if type is a iterable type, false otherwise.

isMap

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

isList

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

getIndexedValue

public static java.lang.Object getIndexedValue(java.lang.Object value,
                                               java.lang.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 java.lang.Object getMappedValue(java.lang.Object value,
                                              java.lang.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}.

containsMember

public static boolean containsMember(java.lang.Class<?> clazz,
                                     java.lang.String property)
Checks whether the specified class contains a field or property matching the given name.

Parameters:
clazz - The class to check.
property - The property name.
Returns:
Returns true if the cass contains a field or member for the specified property, false otherwise.

containsField

public static boolean containsField(java.lang.Class<?> clazz,
                                    java.lang.String fieldName)
Checks whether the specified class contains a field matching the specified name.

Parameters:
clazz - The class to check.
fieldName - The field name.
Returns:
Returns true if the cass contains a field for the specified name, false otherwise.

getField

public static java.lang.reflect.Field getField(java.lang.Class<?> clazz,
                                               java.lang.String fieldName)
Returns the 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 field with the specified name or null if it does not exist.

containsMethod

public static boolean containsMethod(java.lang.Class<?> clazz,
                                     java.lang.String methodName)
Checks whether the specified class contains a method matching the specified name.

Parameters:
clazz - The class to check.
methodName - The method name.
Returns:
Returns true if the cass contains a property for the specified name, false otherwise.

getMethod

public static java.lang.reflect.Method getMethod(java.lang.Class<?> clazz,
                                                 java.lang.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 name or null if it does not exist.

classForName

public static java.lang.Class<?> classForName(java.lang.String name,
                                              java.lang.Class<?> caller)
                                       throws java.lang.ClassNotFoundException
Throws:
java.lang.ClassNotFoundException

boxedTyp

public static java.lang.Class<?> boxedTyp(java.lang.reflect.Type primitiveType)
Returns the autoboxed type of a primitive type.

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


Copyright © 2007-2009. All Rights Reserved.