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 TypeMethodDescriptionprotected static String
fromGetterMethod
(Method aMethod) static boolean
Tests whether we have a getter method.static boolean
Tests whether we have a setter method.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.static Constructor<?>
toConstructor
(Class<Record> aType) Determines the "default" constructor of the provided type.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.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
-
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.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
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.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
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.
-
fromGetterMethod
-