Class 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 <kevin.pollet@serli.com> (C) 2011 SERLI, Guillaume Smet
    • Method Detail

      • 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.
      • isCollection

        public static boolean isCollection​(Type type)
        Indicates whether the given type represents a collection of elements or not (i.e. whether it is an Iterable, Map or array type).
      • getCollectionElementType

        public static Type getCollectionElementType​(Type type)
        Determines the type of the elements of an Iterable, array or the value of a Map.
      • isIndexable

        public static boolean isIndexable​(Type type)
        Indicates if the type is considered indexable (ie is a List, an array or a Map).

        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 given Type to a Class.
        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 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,
                                             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 non null and either a collection or array.
        index - The index.
        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}.
      • 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 Void is passed (which is considered as primitive type by Class.isPrimitive()), Void will be returned.
        Throws:
        IllegalArgumentException - in case the parameter primitiveType does not represent a primitive type.