Class ClassUtils


  • public final class ClassUtils
    extends java.lang.Object
    Utility methods for Class Loading.
    Author:
    Davanum Srinvas (dims@yahoo.com), Matthew Pocock (matthew_pocock@yahoo.co.uk)
    • Constructor Summary

      Constructors 
      Constructor Description
      ClassUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.ClassLoader createClassLoader​(java.lang.String classpath, java.lang.ClassLoader parent)
      Creates a new ClassLoader from a classpath specification and a parent class loader.
      static java.lang.Class forName​(java.lang.String className)
      Use this method instead of Class.forName
      static java.lang.Class forName​(java.lang.String _className, boolean init, java.lang.ClassLoader _loader)
      Use this method instead of Class.forName (String className, boolean init, ClassLoader loader)
      static java.lang.ClassLoader getClassLoader​(java.lang.String className)
      Obtain the ClassLoader (if any) associated with the given className.
      static java.lang.ClassLoader getDefaultClassLoader()  
      static java.io.InputStream getResourceAsStream​(java.lang.Class clazz, java.lang.String resource)
      Get an input stream from a named resource.
      static java.io.InputStream getResourceAsStream​(java.lang.Class clazz, java.lang.String resource, boolean checkThreadContextFirst)
      Get an input stream from a named resource.
      static void removeClassLoader​(java.lang.String className)
      Deregister the ClassLoader for a given className.
      static void setClassLoader​(java.lang.String className, java.lang.ClassLoader loader)
      Set the ClassLoader associated with the given className.
      static void setDefaultClassLoader​(java.lang.ClassLoader loader)
      Set the default ClassLoader.
      • Methods inherited from class java.lang.Object

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

      • ClassUtils

        public ClassUtils()
    • Method Detail

      • setDefaultClassLoader

        public static void setDefaultClassLoader​(java.lang.ClassLoader loader)
        Set the default ClassLoader. If loader is null, the default loader is not changed.
        Parameters:
        loader - the new default ClassLoader
      • getDefaultClassLoader

        public static java.lang.ClassLoader getDefaultClassLoader()
      • setClassLoader

        public static void setClassLoader​(java.lang.String className,
                                          java.lang.ClassLoader loader)
        Set the ClassLoader associated with the given className. If either the class name or the loader are null, no action is performed.
        Parameters:
        className - the name of a class
        loader - the ClassLoader for the class
      • getClassLoader

        public static java.lang.ClassLoader getClassLoader​(java.lang.String className)
        Obtain the ClassLoader (if any) associated with the given className.
        Parameters:
        className - the name of a class
        Returns:
        class loader
      • removeClassLoader

        public static void removeClassLoader​(java.lang.String className)
        Deregister the ClassLoader for a given className.
        Parameters:
        className - the name of a class
      • forName

        public static java.lang.Class forName​(java.lang.String className)
                                       throws java.lang.ClassNotFoundException
        Use this method instead of Class.forName
        Parameters:
        className - Class name
        Returns:
        java class
        Throws:
        java.lang.ClassNotFoundException - if the class is not found
      • forName

        public static java.lang.Class forName​(java.lang.String _className,
                                              boolean init,
                                              java.lang.ClassLoader _loader)
                                       throws java.lang.ClassNotFoundException
        Use this method instead of Class.forName (String className, boolean init, ClassLoader loader)
        Parameters:
        _className - Class name
        init - initialize the class
        _loader - class loader
        Returns:
        java class
        Throws:
        java.lang.ClassNotFoundException - if the class is not found
      • getResourceAsStream

        public static java.io.InputStream getResourceAsStream​(java.lang.Class clazz,
                                                              java.lang.String resource,
                                                              boolean checkThreadContextFirst)
        Get an input stream from a named resource. Tries
        1. the classloader that loaded "clazz" first,
        2. the system classloader
        3. the class "clazz" itself
        Parameters:
        clazz - class to use in the lookups
        resource - resource string to look for
        checkThreadContextFirst - check the thread context first?
        Returns:
        input stream if found, or null
      • getResourceAsStream

        public static java.io.InputStream getResourceAsStream​(java.lang.Class clazz,
                                                              java.lang.String resource)
        Get an input stream from a named resource. Tries
        1. the classloader that loaded "clazz" first,
        2. the system classloader
        3. the class "clazz" itself
        Parameters:
        clazz - class to use in the lookups
        resource - resource string to look for
        Returns:
        input stream if found, or null
      • createClassLoader

        public static java.lang.ClassLoader createClassLoader​(java.lang.String classpath,
                                                              java.lang.ClassLoader parent)
                                                       throws java.lang.SecurityException
        Creates a new ClassLoader from a classpath specification and a parent class loader. The classpath string will be split using the system path seperator character (e.g. : or ;), just as the java system-wide class path is processed.
        Parameters:
        classpath - the classpath String
        parent - the parent ClassLoader, or null if the default is to be used
        Throws:
        java.lang.SecurityException - if you don't have privilages to create class loaders
        java.lang.IllegalArgumentException - if your classpath string is silly