Package org.apache.velocity.tools
Class ClassUtils
java.lang.Object
org.apache.velocity.tools.ClassUtils
Repository for common class and reflection methods.
- Version:
- $Id: ClassUtils.java 511959 2007-02-26 19:24:39Z nbubna $
- Author:
- Nathan Bubna
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static final void
addResource
(String name, Set<String> urls, Class c) private static final boolean
addResources
(String name, Set<String> urls, ClassLoader loader) static Method
findDeclaredMethod
(Class clazz, String name, Class[] params) static Method
findMethod
(Class clazz, String name, Class[] params) private static final ClassLoader
getCallerLoader
(Object caller) static Class
Load a class with a given name.private static final ClassLoader
static Object
getFieldValue
(Class clazz, String fieldname) static Object
getFieldValue
(String fieldPath) static Object
getInstance
(String classname) static Iterator
getIterator
(Object obj) Retrieves an Iterator from or creates and Iterator for the specified object.static URL
getResource
(String name, Object caller) Load a given resource.static InputStream
getResourceAsStream
(String name, Object caller) This is a convenience method to load a resource as a stream.getResources
(String name, Object caller) Load all resources with the specified name.private static final ClassLoader
-
Field Details
-
INSTANCE
-
-
Constructor Details
-
ClassUtils
private ClassUtils()
-
-
Method Details
-
getInstance
-
getThreadContextLoader
-
getClassLoader
-
getCallerLoader
-
getClass
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:
ClassNotFoundException
- if the class cannot be found
- From
-
getInstance
public static Object getInstance(String classname) throws ClassNotFoundException, IllegalAccessException, InstantiationException -
getResources
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 loadcaller
- The instance orClass
calling this method
-
addResource
-
addResources
-
getResource
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 loadcaller
- The instance orClass
calling this method
-
getResourceAsStream
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 loadcaller
- The instance orClass
calling this method
-
findMethod
- Throws:
SecurityException
-
findDeclaredMethod
public static Method findDeclaredMethod(Class clazz, String name, Class[] params) throws SecurityException - Throws:
SecurityException
-
getFieldValue
public static Object getFieldValue(String fieldPath) throws ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalAccessException -
getFieldValue
public static Object getFieldValue(Class clazz, String fieldname) throws NoSuchFieldException, SecurityException, IllegalAccessException -
getIterator
public static Iterator getIterator(Object obj) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException Retrieves an Iterator from or creates and Iterator for the specified object. This method is almost entirely copied from Engine's UberspectImpl class.
-