public final class ClassUtils extends Object
| Modifier and Type | Field and Description |
|---|---|
static Class<?>[] |
EMPTY_CLASS_ARRAY
Only support constructors with no parameters.
|
| Modifier and Type | Method and Description |
|---|---|
static List<Field> |
getAllFields(Class<?> clazz)
Given a class instance, return all fields, including fields in super classes.
|
static List<Method> |
getAllMethods(Class<?> clazz)
Given a class instance, return all methods, including methods in super classes.
|
static Field |
getDeclaredFieldIncludeSuper(String fieldName,
Class<?> clazz)
Get declared field given field name including fields in super classes.
|
static Method |
getDeclaredMethod(String methodName,
Class<?> clazz)
Return declared method with empty parameter.
|
static Object |
getFieldValue(String fieldName,
Class<?> clazz,
Object instance)
Return field value according to field name, class, instance.All exceptions are wrapped as RuntimeException.
|
static Method |
getFirstMethodWithName(String name,
Class<?> clazz)
Iterate all methods including methods in super class, return the first one; if no method with such name, return
null.
|
static Object |
invokeMethod(Method method,
Object instance,
Object... parameters)
Call method through java reflection.
|
static Object |
invokeMethod(String name,
Class<?> clazz,
Object instance,
Object... parameters)
Call method through java reflection.
|
static <T> T |
newInstance(Class<T> clazz)
Create an object for the given class.
|
static <T> T |
newInstance(Class<T> clazz,
Class<?>[] parameterClasses,
Object[] parameters)
Create an object for the given class.
|
static void |
setFieldValue(String fieldName,
Class<?> clazz,
Object instance,
Object value)
Set filed value according to field name, class instance, object instance and field value.
|
public static final Class<?>[] EMPTY_CLASS_ARRAY
public static List<Field> getAllFields(Class<?> clazz)
public static List<Method> getAllMethods(Class<?> clazz)
public static <T> T newInstance(Class<T> clazz)
clazz - class of which an object is createdRuntimeException - In case any exception for reflection.public static <T> T newInstance(Class<T> clazz, Class<?>[] parameterClasses, Object[] parameters)
clazz - class of which an object is createdparameterClasses - the parameter classes used for constructorparameters - the parameters used for new instanceRuntimeException - In case any exception for reflection.public static Field getDeclaredFieldIncludeSuper(String fieldName, Class<?> clazz)
public static Method getDeclaredMethod(String methodName, Class<?> clazz) throws NoSuchMethodException
NoSuchMethodExceptionpublic static Method getFirstMethodWithName(String name, Class<?> clazz)
public static void setFieldValue(String fieldName, Class<?> clazz, Object instance, Object value)
RuntimeException - wraps any exception as root cause.public static Object getFieldValue(String fieldName, Class<?> clazz, Object instance)
RuntimeException - wraps any exception as root cause.public static Object invokeMethod(Method method, Object instance, Object... parameters)
RuntimeException - wraps any exception as root cause.public static Object invokeMethod(String name, Class<?> clazz, Object instance, Object... parameters)
RuntimeException - wraps any exception as root cause.Copyright © 2015. All Rights Reserved.