Class ObjectUtilities


  • public final class ObjectUtilities
    extends java.lang.Object
    A collection of useful static utility methods for handling classes and object instantiation.
    Author:
    Thomas Morgner
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String CLASS_CONTEXT
      A constant for using the ClassContext as source for the classloader.
      static java.lang.String THREAD_CONTEXT
      A constant for using the TheadContext as source for the classloader.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Object clone​(java.lang.Object object)
      Returns a clone of the specified object, if it can be cloned, otherwise throws a CloneNotSupportedException.
      static java.util.Collection deepClone​(java.util.Collection collection)
      Returns a new collection containing clones of all the items in the specified collection.
      static boolean equal​(java.lang.Object o1, java.lang.Object o2)
      Returns true if the two objects are equal OR both null.
      static java.lang.ClassLoader getClassLoader()
      Returns the custom classloader or null, if no custom classloader is defined.
      static java.lang.ClassLoader getClassLoader​(java.lang.Class c)
      Returns the classloader, which was responsible for loading the given class.
      static java.lang.String getClassLoaderSource()
      Returns the internal configuration entry, whether the classloader of the thread context or the context classloader should be used.
      static java.net.URL getResource​(java.lang.String name, java.lang.Class c)
      Returns the resource specified by the absolute name.
      static java.io.InputStream getResourceAsStream​(java.lang.String name, java.lang.Class context)
      Returns the inputstream for the resource specified by the absolute name.
      static java.net.URL getResourceRelative​(java.lang.String name, java.lang.Class c)
      Returns the resource specified by the relative name.
      static java.io.InputStream getResourceRelativeAsStream​(java.lang.String name, java.lang.Class context)
      Returns the inputstream for the resource specified by the relative name.
      static int hashCode​(java.lang.Object object)
      Returns a hash code for an object, or zero if the object is null.
      static boolean isJDK14()
      Returns true if this is version 1.4 or later of the Java runtime.
      static java.lang.Object loadAndInstantiate​(java.lang.String className, java.lang.Class source)
      Tries to create a new instance of the given class.
      static java.lang.Object loadAndInstantiate​(java.lang.String className, java.lang.Class source, java.lang.Class type)
      Tries to create a new instance of the given class.
      static void setClassLoader​(java.lang.ClassLoader classLoader)
      Redefines the custom classloader.
      static void setClassLoaderSource​(java.lang.String classLoaderSource)
      Defines the internal configuration entry, whether the classloader of the thread context or the context classloader should be used.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getClassLoaderSource

        public static java.lang.String getClassLoaderSource()
        Returns the internal configuration entry, whether the classloader of the thread context or the context classloader should be used.
        Returns:
        the classloader source, either THREAD_CONTEXT or CLASS_CONTEXT.
      • setClassLoaderSource

        public static void setClassLoaderSource​(java.lang.String classLoaderSource)
        Defines the internal configuration entry, whether the classloader of the thread context or the context classloader should be used.

        This setting can only be defined using the API, there is no safe way to put this into an external configuration file.

        Parameters:
        classLoaderSource - the classloader source, either THREAD_CONTEXT or CLASS_CONTEXT.
      • equal

        public static boolean equal​(java.lang.Object o1,
                                    java.lang.Object o2)
        Returns true if the two objects are equal OR both null.
        Parameters:
        o1 - object 1 (null permitted).
        o2 - object 2 (null permitted).
        Returns:
        true or false.
      • hashCode

        public static int hashCode​(java.lang.Object object)
        Returns a hash code for an object, or zero if the object is null.
        Parameters:
        object - the object (null permitted).
        Returns:
        The object's hash code (or zero if the object is null).
      • clone

        public static java.lang.Object clone​(java.lang.Object object)
                                      throws java.lang.CloneNotSupportedException
        Returns a clone of the specified object, if it can be cloned, otherwise throws a CloneNotSupportedException.
        Parameters:
        object - the object to clone (null not permitted).
        Returns:
        A clone of the specified object.
        Throws:
        java.lang.CloneNotSupportedException - if the object cannot be cloned.
      • deepClone

        public static java.util.Collection deepClone​(java.util.Collection collection)
                                              throws java.lang.CloneNotSupportedException
        Returns a new collection containing clones of all the items in the specified collection.
        Parameters:
        collection - the collection (null not permitted).
        Returns:
        A new collection containing clones of all the items in the specified collection.
        Throws:
        java.lang.CloneNotSupportedException - if any of the items in the collection cannot be cloned.
      • setClassLoader

        public static void setClassLoader​(java.lang.ClassLoader classLoader)
        Redefines the custom classloader.
        Parameters:
        classLoader - the new classloader or null to use the default.
      • getClassLoader

        public static java.lang.ClassLoader getClassLoader()
        Returns the custom classloader or null, if no custom classloader is defined.
        Returns:
        the custom classloader or null to use the default.
      • getClassLoader

        public static java.lang.ClassLoader getClassLoader​(java.lang.Class c)
        Returns the classloader, which was responsible for loading the given class.
        Parameters:
        c - the classloader, either an application class loader or the boot loader.
        Returns:
        the classloader, never null.
        Throws:
        java.lang.SecurityException - if the SecurityManager does not allow to grab the context classloader.
      • getResource

        public static java.net.URL getResource​(java.lang.String name,
                                               java.lang.Class c)
        Returns the resource specified by the absolute name.
        Parameters:
        name - the name of the resource
        c - the source class
        Returns:
        the url of the resource or null, if not found.
      • getResourceRelative

        public static java.net.URL getResourceRelative​(java.lang.String name,
                                                       java.lang.Class c)
        Returns the resource specified by the relative name.
        Parameters:
        name - the name of the resource relative to the given class
        c - the source class
        Returns:
        the url of the resource or null, if not found.
      • getResourceAsStream

        public static java.io.InputStream getResourceAsStream​(java.lang.String name,
                                                              java.lang.Class context)
        Returns the inputstream for the resource specified by the absolute name.
        Parameters:
        name - the name of the resource
        context - the source class
        Returns:
        the url of the resource or null, if not found.
      • getResourceRelativeAsStream

        public static java.io.InputStream getResourceRelativeAsStream​(java.lang.String name,
                                                                      java.lang.Class context)
        Returns the inputstream for the resource specified by the relative name.
        Parameters:
        name - the name of the resource relative to the given class
        context - the source class
        Returns:
        the url of the resource or null, if not found.
      • loadAndInstantiate

        public static java.lang.Object loadAndInstantiate​(java.lang.String className,
                                                          java.lang.Class source)
        Tries to create a new instance of the given class. This is a short cut for the common bean instantiation code.
        Parameters:
        className - the class name as String, never null.
        source - the source class, from where to get the classloader.
        Returns:
        the instantiated object or null, if an error occured.
      • loadAndInstantiate

        public static java.lang.Object loadAndInstantiate​(java.lang.String className,
                                                          java.lang.Class source,
                                                          java.lang.Class type)
        Tries to create a new instance of the given class. This is a short cut for the common bean instantiation code. This method is a type-safe method and will not instantiate the class unless it is an instance of the given type.
        Parameters:
        className - the class name as String, never null.
        source - the source class, from where to get the classloader.
        type - the type.
        Returns:
        the instantiated object or null, if an error occurred.
      • isJDK14

        public static boolean isJDK14()
        Returns true if this is version 1.4 or later of the Java runtime.
        Returns:
        A boolean.