public class ReflectionUtils extends Object
Constructor | Description |
---|---|
ReflectionUtils() |
Modifier and Type | Method | Description |
---|---|---|
static Class<?> |
classForNameOrNull(String className) |
Call Class.forName(className), but return null if any exception is thrown.
|
static Object |
getFieldVal(Object obj,
String fieldName,
boolean throwException) |
Get the value of the named field in the class of the given object or any of its superclasses.
|
static Object |
getStaticFieldVal(Class<?> cls,
String fieldName,
boolean throwException) |
Get the value of the named static field in the given class or any of its superclasses.
|
static Object |
invokeDefaultMethod(Class<?> cls,
String methodName,
Class<?> returnType,
ClassLoader classLoader,
boolean throwException) |
Invoke the named default interface method.
|
static Object |
invokeMethod(Object obj,
String methodName,
boolean throwException) |
Invoke the named method in the given object or its superclasses.
|
static Object |
invokeMethod(Object obj,
String methodName,
Class<?> argType,
Object arg,
boolean throwException) |
Invoke the named method in the given object or its superclasses.
|
static Object |
invokeStaticMethod(Class<?> cls,
String methodName,
boolean throwException) |
Invoke the named static method.
|
static Object |
invokeStaticMethod(Class<?> cls,
String methodName,
Class<?> argType,
Object arg,
boolean throwException) |
Invoke the named static method.
|
public static Object getFieldVal(Object obj, String fieldName, boolean throwException) throws IllegalArgumentException
obj
- The object.fieldName
- The field name.throwException
- If true, throw an exception if the field value could not be read.IllegalArgumentException
- If the field value could not be read.public static Object getStaticFieldVal(Class<?> cls, String fieldName, boolean throwException) throws IllegalArgumentException
cls
- The class.fieldName
- The field name.throwException
- If true, throw an exception if the field value could not be read.IllegalArgumentException
- If the field value could not be read.public static Object invokeMethod(Object obj, String methodName, boolean throwException) throws IllegalArgumentException
obj
- The object.methodName
- The method name.throwException
- If true, throw an exception if the field value could not be read.IllegalArgumentException
- If the field value could not be read.public static Object invokeMethod(Object obj, String methodName, Class<?> argType, Object arg, boolean throwException) throws IllegalArgumentException
obj
- The object.methodName
- The method name.argType
- The type of the parameter.arg
- The argument value.throwException
- Whether to throw an exception on failure.IllegalArgumentException
- If the method could not be invoked.public static Object invokeStaticMethod(Class<?> cls, String methodName, boolean throwException) throws IllegalArgumentException
cls
- The class.methodName
- The method name.throwException
- Whether to throw an exception on failure.IllegalArgumentException
- If the method could not be invoked.public static Object invokeStaticMethod(Class<?> cls, String methodName, Class<?> argType, Object arg, boolean throwException) throws IllegalArgumentException
cls
- The class.methodName
- The method name.argType
- The type of the parameter.arg
- The argument value.throwException
- Whether to throw an exception on failure.IllegalArgumentException
- If the method could not be invoked.public static Object invokeDefaultMethod(Class<?> cls, String methodName, Class<?> returnType, ClassLoader classLoader, boolean throwException) throws IllegalArgumentException
cls
- The object.methodName
- The method name.returnType
- The return type of the method.classLoader
- The ClassLoader.throwException
- Whether to throw an exception on failure.IllegalArgumentException
- If the method could not be invoked.public static Class<?> classForNameOrNull(String className)
className
- The class name to load.Copyright © 2018. All rights reserved.