Class 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
    • 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 type ReflectiveOperationException) 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 type ReflectiveOperationException) gives a more detailed indication why the operation failed.
      • 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 - The ClassLoader to 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 object o (which usually is a String) to another type requiredType.
        Type Parameters:
        T - Type of returned object.
        Parameters:
        o - The object to convert.
        requiredType - The desired type o should be converted to.
        context - A String describing the context of o. This is used to produce more meaningful error messages.
        l - The ClassLoader to use.
        packageSearchPath - Search path when looking up classes.
        Returns:
        o converted to requiredType.
        Throws:
        TypeUtil.TypeConversionException
      • computeClasses

        public static Class<?>[] computeClasses​(Class<?> requiredType)
        Computes an array of all super-classes and interfaces of requiredType. 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, B
         
        This will produce the following result for x as requiredType:
         { 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.
      • 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 of c which have both getter and setter methods. If an IntrospectionException is raised when executing the method, then this exception is raised again as an unchecked exception (wrapped in a RuntimeException).
        Parameters:
        c - An arbitrary class.
        Returns:
        An array containing a PropertyDescriptor for each property of c.
      • 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 calling clone() using Java reflection, therefore clone() not necessarily has to be public.
        Type Parameters:
        T - Type of returned object.
        Parameters:
        o - The object to be cloned.
        Returns:
        A clone of o if it was Cloneable, or otherwise the original object.
      • deepCloneArrayIfPossible

        public static <T> T[] deepCloneArrayIfPossible​(T[] array)
        Produces a deep clone of array, i.e., for each element of array creating a clone is attempted using cloneIfPossible(Object).
        Type Parameters:
        T - Component type of the array.
        Parameters:
        array - The array to be cloned.
        Returns:
        A clone of array with each element also cloned.
      • 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 properties jasima.experiment and sun.java.command first. 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 entry Main-Class.

        This code is necessary because Java has no virtual static methods and therefore there is no equivalent to the keyword this in 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 Exceptions InstantiationException and an IllegalAccessException), 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 a RuntimeException
      • rethrowUncheckedException

        public static void rethrowUncheckedException​(Throwable t)
        Throws a throwable in the current Thread (can be either a RuntimeException or an Error).
        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 with requiredParentClass.