类 ClassUtils
java.lang.Object
org.apache.velocity.tools.ClassUtils
Repository for common class and reflection methods.
- 版本:
- $Id: ClassUtils.java 511959 2007-02-26 19:24:39Z nbubna $
- 作者:
- Nathan Bubna
-
方法概要
修饰符和类型方法说明static MethodfindDeclaredMethod(Class clazz, String name, Class... params) Find a declared method in a class.static MethodfindFactoryMethod(Class factory, Class target) Given a factory class and a target class, search for the following methods:static MethodfindGetter(String getterName, Class clazz) static MethodfindGetter(String getterName, Class clazz, boolean mandatory) static MethodfindMethod(Class clazz, String name, Class... params) Find a callable method in a classstatic MethodfindSetter(String setterName, Class clazz) static MethodfindSetter(String setterName, Class clazz, Predicate<Class> argumentClassFilter) static MethodfindSetter(String setterName, Class clazz, Predicate<Class> argumentClassFilter, boolean mandatory) static ClassLoad a class with a given name.static ObjectgetFieldValue(Class clazz, String fieldname) Given a class and a static field name, get the field value.static ObjectgetFieldValue(String fieldPath) Given a static field path, aka classname.field, get the field value.static ObjectgetInstance(String classname) Get an instance of a named class.static IteratorgetIterator(Object obj) Retrieves an Iterator from or creates and Iterator for the specified object.static URLgetResource(String name, Object caller) Load a given resource.static InputStreamgetResourceAsStream(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.
-
方法详细资料
-
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()
- 参数:
name- Fully qualified class name to be loaded- 返回:
- Class object
- 抛出:
ClassNotFoundException- if the class cannot be found
- From
-
getInstance
public static Object getInstance(String classname) throws ClassNotFoundException, IllegalAccessException, InstantiationException Get an instance of a named class.- 参数:
classname- class name- 返回:
- new class instance
- 抛出:
ClassNotFoundException- if class is not foundIllegalAccessException- if not grantedInstantiationException- if instance creation throwed
-
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)
- 参数:
name- The name of the resources to loadcaller- The instance orClasscalling this method- 返回:
- the list of found resources
-
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)
- 参数:
name- The name of the resource to loadcaller- The instance orClasscalling this method- 返回:
- the found URL, or null if not found
-
getResourceAsStream
This is a convenience method to load a resource as a stream. The algorithm used to find the resource is given in getResource()- 参数:
name- The name of the resource to loadcaller- The instance orClasscalling this method- 返回:
- the resource input stream or null if not found
-
findMethod
Find a callable method in a class- 参数:
clazz- target classname- method nameparams- method arguments classes- 返回:
- method object
- 抛出:
SecurityException- if not granted
-
findDeclaredMethod
public static Method findDeclaredMethod(Class clazz, String name, Class... params) throws SecurityException Find a declared method in a class. It will be made accessible if needed and allowed.- 参数:
clazz- target classname- method nameparams- method arguments classes- 返回:
- 抛出:
SecurityException- if not allowed
-
getFieldValue
public static Object getFieldValue(String fieldPath) throws ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalAccessException Given a static field path, aka classname.field, get the field value.- 参数:
fieldPath- field path- 返回:
- field value
- 抛出:
ClassNotFoundException- if class hasn't been foundNoSuchFieldException- if field hasn't been foundSecurityException- if not grantedIllegalAccessException- if field is not accessible
-
getFieldValue
public static Object getFieldValue(Class clazz, String fieldname) throws NoSuchFieldException, SecurityException, IllegalAccessException Given a class and a static field name, get the field value.- 参数:
clazz- target classfieldname- field name- 返回:
- field value
- 抛出:
NoSuchFieldException- if field hasn't been foundSecurityException- if not grantedIllegalAccessException- if field is not accessible
-
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.- 参数:
obj- the target obj- 返回:
- an iterator over the content of obj, or null if not found
- 抛出:
NoSuchMethodException- if no iterator() methodIllegalAccessException- if iterator() method not callableInvocationTargetException- if iterator() method throwed
-
findFactoryMethod
Given a factory class and a target class, search for the following methods:
createTargetClassname(),newTargetClassname(), orgetTargetClassname().
- 参数:
factory- factory classtarget- target class- 返回:
- first factory method found, or null otherwise
-
findGetter
-
findGetter
public static Method findGetter(String getterName, Class clazz, boolean mandatory) throws NoSuchMethodException -
findSetter
-
findSetter
-
findSetter
-