java.lang.Object
org.refcodes.struct.TypeUtility
The
TypeUtility
uses reflection to fiddle around with primitives,
types, classes, instances and the like.-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Extension of theNoSuchMethodException
with an additional aCauseTypeUtility.NoSuchMethodNotInvokedException.getCause()
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
Tests whether we have a getter method.static boolean
Tests whether we have a setter method.static <T> T[]
toArray
(Collection<T> aCollection) Converts a genericCollection
's elements into an array which's elements are assignment-compliant to the collection's generic type.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.toCommonAncestors
(Collection<Class<?>> aTypes) Determines the common ancestors for an array of types.static Constructor<?>
toConstructor
(Class<Record> aType) Determines the "default" constructor of the provided type.static <T> Class<T>
toElelemtType
(Collection<T> aCollection) Determines the collection's elements' least common type.protected static String
toGetterPropertyName
(Method aMethod) Retrieves the Java Bean property name from the providedMethod
instance.static <T> void
toInstance
(Object aValue, T aInstance) Updates the provided instance with the data provided by the given value (a mixture of array andMap
objects).toMap
(T aInstance) This method creates aMap
with the attributes found in the given instance.The values of the attributes are not introspected any more!static String[]
toProperties
(Class<?> aType) Retrieves all properties detected in the provided type.static String
toPropertyName
(Field aField) Converts a field's name to a property name.static String
toPropertyName
(Method aMethod) Converts a method's name to a property name.toSuperTypes
(Class<?> aType) Determines the super types (class and interfaces) of a given type (including itself).static <T> T
Creates an instance of the given type filled with the data provided by the given Data-Structure (a mixture of array andMap
objects).
-
Method Details
-
toArray
Converts a genericCollection
'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 containsnull
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
- TheCollection
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
Determines the collection's elements' least common type. Attention: Use only when you have a direct type hierarchy as ambiguous type hierarchies may causeClassCastException
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
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
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
public static <T> T toType(Object aValue, Class<T> aType) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException Creates an instance of the given type filled with the data provided by the given Data-Structure (a mixture of array andMap
objects).- Type Parameters:
T
- the generic type- Parameters:
aValue
- The Data-Structure, a mixture of arrays andMap
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 classClass
, 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
public static <T> void toInstance(Object aValue, T aInstance) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException Updates the provided instance with the data provided by the given value (a mixture of array andMap
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 andMap
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 classClass
, 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
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
- TheMethod
to be tested.- Returns:
- True in case we have a setter method, else false.
-
isGetter
Tests whether we have a getter method. A getter method must return a value (other thanVoid
) and expect none arguments. It also must be public.- Parameters:
aMethod
- TheMethod
to be tested.- Returns:
- True in case we have a getter method, else false.
-
toPropertyName
Converts a method's name to a property name.- Parameters:
aMethod
- TheMethod
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
Converts a field's name to a property name.- Parameters:
aField
- TheField
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
Retrieves all properties detected in the provided type.- Parameters:
aType
- The type from which to determine the properties.- Returns:
- The according detected properties.
-
toArrayType
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
This method creates aMap
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
Determines the "default" constructor of the provided type. The arguments order of the constructor match theRecordComponent
order as ofClass.getRecordComponents()
.- Parameters:
aType
- The type for which to retrieve the constructor.- Returns:
- The according constructor or null if none matching was found.
-
toGetterPropertyName
Retrieves the Java Bean property name from the providedMethod
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.
-