Class ClassUtils


  • public class ClassUtils
    extends java.lang.Object
    Simple utility functions for manipulating classes and resources from the classloader.
    Since:
    1.5
    Version:
    $Id: ClassUtils.java 898032 2010-01-11 19:51:03Z nbubna $
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ClassUtils()
      Utility class; cannot be instantiated.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Class getClass​(java.lang.String clazz)
      Return the specified class.
      static VelMethod getMethod​(java.lang.String methodName, java.lang.Object[] params, java.lang.Class[] paramClasses, java.lang.Object o, InternalContextAdapter context, SimpleNode node, boolean strictRef)
      Lookup a VelMethod object given the method signature that is specified in the passed in parameters.
      static java.lang.Object getNewInstance​(java.lang.String clazz)
      Return a new instance of the given class.
      static java.io.InputStream getResourceAsStream​(java.lang.Class claz, java.lang.String name)
      Finds a resource with the given name.
      • Methods inherited from class java.lang.Object

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

      • ClassUtils

        private ClassUtils()
        Utility class; cannot be instantiated.
    • Method Detail

      • getClass

        public static java.lang.Class getClass​(java.lang.String clazz)
                                        throws java.lang.ClassNotFoundException
        Return the specified class. Checks the ThreadContext classloader first, then uses the System classloader. Should replace all calls to Class.forName( claz ) (which only calls the System class loader) when the class might be in a different classloader (e.g. in a webapp).
        Parameters:
        clazz - the name of the class to instantiate
        Returns:
        the requested Class object
        Throws:
        java.lang.ClassNotFoundException
      • getNewInstance

        public static java.lang.Object getNewInstance​(java.lang.String clazz)
                                               throws java.lang.ClassNotFoundException,
                                                      java.lang.IllegalAccessException,
                                                      java.lang.InstantiationException
        Return a new instance of the given class. Checks the ThreadContext classloader first, then uses the System classloader. Should replace all calls to Class.forName( claz ).newInstance() (which only calls the System class loader) when the class might be in a different classloader (e.g. in a webapp).
        Parameters:
        clazz - the name of the class to instantiate
        Returns:
        an instance of the specified class
        Throws:
        java.lang.ClassNotFoundException
        java.lang.IllegalAccessException
        java.lang.InstantiationException
      • getResourceAsStream

        public static java.io.InputStream getResourceAsStream​(java.lang.Class claz,
                                                              java.lang.String name)
        Finds a resource with the given name. Checks the Thread Context classloader, then uses the System classloader. Should replace all calls to Class.getResourceAsString when the resource might come from a different classloader. (e.g. a webapp).
        Parameters:
        claz - Class to use when getting the System classloader (used if no Thread Context classloader available or fails to get resource).
        name - name of the resource
        Returns:
        InputStream for the resource.
      • getMethod

        public static VelMethod getMethod​(java.lang.String methodName,
                                          java.lang.Object[] params,
                                          java.lang.Class[] paramClasses,
                                          java.lang.Object o,
                                          InternalContextAdapter context,
                                          SimpleNode node,
                                          boolean strictRef)
        Lookup a VelMethod object given the method signature that is specified in the passed in parameters. This method first searches the cache, if not found in the cache then uses reflections to inspect Object o, for the given method.
        Parameters:
        methodName - Name of method
        params - Array of objects that are parameters to the method
        paramClasses - Array of Classes coresponding to the types in params.
        o - Object to introspect for the given method.
        context - Context from which the method cache is aquirred
        node - ASTNode, used for error reporting.
        strictRef - If no method is found, throw an exception, never return null in this case
        Returns:
        VelMethod object if the object is found, null if not matching method is found