- java.lang.Object
-
- org.refcodes.struct.TypeUtility
-
public class TypeUtility extends Object
The Class TypeUtility.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTypeUtility.NoSuchMethodNotInvokedExceptionExtension of theNoSuchMethodExceptionwith an additional causeTypeUtility.NoSuchMethodNotInvokedException.getCause()
-
Constructor Summary
Constructors Constructor Description TypeUtility()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description protected static StringfromGetterMethod(Method aMethod)static booleanisGetter(Method aMethod)Tests whether we have a getter method.static booleanisSetter(Method aMethod)Tests whether we have a setter method.static <T> TtoArrayType(Object aValue, Class<T> aType)Converts the provided object reference (which must point to the according array type) to the required array type.static <T> voidtoInstance(Object aValue, T aInstance)Updates the provided instance with the data provided by the given value (a mixture of array andMapobjects).static <T> Map<String,Object>toMap(T aInstance)This method creates aMapwith the attributes found in the given instance.The values of the attributes are not introspected any more!static StringtoPropertyName(Field aField)Converts a field's name to a property name.static StringtoPropertyName(Method aMethod)Converts a method's name to a property name.static <T> TtoType(Object aValue, Class<T> aType)Creates an instance of the given type filled with the data provided by the given Data-Structure (a mixture of array andMapobjects).
-
-
-
Method Detail
-
toType
public static <T> T toType(Object aValue, Class<T> aType) throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, InvocationTargetException, ClassNotFoundException
Creates an instance of the given type filled with the data provided by the given Data-Structure (a mixture of array andMapobjects).- Type Parameters:
T- the generic type- Parameters:
aValue- The Data-Structure, a mixture of arrays andMapinstance, 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, SecurityException, InvocationTargetException, ClassNotFoundException
Updates the provided instance with the data provided by the given value (a mixture of array andMapobjects). 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 andMapinstance, 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
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- TheMethodto 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 thanVoid) and expect none arguments. It also must be public.- Parameters:
aMethod- TheMethodto 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- TheMethodwhich'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- TheFieldwhich'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.
-
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 aMapwith 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
Mapcontaining the attributes of the provided instance, the keys being the attribute names, the values being the attribute values
-
-