Class Loader


  • public final class Loader
    extends java.lang.Object
    Load resources (or images) from various sources.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static Logger LOGGER  
      private static java.lang.String TSTR  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Loader()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.ClassLoader getClassLoader()
      Returns the ClassLoader to use.
      static java.lang.ClassLoader getClassLoader​(java.lang.Class<?> class1, java.lang.Class<?> class2)  
      static java.net.URL getResource​(java.lang.String resource, java.lang.ClassLoader defaultLoader)
      This method will search for resource in different places.
      static java.io.InputStream getResourceAsStream​(java.lang.String resource, java.lang.ClassLoader defaultLoader)
      This method will search for resource in different places.
      static java.lang.ClassLoader getThreadContextClassLoader()
      Returns the ClassLoader of current thread if possible, or falls back to the system ClassLoader if none is available.
      static java.lang.Class<?> initializeClass​(java.lang.String className, java.lang.ClassLoader loader)
      Loads and initializes a named Class using a given ClassLoader.
      private static boolean isChild​(java.lang.ClassLoader loader1, java.lang.ClassLoader loader2)
      Determines if one ClassLoader is a child of another ClassLoader.
      static boolean isClassAvailable​(java.lang.String className)
      Determines if a named Class can be loaded or not.
      static boolean isJansiAvailable()  
      static java.lang.Class<?> loadClass​(java.lang.String className)
      Loads a class by name.
      static java.lang.Class<?> loadClass​(java.lang.String className, java.lang.ClassLoader loader)
      Loads a named Class using a given ClassLoader.
      static java.lang.Class<?> loadSystemClass​(java.lang.String className)
      Load a Class in the java.* namespace by name.
      static <T> T newCheckedInstanceOf​(java.lang.String className, java.lang.Class<T> clazz)
      Loads, instantiates, and casts a Class using the default constructor.
      static <T> T newCheckedInstanceOfProperty​(java.lang.String propertyName, java.lang.Class<T> clazz)
      Loads and instantiates a class given by a property name.
      static <T> T newInstanceOf​(java.lang.String className)
      Loads and instantiates a Class using the default constructor.
      • Methods inherited from class java.lang.Object

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

      • Loader

        private Loader()
    • Method Detail

      • getClassLoader

        public static java.lang.ClassLoader getClassLoader()
        Returns the ClassLoader to use.
        Returns:
        the ClassLoader.
      • getThreadContextClassLoader

        public static java.lang.ClassLoader getThreadContextClassLoader()
        Returns the ClassLoader of current thread if possible, or falls back to the system ClassLoader if none is available.
        Returns:
        the TCCL.
        See Also:
        LoaderUtil.getThreadContextClassLoader()
      • getClassLoader

        public static java.lang.ClassLoader getClassLoader​(java.lang.Class<?> class1,
                                                           java.lang.Class<?> class2)
      • getResource

        public static java.net.URL getResource​(java.lang.String resource,
                                               java.lang.ClassLoader defaultLoader)
        This method will search for resource in different places. The search order is as follows:
        1. Search for resource using the thread context class loader under Java2. If that fails, search for resource using the class loader that loaded this class (Loader). Under JDK 1.1, only the class loader that loaded this class (Loader) is used.
        2. Try one last time with ClassLoader.getSystemResource(resource), that is is using the system class loader in JDK 1.2 and virtual machine's built-in class loader in JDK 1.1.
        Parameters:
        resource - The resource to load.
        defaultLoader - The default ClassLoader.
        Returns:
        A URL to the resource.
      • getResourceAsStream

        public static java.io.InputStream getResourceAsStream​(java.lang.String resource,
                                                              java.lang.ClassLoader defaultLoader)
        This method will search for resource in different places. The search order is as follows:
        1. Search for resource using the thread context class loader under Java2. If that fails, search for resource using the class loader that loaded this class (Loader). Under JDK 1.1, only the class loader that loaded this class (Loader) is used.
        2. Try one last time with ClassLoader.getSystemResource(resource), that is is using the system class loader in JDK 1.2 and virtual machine's built-in class loader in JDK 1.1.
        Parameters:
        resource - The resource to load.
        defaultLoader - The default ClassLoader.
        Returns:
        An InputStream to read the resouce.
      • isChild

        private static boolean isChild​(java.lang.ClassLoader loader1,
                                       java.lang.ClassLoader loader2)
        Determines if one ClassLoader is a child of another ClassLoader. Note that a null ClassLoader is interpreted as the system ClassLoader as per convention.
        Parameters:
        loader1 - the ClassLoader to check for childhood.
        loader2 - the ClassLoader to check for parenthood.
        Returns:
        true if the first ClassLoader is a strict descendant of the second ClassLoader.
      • initializeClass

        public static java.lang.Class<?> initializeClass​(java.lang.String className,
                                                         java.lang.ClassLoader loader)
                                                  throws java.lang.ClassNotFoundException
        Loads and initializes a named Class using a given ClassLoader.
        Parameters:
        className - The class name.
        loader - The class loader.
        Returns:
        The class.
        Throws:
        java.lang.ClassNotFoundException - if the class could not be found.
      • loadClass

        public static java.lang.Class<?> loadClass​(java.lang.String className,
                                                   java.lang.ClassLoader loader)
                                            throws java.lang.ClassNotFoundException
        Loads a named Class using a given ClassLoader.
        Parameters:
        className - The class name.
        loader - The class loader.
        Returns:
        The class, or null if loader is null.
        Throws:
        java.lang.ClassNotFoundException - if the class could not be found.
      • loadSystemClass

        public static java.lang.Class<?> loadSystemClass​(java.lang.String className)
                                                  throws java.lang.ClassNotFoundException
        Load a Class in the java.* namespace by name. Useful for peculiar scenarios typically involving Google App Engine.
        Parameters:
        className - The class name.
        Returns:
        The Class.
        Throws:
        java.lang.ClassNotFoundException - if the Class could not be found.
      • newInstanceOf

        public static <T> T newInstanceOf​(java.lang.String className)
                                   throws java.lang.ClassNotFoundException,
                                          java.lang.IllegalAccessException,
                                          java.lang.InstantiationException,
                                          java.lang.NoSuchMethodException,
                                          java.lang.reflect.InvocationTargetException
        Loads and instantiates a Class using the default constructor.
        Parameters:
        className - The class name.
        Returns:
        new instance of the class.
        Throws:
        java.lang.ClassNotFoundException - if the class isn't available to the usual ClassLoaders
        java.lang.IllegalAccessException - if the class can't be instantiated through a public constructor
        java.lang.InstantiationException - if there was an exception whilst instantiating the class
        java.lang.NoSuchMethodException - if there isn't a no-args constructor on the class
        java.lang.reflect.InvocationTargetException - if there was an exception whilst constructing the class
      • newCheckedInstanceOf

        public static <T> T newCheckedInstanceOf​(java.lang.String className,
                                                 java.lang.Class<T> clazz)
                                          throws java.lang.ClassNotFoundException,
                                                 java.lang.NoSuchMethodException,
                                                 java.lang.IllegalAccessException,
                                                 java.lang.reflect.InvocationTargetException,
                                                 java.lang.InstantiationException
        Loads, instantiates, and casts a Class using the default constructor.
        Type Parameters:
        T - The type to cast it to.
        Parameters:
        className - The class name.
        clazz - The class to cast it to.
        Returns:
        new instance of the class cast to T
        Throws:
        java.lang.ClassNotFoundException - if the class isn't available to the usual ClassLoaders
        java.lang.IllegalAccessException - if the class can't be instantiated through a public constructor
        java.lang.InstantiationException - if there was an exception whilst instantiating the class
        java.lang.NoSuchMethodException - if there isn't a no-args constructor on the class
        java.lang.reflect.InvocationTargetException - if there was an exception whilst constructing the class
        java.lang.ClassCastException - if the constructed object isn't type compatible with T
      • newCheckedInstanceOfProperty

        public static <T> T newCheckedInstanceOfProperty​(java.lang.String propertyName,
                                                         java.lang.Class<T> clazz)
                                                  throws java.lang.ClassNotFoundException,
                                                         java.lang.NoSuchMethodException,
                                                         java.lang.reflect.InvocationTargetException,
                                                         java.lang.InstantiationException,
                                                         java.lang.IllegalAccessException
        Loads and instantiates a class given by a property name.
        Type Parameters:
        T - The type to cast it to.
        Parameters:
        propertyName - The property name to look up a class name for.
        clazz - The class to cast it to.
        Returns:
        new instance of the class given in the property or null if the property was unset.
        Throws:
        java.lang.ClassNotFoundException - if the class isn't available to the usual ClassLoaders
        java.lang.IllegalAccessException - if the class can't be instantiated through a public constructor
        java.lang.InstantiationException - if there was an exception whilst instantiating the class
        java.lang.NoSuchMethodException - if there isn't a no-args constructor on the class
        java.lang.reflect.InvocationTargetException - if there was an exception whilst constructing the class
        java.lang.ClassCastException - if the constructed object isn't type compatible with T
      • isClassAvailable

        public static boolean isClassAvailable​(java.lang.String className)
        Determines if a named Class can be loaded or not.
        Parameters:
        className - The class name.
        Returns:
        true if the class could be found or false otherwise.
      • isJansiAvailable

        public static boolean isJansiAvailable()
      • loadClass

        public static java.lang.Class<?> loadClass​(java.lang.String className)
                                            throws java.lang.ClassNotFoundException
        Loads a class by name. This method respects the #IGNORE_TCCL_PROPERTY Log4j property. If this property is specified and set to anything besides false, then the default ClassLoader will be used.
        Parameters:
        className - The class name.
        Returns:
        the Class for the given name.
        Throws:
        java.lang.ClassNotFoundException - if the specified class name could not be found