Class ClassUtils


  • public class ClassUtils
    extends java.lang.Object
    Repository for common class and reflection methods.
    Version:
    $Id: ClassUtils.java 511959 2007-02-26 19:24:39Z nbubna $
    Author:
    Nathan Bubna
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ClassUtils()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private static void addResource​(java.lang.String name, java.util.Set<java.lang.String> urls, java.lang.Class c)  
      private static boolean addResources​(java.lang.String name, java.util.Set<java.lang.String> urls, java.lang.ClassLoader loader)  
      static java.lang.reflect.Method findDeclaredMethod​(java.lang.Class clazz, java.lang.String name, java.lang.Class[] params)  
      static java.lang.reflect.Method findMethod​(java.lang.Class clazz, java.lang.String name, java.lang.Class[] params)  
      private static java.lang.ClassLoader getCallerLoader​(java.lang.Object caller)  
      static java.lang.Class getClass​(java.lang.String name)
      Load a class with a given name.
      private static java.lang.ClassLoader getClassLoader()  
      static java.lang.Object getFieldValue​(java.lang.Class clazz, java.lang.String fieldname)  
      static java.lang.Object getFieldValue​(java.lang.String fieldPath)  
      ClassUtils getInstance()  
      static java.lang.Object getInstance​(java.lang.String classname)  
      static java.util.Iterator getIterator​(java.lang.Object obj)
      Retrieves an Iterator from or creates and Iterator for the specified object.
      static java.net.URL getResource​(java.lang.String name, java.lang.Object caller)
      Load a given resource.
      static java.io.InputStream getResourceAsStream​(java.lang.String name, java.lang.Object caller)
      This is a convenience method to load a resource as a stream.
      static java.util.List<java.net.URL> getResources​(java.lang.String name, java.lang.Object caller)
      Load all resources with the specified name.
      private static java.lang.ClassLoader getThreadContextLoader()  
      • Methods inherited from class java.lang.Object

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

      • INSTANCE

        public static final ClassUtils INSTANCE
    • Constructor Detail

      • ClassUtils

        private ClassUtils()
    • Method Detail

      • getThreadContextLoader

        private static final java.lang.ClassLoader getThreadContextLoader()
      • getClassLoader

        private static final java.lang.ClassLoader getClassLoader()
      • getCallerLoader

        private static final java.lang.ClassLoader getCallerLoader​(java.lang.Object caller)
      • getClass

        public static java.lang.Class getClass​(java.lang.String name)
                                        throws java.lang.ClassNotFoundException
        Load a class with a given name.

        It will try to load the class in the following order:

        • From Thread.currentThread().getContextClassLoader()
        • Using the basic Class.forName(java.lang.String)
        • From ClassUtils.class.getClassLoader()
        Parameters:
        name - Fully qualified class name to be loaded
        Returns:
        Class object
        Throws:
        java.lang.ClassNotFoundException - if the class cannot be found
      • getInstance

        public static java.lang.Object getInstance​(java.lang.String classname)
                                            throws java.lang.ClassNotFoundException,
                                                   java.lang.IllegalAccessException,
                                                   java.lang.InstantiationException
        Throws:
        java.lang.ClassNotFoundException
        java.lang.IllegalAccessException
        java.lang.InstantiationException
      • getResources

        public static java.util.List<java.net.URL> getResources​(java.lang.String name,
                                                                java.lang.Object caller)
        Load all resources with the specified name. If none are found, we prepend the name with '/' and try again. This will attempt to load the resources from the following methods (in order):
        Parameters:
        name - The name of the resources to load
        caller - The instance or Class calling this method
      • addResource

        private static final void addResource​(java.lang.String name,
                                              java.util.Set<java.lang.String> urls,
                                              java.lang.Class c)
      • addResources

        private static final boolean addResources​(java.lang.String name,
                                                  java.util.Set<java.lang.String> urls,
                                                  java.lang.ClassLoader loader)
      • getResource

        public static java.net.URL getResource​(java.lang.String name,
                                               java.lang.Object caller)
        Load a given resource.

        This method will try to load the resource using the following methods (in order):

        • Thread.currentThread().getContextClassLoader().getResource(name)
        • ClassUtils.class.getClassLoader().getResource(name)
        • ClassUtils.class.getResource(name)
        • caller.getClass().getResource(name) or, if caller is a Class, caller.getResource(name)
        Parameters:
        name - The name of the resource to load
        caller - The instance or Class calling this method
      • getResourceAsStream

        public static java.io.InputStream getResourceAsStream​(java.lang.String name,
                                                              java.lang.Object caller)
        This is a convenience method to load a resource as a stream.

        The algorithm used to find the resource is given in getResource()

        Parameters:
        name - The name of the resource to load
        caller - The instance or Class calling this method
      • findMethod

        public static java.lang.reflect.Method findMethod​(java.lang.Class clazz,
                                                          java.lang.String name,
                                                          java.lang.Class[] params)
                                                   throws java.lang.SecurityException
        Throws:
        java.lang.SecurityException
      • findDeclaredMethod

        public static java.lang.reflect.Method findDeclaredMethod​(java.lang.Class clazz,
                                                                  java.lang.String name,
                                                                  java.lang.Class[] params)
                                                           throws java.lang.SecurityException
        Throws:
        java.lang.SecurityException
      • getFieldValue

        public static java.lang.Object getFieldValue​(java.lang.String fieldPath)
                                              throws java.lang.ClassNotFoundException,
                                                     java.lang.NoSuchFieldException,
                                                     java.lang.SecurityException,
                                                     java.lang.IllegalAccessException
        Throws:
        java.lang.ClassNotFoundException
        java.lang.NoSuchFieldException
        java.lang.SecurityException
        java.lang.IllegalAccessException
      • getFieldValue

        public static java.lang.Object getFieldValue​(java.lang.Class clazz,
                                                     java.lang.String fieldname)
                                              throws java.lang.NoSuchFieldException,
                                                     java.lang.SecurityException,
                                                     java.lang.IllegalAccessException
        Throws:
        java.lang.NoSuchFieldException
        java.lang.SecurityException
        java.lang.IllegalAccessException
      • getIterator

        public static java.util.Iterator getIterator​(java.lang.Object obj)
                                              throws java.lang.NoSuchMethodException,
                                                     java.lang.IllegalAccessException,
                                                     java.lang.reflect.InvocationTargetException
        Retrieves an Iterator from or creates and Iterator for the specified object. This method is almost entirely copied from Engine's UberspectImpl class.
        Throws:
        java.lang.NoSuchMethodException
        java.lang.IllegalAccessException
        java.lang.reflect.InvocationTargetException