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.
  • Method Details

    • toArray

      public static <T> T[] toArray(Collection<T> aCollection)
      Converts a generic Collection's elements into an array which's elements are assignment-compliant to the collection's generic type. In case the collection is empty or merely contains null values, then null is returned as no type can be inferred from any elements inside the collection.
      Type Parameters:
      T - The type of the collection.
      Parameters:
      aCollection - The Collection to convert.
      Returns:
      The array which's elements are assignment-compliant to the collection's generic type or null if the collection was empty or merely contains null values.
    • toElelemtType

      public static <T> Class<T> toElelemtType(Collection<T> aCollection)
      Determines the collection's elements' least common type. Attention: Use only when you have a direct type hierarchy as ambiguous type hierarchies may cause ClassCastException exceptions to be thrown in your code!
      Type Parameters:
      T - The generic type of the collection.
      Parameters:
      aCollection - The collection for which to determine the element's generic type.
      Returns:
      The generic type as of best knowledge or null if it cannot be determined.
    • toCommonAncestors

      public static List<Class<?>> toCommonAncestors(Collection<Class<?>> aTypes)
      Determines the common ancestors for an array of types.
      Parameters:
      aTypes - The classes for which to determine the common ancestors.
      Returns:
      A List with the according common ancestors.
    • toSuperTypes

      public static Set<Class<?>> toSuperTypes(Class<?> aType)
      Determines the super types (class and interfaces) of a given type (including itself).
      Parameters:
      aType - The type for which to determine all super types.
      Returns:
      The according super types (including itself).
    • 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.
      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.
      SecurityException - thrown by the security manager to indicate a security violation.
    • 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.
      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.
      SecurityException - thrown by the security manager to indicate a security violation.
    • 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)
      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.
    • toGetterPropertyName

      protected static String toGetterPropertyName(Method aMethod)
      Retrieves the Java Bean property name from the provided Method instance.
      Parameters:
      aMethod - The method from which to retrieve the Java Bean property name.
      Returns:
      the Java property name or null if there is no such getter.