Class ReflectionHelper
- java.lang.Object
-
- org.hibernate.validator.internal.util.ReflectionHelper
-
public final class ReflectionHelper extends Object
Some reflection utility methods. Where necessary calls will be performed asPrivilegedActionwhich is necessary for situations where a security manager is in place.- Author:
- Hardy Ferentschik, Gunnar Morling, Kevin Pollet <kevin.pollet@serli.com> (C) 2011 SERLI, Guillaume Smet
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Class<?>boxedType(Class<?> type)Returns the corresponding auto-boxed type if given a primitive type.static TypeboxedType(Type type)Returns the corresponding auto-boxed type if given a primitive type.static Class<?>getClassFromType(Type type)Converts the givenTypeto aClass.static TypegetCollectionElementType(Type type)Determines the type of the elements of anIterable, array or the value of aMap.static ObjectgetIndexedValue(Object value, int index)Tries to retrieve the indexed value from the specified object.static ObjectgetMappedValue(Object value, Object key)Tries to retrieve the mapped value from the specified object.static ObjectgetValue(Field field, Object object)static ObjectgetValue(Method method, Object object)static booleanisCollection(Type type)Indicates whether the given type represents a collection of elements or not (i.e.static booleanisIndexable(Type type)Indicates if the type is considered indexable (ie is aList, an array or aMap).static booleanisIterable(Type type)static booleanisList(Type type)static booleanisMap(Type type)static TypetypeOf(Member member)static Class<?>unBoxedType(Class<?> type)Returns the primitive type for a boxed type.
-
-
-
Method Detail
-
typeOf
public static Type typeOf(Member member)
- Parameters:
member- TheMemberinstance for which to retrieve the type.- Returns:
- Returns the
Typeof the givenFieldorMethod. - Throws:
IllegalArgumentException- in casememberis not aFieldorMethod.
-
isCollection
public static boolean isCollection(Type type)
Indicates whether the given type represents a collection of elements or not (i.e. whether it is anIterable,Mapor array type).
-
getCollectionElementType
public static Type getCollectionElementType(Type type)
Determines the type of the elements of anIterable, array or the value of aMap.
-
isIndexable
public static boolean isIndexable(Type type)
Indicates if the type is considered indexable (ie is aList, an array or aMap).Note that it does not include
Sets as they are not indexable.- Parameters:
type- the type to inspect.- Returns:
- Returns true if the type is indexable.
-
getClassFromType
public static Class<?> getClassFromType(Type type)
Converts the givenTypeto aClass.- Parameters:
type- the type to convert- Returns:
- the class corresponding to the type
-
isIterable
public static boolean isIterable(Type type)
- Parameters:
type- the type to check.- Returns:
- Returns
trueiftypeis a iterable type,falseotherwise.
-
isMap
public static boolean isMap(Type type)
- Parameters:
type- the type to check.- Returns:
- Returns
trueiftypeis implementingMap,falseotherwise.
-
isList
public static boolean isList(Type type)
- Parameters:
type- the type to check.- Returns:
- Returns
trueiftypeis implementingList,falseotherwise.
-
getIndexedValue
public static Object getIndexedValue(Object value, int 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 nonnulland either a collection or array.index- The index.- Returns:
- The indexed value or
nullifvalueisnullor not a collection or array.nullis 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 nonnulland must implement the @{code Map} interface.key- The map key. index.- Returns:
- The mapped value or
nullifvalueisnullor not implementing @{code Map}.
-
boxedType
public static Type boxedType(Type type)
Returns the corresponding auto-boxed type if given a primitive type. Returns the given type itself otherwise.
-
boxedType
public static Class<?> boxedType(Class<?> type)
Returns the corresponding auto-boxed type if given a primitive type. Returns the given type itself otherwise.
-
unBoxedType
public static Class<?> unBoxedType(Class<?> type)
Returns the primitive type for a boxed type.- Parameters:
type- the boxed type- Returns:
- the primitive type for a auto-boxed type. In case
Voidis passed (which is considered as primitive type byClass.isPrimitive()),Voidwill be returned. - Throws:
IllegalArgumentException- in case the parameterprimitiveTypedoes not represent a primitive type.
-
-