Package jasima.core.util
Class TypeUtil
- java.lang.Object
-
- jasima.core.util.TypeUtil
-
public class TypeUtil extends Object
This class contains a collection of methods concerned with reading/writing/creating/converting Bean properties and loading classes using Java reflection.- Author:
- Torsten Hildebrandt
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTypeUtil.TypeConversionExceptionATypeConversionExceptionis thrown, when the conversion between types fails.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> TcloneIfPossible(T o)This method returns a clone of an object, if this object is cloneable.static Class<?>[]computeClasses(Class<?> requiredType)Computes an array of all super-classes and interfaces ofrequiredType.static <T> Tconvert(Object o, Class<T> requiredType, String context, ClassLoader l, String[] packageSearchPath)Converts an objecto(which usually is aString) to another typerequiredType.static <T> TcreateInstance(Class<T> clazz)Creates an instance of the class passed as a parameter using its parameter-less constructor.static boolean[]deepCloneArrayIfPossible(boolean[] array)static byte[]deepCloneArrayIfPossible(byte[] array)static char[]deepCloneArrayIfPossible(char[] array)static double[]deepCloneArrayIfPossible(double[] array)static float[]deepCloneArrayIfPossible(float[] array)static int[]deepCloneArrayIfPossible(int[] array)static long[]deepCloneArrayIfPossible(long[] array)static short[]deepCloneArrayIfPossible(short[] array)static <T> T[]deepCloneArrayIfPossible(T[] array)Produces a deep clone ofarray, i.e., for each element ofarraycreating a clone is attempted usingcloneIfPossible(Object).static PropertyDescriptor[]findWritableProperties(Class<?> c)Finds (bean) properties ofcwhich have both getter and setter methods.static PropertyDescriptor[]findWritableProperties(Object o)Finds (bean) properties ofowhich have both getter and setter methods.static <E> Class<? extends E>getClassFromSystemProperty(String propertyName, Class<E> clazz, Class<? extends E> defaultClass)static Class<?>getMainClass()Tries to find the main class of a java run.static Class<?>getPropertyType(Object o, String propPath)Determines the type of a property named with propPath using reflection.static ObjectgetPropertyValue(Object o, String propPath)Gets the current value of a property named with propPath using reflection.static Map<String,Object>getPropertyValues(Object o, PropertyDescriptor[] pds)Returns a map with all values of the given properties. .static <T> Class<? extends T>loadClass(String className, Class<T> requiredParentClass)Loads a class of the given name using the context class loader.static voidrethrowUncheckedException(Throwable t)Throws a throwable in the current Thread (can be either aRuntimeExceptionor anError).static voidsetPropertyValue(Object o, String propPath, Object value)CallssetPropertyValue(Object, String, Object, ClassLoader, String[])using the ClassLoader that was used to loadTypeUtiland the default package search pathUtil.DEF_CLASS_SEARCH_PATH.static voidsetPropertyValue(Object o, String propPath, Object value, ClassLoader loader, String[] packageSearchPath)Sets a property named with propPath to a certain value using reflection.static Map<String,PropertyDescriptor>writableProperties(Class<?> c)Returns a map of property descriptors.
-
-
-
Method Detail
-
getPropertyType
public static Class<?> getPropertyType(Object o, String propPath) throws RuntimeException
Determines the type of a property named with propPath using reflection.This method interprets propPath the same way as
getPropertyValue(Object, String)does.- Parameters:
o- The Object from which to get a property value.propPath- A String containing the path to the required property.- Returns:
- The property's type.
- Throws:
RuntimeException- If there was a problem getting the value. The cause of this exception (of typeReflectiveOperationException) gives a more detailed indication why the operation failed.
-
getPropertyValue
public static Object getPropertyValue(Object o, String propPath) throws RuntimeException
Gets the current value of a property named with propPath using reflection.Example: getProperty( obj, "a.b.c" ); is equivalent to a direct call obj.getA().getB().getC()
- Parameters:
o- The Object from which to get a property value.propPath- A String containing the path to the required property.- Returns:
- The property value.
- Throws:
RuntimeException- If there was a problem getting the value. The cause of this exception (of typeReflectiveOperationException) gives a more detailed indication why the operation failed.
-
setPropertyValue
public static void setPropertyValue(Object o, String propPath, Object value)
CallssetPropertyValue(Object, String, Object, ClassLoader, String[])using the ClassLoader that was used to loadTypeUtiland the default package search pathUtil.DEF_CLASS_SEARCH_PATH.- Parameters:
o- The object with a property to set.propPath- The property path and name of the property to set.value- The value to set the property to.- See Also:
setPropertyValue(Object, String, Object, ClassLoader, String[])
-
setPropertyValue
public static void setPropertyValue(Object o, String propPath, Object value, ClassLoader loader, String[] packageSearchPath) throws IllegalArgumentException
Sets a property named with propPath to a certain value using reflection.Example: setProperty( obj, "a.b.c", 5 ); is equivalent to a direct call obj.getA().getB().setC(5)
- Parameters:
o- The object with a property to set.propPath- The property path and name of the property to set.value- The value to set the property to.loader- TheClassLoaderto use when new classes have to be loaded.packageSearchPath- A list of package names that are used to complete abbreviated class names.- Throws:
IllegalArgumentException
-
convert
public static <T> T convert(Object o, Class<T> requiredType, String context, ClassLoader l, String[] packageSearchPath) throws TypeUtil.TypeConversionException
Converts an objecto(which usually is aString) to another typerequiredType.- Type Parameters:
T- Type of returned object.- Parameters:
o- The object to convert.requiredType- The desired typeoshould be converted to.context- A String describing the context ofo. This is used to produce more meaningful error messages.l- TheClassLoaderto use.packageSearchPath- Search path when looking up classes.- Returns:
oconverted torequiredType.- Throws:
TypeUtil.TypeConversionException
-
computeClasses
public static Class<?>[] computeClasses(Class<?> requiredType)
Computes an array of all super-classes and interfaces ofrequiredType. This method performs a breadth first traversal of the class/interface hierarchy. Consider the following example:interface A extends M, N interface B extends O class Y implements C, D class X extends Y implements A, BThis will produce the following result forxasrequiredType:{ X, Y, A, B, C, D, M, N, O, Object }- Parameters:
requiredType- The class for which to compute the type hierarchy.- Returns:
- A list of super classes/interfaces from most to least specific.
-
findWritableProperties
public static PropertyDescriptor[] findWritableProperties(Object o)
Finds (bean) properties ofowhich have both getter and setter methods.- Parameters:
o- An arbitrary object.- Returns:
- An array containing a
PropertyDescriptorfor each property ofo. - See Also:
findWritableProperties(Class)
-
getPropertyValues
public static Map<String,Object> getPropertyValues(Object o, PropertyDescriptor[] pds)
Returns a map with all values of the given properties. .- Parameters:
o- The object to get the values from.pds- The properties to use.- Returns:
- A map containing the names and values of the given properties.
-
findWritableProperties
public static PropertyDescriptor[] findWritableProperties(Class<?> c)
Finds (bean) properties ofcwhich have both getter and setter methods. If anIntrospectionExceptionis raised when executing the method, then this exception is raised again as an unchecked exception (wrapped in aRuntimeException).- Parameters:
c- An arbitrary class.- Returns:
- An array containing a
PropertyDescriptorfor each property ofc.
-
writableProperties
public static Map<String,PropertyDescriptor> writableProperties(Class<?> c)
Returns a map of property descriptors. Keys in this map are the property names converted to lower case.- Parameters:
c- The class for which to find the properties.- Returns:
- A map associating a property name (converted to lower case) with a
PropertyDescriptor.
-
cloneIfPossible
public static <T> T cloneIfPossible(T o)
This method returns a clone of an object, if this object is cloneable. The clone is created by callingclone()using Java reflection, thereforeclone()not necessarily has to be public.- Type Parameters:
T- Type of returned object.- Parameters:
o- The object to be cloned.- Returns:
- A clone of
oif it wasCloneable, or otherwise the original object.
-
deepCloneArrayIfPossible
public static <T> T[] deepCloneArrayIfPossible(T[] array)
Produces a deep clone ofarray, i.e., for each element ofarraycreating a clone is attempted usingcloneIfPossible(Object).- Type Parameters:
T- Component type of the array.- Parameters:
array- The array to be cloned.- Returns:
- A clone of
arraywith each element also cloned.
-
deepCloneArrayIfPossible
public static int[] deepCloneArrayIfPossible(int[] array)
- See Also:
deepCloneArrayIfPossible(Object[])
-
deepCloneArrayIfPossible
public static long[] deepCloneArrayIfPossible(long[] array)
- See Also:
deepCloneArrayIfPossible(Object[])
-
deepCloneArrayIfPossible
public static byte[] deepCloneArrayIfPossible(byte[] array)
- See Also:
deepCloneArrayIfPossible(Object[])
-
deepCloneArrayIfPossible
public static short[] deepCloneArrayIfPossible(short[] array)
- See Also:
deepCloneArrayIfPossible(Object[])
-
deepCloneArrayIfPossible
public static char[] deepCloneArrayIfPossible(char[] array)
- See Also:
deepCloneArrayIfPossible(Object[])
-
deepCloneArrayIfPossible
public static boolean[] deepCloneArrayIfPossible(boolean[] array)
- See Also:
deepCloneArrayIfPossible(Object[])
-
deepCloneArrayIfPossible
public static float[] deepCloneArrayIfPossible(float[] array)
- See Also:
deepCloneArrayIfPossible(Object[])
-
deepCloneArrayIfPossible
public static double[] deepCloneArrayIfPossible(double[] array)
- See Also:
deepCloneArrayIfPossible(Object[])
-
getMainClass
public static Class<?> getMainClass() throws ClassNotFoundException
Tries to find the main class of a java run. This is attempted by looking up the system propertiesjasima.experimentandsun.java.commandfirst. If this does not lead to a valid classname (e.g., if started with "-jar" option) an attempt is made to interpret the property as the name of a jar file. The manifest of this jar is then searched for its entryMain-Class.This code is necessary because Java has no virtual static methods and therefore there is no equivalent to the keyword
thisin a static method.- Throws:
ClassNotFoundException- If there were problems locating the main class. Should not occur.
-
getClassFromSystemProperty
public static <E> Class<? extends E> getClassFromSystemProperty(String propertyName, Class<E> clazz, Class<? extends E> defaultClass)
-
createInstance
public static <T> T createInstance(Class<T> clazz)
Creates an instance of the class passed as a parameter using its parameter-less constructor. Should any Exception be triggered creating the instance (Class#newInstance()can throw the checked ExceptionsInstantiationExceptionand anIllegalAccessException), then it is rethrown as an unchecked exception wrapped in a RuntimeException.- Type Parameters:
T- Type of the object to create- Parameters:
clazz- Class of the object to create (mustn't be null)- Returns:
- The new instance
- Throws:
RuntimeException- If any Exception occurs when creating the instance, it will be rethrown wrapped in aRuntimeException
-
rethrowUncheckedException
public static void rethrowUncheckedException(Throwable t)
Throws a throwable in the current Thread (can be either aRuntimeExceptionor anError).- Parameters:
t- The unchecked exception to throw.
-
loadClass
public static <T> Class<? extends T> loadClass(String className, Class<T> requiredParentClass) throws ClassNotFoundException, ClassCastException
Loads a class of the given name using the context class loader.- Type Parameters:
T- Required type of the class.- Parameters:
className- Name of the class.requiredParentClass- Type token for the class or interface that has to be implemented.- Returns:
- The loaded class.
- Throws:
ClassNotFoundException- If no class of the given name could be found.ClassCastException- If a matching class was found, but was not compatible withrequiredParentClass.
-
-