Class ReflectionTools

java.lang.Object
de.ppi.deepsampler.persistence.bean.ReflectionTools

public class ReflectionTools
extends java.lang.Object
A collection of utility methods for frequently occurring reflection problems.
  • Method Summary

    Modifier and Type Method Description
    static <T> T createEmptyArray​(java.lang.Object templateArray, java.lang.Class<T> newArrayType)
    Creates an empty array.
    static int[] getArrayDimensions​(java.lang.Object array)
    Finds the dimensions of the array array.
    static java.lang.Class<?> getOriginalClassFromPolymorphicPersistentBean​(PolymorphicPersistentBean polymorphicPersistentBean)
    Retrieves the original type from polymorphicPersistentBean and tries to instantiate a Class for the type.
    static java.lang.reflect.ParameterizedType getParameterizedType​(java.lang.reflect.Type type)
    Casts type to a ParameterizedType if type is a ParameterizedType.
    static <T> java.lang.Class<T> getRawClass​(java.lang.reflect.Type type)
    If type is a ParameterizedType the raw type, that a ParameterizedType contains, is returned.
    static java.lang.Class<?> getRootComponentType​(java.lang.Class<?> array)
    Finds the root component type of the array array.
    static boolean hasPrimitiveTypeParameters​(java.lang.reflect.Type type)
    Checks if type has a generic type parameter which is a primitive or its wrapper like Integer.
    static boolean hasPrimitiveTypeParameters​(java.lang.reflect.Type type, int numberOfParametersToCheck)
    Checks if type has generic type parameters which are a primitive or its wrapper like Integer.
    static boolean isObjectArray​(java.lang.Class<?> cls)
    Checks if cls is an Array with a complex component type.
    static boolean isPrimitiveOrWrapper​(java.lang.Class<?> cls)
    Checks if cls is a primitive type or one of its wrapper types i.e.
    static <T> T parseString​(java.lang.String source, java.lang.Class<T> wrapperType)
    Converts the String source into a primitive wrapper object using the supplied wrapperType.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • isObjectArray

      public static boolean isObjectArray​(java.lang.Class<?> cls)
      Checks if cls is an Array with a complex component type. This is the case if the component type is not a primitive type nor one of its wrapper types. Multidimensional arrays, thus arrays in arrays are also treated as primitive arrays if the component type of the deepest not array type is a primitive.
      Parameters:
      cls - The class of the suspected array
      Returns:
      true if cls is an array with elements that have a Type that is not primitive nor a primitive wrapper.
    • isPrimitiveOrWrapper

      public static boolean isPrimitiveOrWrapper​(java.lang.Class<?> cls)
      Checks if cls is a primitive type or one of its wrapper types i.e. Integer for int.
      Parameters:
      cls - The suspected primitive type
      Returns:
      true it cls is a primitive or a wrapper like int and Integer.
    • hasPrimitiveTypeParameters

      public static boolean hasPrimitiveTypeParameters​(java.lang.reflect.Type type)
      Checks if type has a generic type parameter which is a primitive or its wrapper like Integer. (Real primitives cannot occur here, because primitives are not allowed as generic type parameters.)
      Parameters:
      type - The generic class that should be checked.
      Returns:
      true if cls has a generic type parameter that is a wrapper type.
    • hasPrimitiveTypeParameters

      public static boolean hasPrimitiveTypeParameters​(java.lang.reflect.Type type, int numberOfParametersToCheck)
      Checks if type has generic type parameters which are a primitive or its wrapper like Integer. (Real primitives cannot occur here, because primitives are not allowed as generic type parameters.)
      Parameters:
      type - The generic class that should be checked.
      numberOfParametersToCheck - The number of the first generic parameters that should be checked.
      Returns:
      true if type has generic type parameters that are wrapper types.
    • getRawClass

      public static <T> java.lang.Class<T> getRawClass​(java.lang.reflect.Type type)
      If type is a ParameterizedType the raw type, that a ParameterizedType contains, is returned. If type is a simple Class, type itself is cast to Class and returned.
      Type Parameters:
      T - The type of the Class
      Parameters:
      type - The type for which the Class is wanted.
      Returns:
      If type is a ParameterizedType, the raw Class is returned, otherwise a cast version of type.
    • getParameterizedType

      public static java.lang.reflect.ParameterizedType getParameterizedType​(java.lang.reflect.Type type)
      Casts type to a ParameterizedType if type is a ParameterizedType. Otherwise, null is returned.
      Parameters:
      type - A Type that is expected to be a ParameterizedType
      Returns:
      returns a cast type or null if type is not a ParameterizedType.
    • createEmptyArray

      public static <T> T createEmptyArray​(java.lang.Object templateArray, java.lang.Class<T> newArrayType)
      Creates an empty array. The dimensions of the new array will be the same as of templateArray. The componentType of the new array will be newArrayType.
      Type Parameters:
      T - the componentType of the new array
      Parameters:
      templateArray - A template array that is used as an example for the dimensions of the new array.
      newArrayType - the componentType of the new array
      Returns:
      the new array formed after templateArray and newArrayType.
    • getArrayDimensions

      public static int[] getArrayDimensions​(java.lang.Object array)
      Finds the dimensions of the array array.
      Parameters:
      array - the array of which the dimensions should be retrieved
      Returns:
      the dimensions of the array. Each entry in the returned array is the size of one dimension. The length of the array is the number of dimensions.
    • getRootComponentType

      public static java.lang.Class<?> getRootComponentType​(java.lang.Class<?> array)
      Finds the root component type of the array array. The root component type is the deepest component type that is not an array. This method is useful to retrieve the component type of a multidimensional array, but it also works with 1d arrays.
      Parameters:
      array - the array of which we want to find the component type
      Returns:
      the component type.
    • parseString

      public static <T> T parseString​(java.lang.String source, java.lang.Class<T> wrapperType)
      Converts the String source into a primitive wrapper object using the supplied wrapperType.
      Type Parameters:
      T - The target type.
      Parameters:
      source - The String that should be converted to a wrapperType. The String must be formatted in a way that complies with the parser of the desired wrapper type.
      wrapperType - A wrapper type. This must be a Class of any Wrapper type otherwise an Exception will be thrown.
      Returns:
      returns an instance of wrapperType containing the parsed value of source.
    • getOriginalClassFromPolymorphicPersistentBean

      public static java.lang.Class<?> getOriginalClassFromPolymorphicPersistentBean​(PolymorphicPersistentBean polymorphicPersistentBean)
      Retrieves the original type from polymorphicPersistentBean and tries to instantiate a Class for the type. If the Class cannot be found a PersistenceException is thrown.
      Parameters:
      polymorphicPersistentBean - The PolymorphicPersistentBean which holds the wanted type.
      Returns:
      the class of the original bean that is described by polymorphicPersistentBean.