Class TypeUtility

java.lang.Object
org.refcodes.struct.TypeUtility

public class TypeUtility extends Object
The TypeUtility uses reflection to fiddle around with primitives, types, classes, instances and the like.
  • Constructor Details

    • TypeUtility

      public TypeUtility()
  • Method Details

    • toType

      Creates an instance of the given type filled with the data provided by the given Data-Structure (a mixture of array and Map objects).
      Type Parameters:
      T - the generic type
      Parameters:
      aValue - The Data-Structure, a mixture of arrays and Map instance, from which to construct the instance of the given type.
      aType - The type for which an instance is to be created.
      Returns:
      The instance filled by the data from the given Data-Structure
      Throws:
      InstantiationException - thrown when an application tries to create an instance of a class using the newInstance method in class Class, but the specified class object cannot be instantiated.
      IllegalAccessException - thrown when an application tries to reflectively create an instance (other than an array), set or get a field, or invoke a method, but the currently executing method does not have access to the definition of the specified class, field, method or constructor.
      NoSuchMethodException - thrown when a particular method cannot be found.
      SecurityException - thrown by the security manager to indicate a security violation.
      InvocationTargetException - wraps an exception thrown by an invoked method or constructor.
      ClassNotFoundException - thrown when an application tries to load in a class through its string name but no definition for the class with the specified name could be found.
    • toInstance

      Updates the provided instance with the data provided by the given value (a mixture of array and Map objects). In case the instance to be updated represents an array, then at most it is filled with the provided value's elements, even if the value provides more elements than would fit. On the other hand, if the instance's array to be updated provides room for more elements than the value can provide, then the remaining instance's elements are left as are.
      Type Parameters:
      T - the generic type
      Parameters:
      aValue - The Data-Structure, a mixture of arrays and Map instance, from which to update the instance of the given type.
      aInstance - The instance to be updated.
      Throws:
      InstantiationException - thrown when an application tries to create an instance of a class using the newInstance method in class Class, but the specified class object cannot be instantiated.
      IllegalAccessException - thrown when an application tries to reflectively create an instance (other than an array), set or get a field, or invoke a method, but the currently executing method does not have access to the definition of the specified class, field, method or constructor.
      NoSuchMethodException - thrown when a particular method cannot be found.
      SecurityException - thrown by the security manager to indicate a security violation.
      InvocationTargetException - wraps an exception thrown by an invoked method or constructor.
      ClassNotFoundException - thrown when an application tries to load in a class through its string name but no definition for the class with the specified name could be found.
    • isSetter

      public static boolean isSetter(Method aMethod)
      Tests whether we have a setter method. A setter method must not return a value and expect exactly one argument. It also must be public.
      Parameters:
      aMethod - The Method to be tested.
      Returns:
      True in case we have a setter method, else false.
    • isGetter

      public static boolean isGetter(Method aMethod)
      Tests whether we have a getter method. A getter method must return a value (other than Void) and expect none arguments. It also must be public.
      Parameters:
      aMethod - The Method to be tested.
      Returns:
      True in case we have a getter method, else false.
    • toPropertyName

      public static String toPropertyName(Method aMethod)
      Converts a method's name to a property name.
      Parameters:
      aMethod - The Method which's name is to be converted to a property name.
      Returns:
      The according property name or null if we do not have a property method.
    • toPropertyName

      public static String toPropertyName(Field aField)
      Converts a field's name to a property name.
      Parameters:
      aField - The Field which's name is to be converted to a property name.
      Returns:
      The according property name or null if we do not have a property method.
    • toProperties

      public static String[] toProperties(Class<?> aType)
      Retrieves all properties detected in the provided type.
      Parameters:
      aType - The type from which to determine the properties.
      Returns:
      The according detected properties.
    • toArrayType

      public static <T> T toArrayType(Object aValue, Class<T> aType) throws IllegalArgumentException
      Converts the provided object reference (which must point to the according array type) to the required array type.
      Type Parameters:
      T - The type of the array to which to convert to.
      Parameters:
      aValue - The object reference which to convert.
      aType - The type to which to convert to.
      Returns:
      The according array type.
      Throws:
      IllegalArgumentException - thrown in case there were reflection or introspection problems when converting the object to the required array type.
    • toMap

      public static <T> Map<String,Object> toMap(T aInstance)
      This method creates a Map with the attributes found in the given instance.The values of the attributes are not introspected any more!
      Type Parameters:
      T - The type of the instance to be introspected.
      Parameters:
      aInstance - The instance from which to get the attributes.
      Returns:
      The Map containing the attributes of the provided instance, the keys being the attribute names, the values being the attribute values
    • toConstructor

      public static Constructor<?> toConstructor(Class<Record> aType)
      Determines the "default" constructor of the provided type. The arguments order of the constructor match the RecordComponent order as of Class.getRecordComponents().
      Parameters:
      aType - The type for which to retrieve the constructor.
      Returns:
      The according constructor or null if none matching was found.
    • fromGetterMethod

      protected static String fromGetterMethod(Method aMethod)