org.apache.velocity.util
Class ClassUtils

java.lang.Object
  extended by org.apache.velocity.util.ClassUtils

public class ClassUtils
extends Object

Simple utility functions for manipulating classes and resources from the classloader.

Version:
$Id: ClassUtils.java 191743 2005-06-21 23:22:20Z dlr $
Author:
Will Glass-Husain

Method Summary
static Object getNewInstance(String classname)
          Return a new instance of the given class.
static InputStream getResourceAsStream(Class claz, 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
 

Method Detail

getNewInstance

public static Object getNewInstance(String classname)
                             throws ClassNotFoundException,
                                    IllegalAccessException,
                                    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:
classname - the name of the class to instantiate
Returns:
new class instance
Throws:
ClassNotFoundException
IllegalAccessException
InstantiationException

getResourceAsStream

public static InputStream getResourceAsStream(Class claz,
                                              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:
null if not found, the input stream otherwise.


Copyright © 2002 Apache Software Foundation. All Rights Reserved.