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
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.reflect.Method findDeclaredMethod​(java.lang.Class clazz, java.lang.String name, java.lang.Class... params)
      Find a declared method in a class.
      static java.lang.reflect.Method findFactoryMethod​(java.lang.Class factory, java.lang.Class target)
      Given a factory class and a target class, search for the following methods:
      static java.lang.reflect.Method findGetter​(java.lang.String getterName, java.lang.Class clazz)  
      static java.lang.reflect.Method findGetter​(java.lang.String getterName, java.lang.Class clazz, boolean mandatory)  
      static java.lang.reflect.Method findMethod​(java.lang.Class clazz, java.lang.String name, java.lang.Class... params)
      Find a callable method in a class
      static java.lang.reflect.Method findSetter​(java.lang.String setterName, java.lang.Class clazz)  
      static java.lang.reflect.Method findSetter​(java.lang.String setterName, java.lang.Class clazz, java.util.function.Predicate<java.lang.Class> argumentClassFilter)  
      static java.lang.reflect.Method findSetter​(java.lang.String setterName, java.lang.Class clazz, java.util.function.Predicate<java.lang.Class> argumentClassFilter, boolean mandatory)  
      static java.lang.Class getClass​(java.lang.String name)
      Load a class with a given name.
      static java.lang.Object getFieldValue​(java.lang.Class clazz, java.lang.String fieldname)
      Given a class and a static field name, get the field value.
      static java.lang.Object getFieldValue​(java.lang.String fieldPath)
      Given a static field path, aka classname.field, get the field value.
      static java.lang.Object getInstance​(java.lang.String classname)
      Get an instance of a named class.
      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.
      • Methods inherited from class java.lang.Object

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

      • 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
        Get an instance of a named class.
        Parameters:
        classname - class name
        Returns:
        new class instance
        Throws:
        java.lang.ClassNotFoundException - if class is not found
        java.lang.IllegalAccessException - if not granted
        java.lang.InstantiationException - if instance creation throwed
      • 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):
        • Thread.currentThread().getContextClassLoader().getResources(name)
        • ClassUtils.class.getClassLoader().getResources(name)
        • ClassUtils.class.getResource(name)
        • getCallerLoader(Object caller).getResources(name)
        • caller.getClass().getResource(name)
        Parameters:
        name - The name of the resources to load
        caller - The instance or Class calling this method
        Returns:
        the list of found resources
      • 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
        Returns:
        the found URL, or null if not found
      • 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
        Returns:
        the resource input stream or null if not found
      • findMethod

        public static java.lang.reflect.Method findMethod​(java.lang.Class clazz,
                                                          java.lang.String name,
                                                          java.lang.Class... params)
                                                   throws java.lang.SecurityException
        Find a callable method in a class
        Parameters:
        clazz - target class
        name - method name
        params - method arguments classes
        Returns:
        method object
        Throws:
        java.lang.SecurityException - if not granted
      • findDeclaredMethod

        public static java.lang.reflect.Method findDeclaredMethod​(java.lang.Class clazz,
                                                                  java.lang.String name,
                                                                  java.lang.Class... params)
                                                           throws java.lang.SecurityException
        Find a declared method in a class. It will be made accessible if needed and allowed.
        Parameters:
        clazz - target class
        name - method name
        params - method arguments classes
        Returns:
        Throws:
        java.lang.SecurityException - if not allowed
      • getFieldValue

        public static java.lang.Object getFieldValue​(java.lang.String fieldPath)
                                              throws java.lang.ClassNotFoundException,
                                                     java.lang.NoSuchFieldException,
                                                     java.lang.SecurityException,
                                                     java.lang.IllegalAccessException
        Given a static field path, aka classname.field, get the field value.
        Parameters:
        fieldPath - field path
        Returns:
        field value
        Throws:
        java.lang.ClassNotFoundException - if class hasn't been found
        java.lang.NoSuchFieldException - if field hasn't been found
        java.lang.SecurityException - if not granted
        java.lang.IllegalAccessException - if field is not accessible
      • 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
        Given a class and a static field name, get the field value.
        Parameters:
        clazz - target class
        fieldname - field name
        Returns:
        field value
        Throws:
        java.lang.NoSuchFieldException - if field hasn't been found
        java.lang.SecurityException - if not granted
        java.lang.IllegalAccessException - if field is not accessible
      • 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.
        Parameters:
        obj - the target obj
        Returns:
        an iterator over the content of obj, or null if not found
        Throws:
        java.lang.NoSuchMethodException - if no iterator() method
        java.lang.IllegalAccessException - if iterator() method not callable
        java.lang.reflect.InvocationTargetException - if iterator() method throwed
      • findFactoryMethod

        public static java.lang.reflect.Method findFactoryMethod​(java.lang.Class factory,
                                                                 java.lang.Class target)

        Given a factory class and a target class, search for the following methods:

        • createTargetClassname(),
        • newTargetClassname(), or
        • getTargetClassname().
        Parameters:
        factory - factory class
        target - target class
        Returns:
        first factory method found, or null otherwise
      • findGetter

        public static java.lang.reflect.Method findGetter​(java.lang.String getterName,
                                                          java.lang.Class clazz)
                                                   throws java.lang.NoSuchMethodException
        Throws:
        java.lang.NoSuchMethodException
      • findGetter

        public static java.lang.reflect.Method findGetter​(java.lang.String getterName,
                                                          java.lang.Class clazz,
                                                          boolean mandatory)
                                                   throws java.lang.NoSuchMethodException
        Throws:
        java.lang.NoSuchMethodException
      • findSetter

        public static java.lang.reflect.Method findSetter​(java.lang.String setterName,
                                                          java.lang.Class clazz)
                                                   throws java.lang.NoSuchMethodException
        Throws:
        java.lang.NoSuchMethodException
      • findSetter

        public static java.lang.reflect.Method findSetter​(java.lang.String setterName,
                                                          java.lang.Class clazz,
                                                          java.util.function.Predicate<java.lang.Class> argumentClassFilter)
                                                   throws java.lang.NoSuchMethodException
        Throws:
        java.lang.NoSuchMethodException
      • findSetter

        public static java.lang.reflect.Method findSetter​(java.lang.String setterName,
                                                          java.lang.Class clazz,
                                                          java.util.function.Predicate<java.lang.Class> argumentClassFilter,
                                                          boolean mandatory)
                                                   throws java.lang.NoSuchMethodException
        Throws:
        java.lang.NoSuchMethodException