Package org.apache.velocity.tools
Class ClassUtils
- java.lang.Object
-
- org.apache.velocity.tools.ClassUtils
-
public class ClassUtils extends java.lang.ObjectRepository 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.MethodfindDeclaredMethod(java.lang.Class clazz, java.lang.String name, java.lang.Class... params)Find a declared method in a class.static java.lang.reflect.MethodfindFactoryMethod(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.MethodfindGetter(java.lang.String getterName, java.lang.Class clazz)static java.lang.reflect.MethodfindGetter(java.lang.String getterName, java.lang.Class clazz, boolean mandatory)static java.lang.reflect.MethodfindMethod(java.lang.Class clazz, java.lang.String name, java.lang.Class... params)Find a callable method in a classstatic java.lang.reflect.MethodfindSetter(java.lang.String setterName, java.lang.Class clazz)static java.lang.reflect.MethodfindSetter(java.lang.String setterName, java.lang.Class clazz, java.util.function.Predicate<java.lang.Class> argumentClassFilter)static java.lang.reflect.MethodfindSetter(java.lang.String setterName, java.lang.Class clazz, java.util.function.Predicate<java.lang.Class> argumentClassFilter, boolean mandatory)static java.lang.ClassgetClass(java.lang.String name)Load a class with a given name.static java.lang.ObjectgetFieldValue(java.lang.Class clazz, java.lang.String fieldname)Given a class and a static field name, get the field value.static java.lang.ObjectgetFieldValue(java.lang.String fieldPath)Given a static field path, aka classname.field, get the field value.static java.lang.ObjectgetInstance(java.lang.String classname)Get an instance of a named class.static java.util.IteratorgetIterator(java.lang.Object obj)Retrieves an Iterator from or creates and Iterator for the specified object.static java.net.URLgetResource(java.lang.String name, java.lang.Object caller)Load a given resource.static java.io.InputStreamgetResourceAsStream(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.
-
-
-
Method Detail
-
getClass
public static java.lang.Class getClass(java.lang.String name) throws java.lang.ClassNotFoundExceptionLoad 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
- From
-
getInstance
public static java.lang.Object getInstance(java.lang.String classname) throws java.lang.ClassNotFoundException, java.lang.IllegalAccessException, java.lang.InstantiationExceptionGet an instance of a named class.- Parameters:
classname- class name- Returns:
- new class instance
- Throws:
java.lang.ClassNotFoundException- if class is not foundjava.lang.IllegalAccessException- if not grantedjava.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 loadcaller- The instance orClasscalling 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 loadcaller- The instance orClasscalling 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 loadcaller- The instance orClasscalling 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.SecurityExceptionFind a callable method in a class- Parameters:
clazz- target classname- method nameparams- 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.SecurityExceptionFind a declared method in a class. It will be made accessible if needed and allowed.- Parameters:
clazz- target classname- method nameparams- 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.IllegalAccessExceptionGiven 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 foundjava.lang.NoSuchFieldException- if field hasn't been foundjava.lang.SecurityException- if not grantedjava.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.IllegalAccessExceptionGiven a class and a static field name, get the field value.- Parameters:
clazz- target classfieldname- field name- Returns:
- field value
- Throws:
java.lang.NoSuchFieldException- if field hasn't been foundjava.lang.SecurityException- if not grantedjava.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.InvocationTargetExceptionRetrieves 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() methodjava.lang.IllegalAccessException- if iterator() method not callablejava.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(), orgetTargetClassname().
- Parameters:
factory- factory classtarget- 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
-
-