public class ClassUtils extends Object
Modifier and Type | Method and Description |
---|---|
static Method |
findDeclaredMethod(Class clazz,
String name,
Class... params)
Find a declared method in a class.
|
static Method |
findFactoryMethod(Class factory,
Class target)
Given a factory class and a target class, search for the following methods:
|
static Method |
findGetter(String getterName,
Class clazz) |
static Method |
findGetter(String getterName,
Class clazz,
boolean mandatory) |
static Method |
findMethod(Class clazz,
String name,
Class... params)
Find a callable method in a class
|
static Method |
findSetter(String setterName,
Class clazz) |
static Method |
findSetter(String setterName,
Class clazz,
Predicate<Class> argumentClassFilter) |
static Method |
findSetter(String setterName,
Class clazz,
Predicate<Class> argumentClassFilter,
boolean mandatory) |
static Class |
getClass(String name)
Load a class with a given name.
|
static Object |
getFieldValue(Class clazz,
String fieldname)
Given a class and a static field name, get the field value.
|
static Object |
getFieldValue(String fieldPath)
Given a static field path, aka classname.field, get the field value.
|
static Object |
getInstance(String classname)
Get an instance of a named class.
|
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.
|
static List<URL> |
getResources(String name,
Object caller)
Load all resources with the specified name.
|
public static Class getClass(String name) throws ClassNotFoundException
Thread
.currentThread().getContextClassLoader()
Class.forName(java.lang.String)
ClassUtils
.class.getClassLoader()
name
- Fully qualified class name to be loadedClassNotFoundException
- if the class cannot be foundpublic static Object getInstance(String classname) throws ClassNotFoundException, IllegalAccessException, InstantiationException
classname
- class nameClassNotFoundException
- if class is not foundIllegalAccessException
- if not grantedInstantiationException
- if instance creation throwedpublic static List<URL> getResources(String name, Object caller)
ClassUtils
.class.getClassLoader().getResources(name)ClassUtils
.class.getResource(name)getCallerLoader(Object caller)
.getResources(name)name
- The name of the resources to loadcaller
- The instance or Class
calling this methodpublic static URL getResource(String name, Object caller)
ClassUtils
.class.getClassLoader().getResource(name)ClassUtils
.class.getResource(name)name
- The name of the resource to loadcaller
- The instance or Class
calling this methodpublic static InputStream getResourceAsStream(String name, Object caller)
name
- The name of the resource to loadcaller
- The instance or Class
calling this methodpublic static Method findMethod(Class clazz, String name, Class... params) throws SecurityException
clazz
- target classname
- method nameparams
- method arguments classesSecurityException
- if not grantedpublic static Method findDeclaredMethod(Class clazz, String name, Class... params) throws SecurityException
clazz
- target classname
- method nameparams
- method arguments classesSecurityException
- if not allowedpublic static Object getFieldValue(String fieldPath) throws ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalAccessException
fieldPath
- field pathClassNotFoundException
- if class hasn't been foundNoSuchFieldException
- if field hasn't been foundSecurityException
- if not grantedIllegalAccessException
- if field is not accessiblepublic static Object getFieldValue(Class clazz, String fieldname) throws NoSuchFieldException, SecurityException, IllegalAccessException
clazz
- target classfieldname
- field nameNoSuchFieldException
- if field hasn't been foundSecurityException
- if not grantedIllegalAccessException
- if field is not accessiblepublic static Iterator getIterator(Object obj) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
obj
- the target objNoSuchMethodException
- if no iterator() methodIllegalAccessException
- if iterator() method not callableInvocationTargetException
- if iterator() method throwedpublic static Method findFactoryMethod(Class factory, Class target)
Given a factory class and a target class, search for the following methods:
createTargetClassname()
,newTargetClassname()
, orgetTargetClassname()
.factory
- factory classtarget
- target classpublic static Method findGetter(String getterName, Class clazz) throws NoSuchMethodException
NoSuchMethodException
public static Method findGetter(String getterName, Class clazz, boolean mandatory) throws NoSuchMethodException
NoSuchMethodException
public static Method findSetter(String setterName, Class clazz) throws NoSuchMethodException
NoSuchMethodException
public static Method findSetter(String setterName, Class clazz, Predicate<Class> argumentClassFilter) throws NoSuchMethodException
NoSuchMethodException
public static Method findSetter(String setterName, Class clazz, Predicate<Class> argumentClassFilter, boolean mandatory) throws NoSuchMethodException
NoSuchMethodException
Copyright © 2002–2021 The Apache Software Foundation. All rights reserved.