public final class ReflectionUtils extends Object
ReflectionUtils
- Reflection utilities holder.Modifier and Type | Method and Description |
---|---|
static <T> Class<T> |
getClass(String className)
Get the Java
Class with the given fully-qualified name. |
static <T> Class<T> |
getClassOrNull(String className)
|
static Field |
getField(Class targetClass,
String fieldName)
|
static Field |
getFieldOrNull(Class targetClass,
String fieldName)
|
static <T> T |
getFieldValue(Object target,
String fieldName)
Get the value of the field matching the given name and belonging to target
Object . |
static <T> T |
getFieldValueOrNull(Object target,
String fieldName)
|
static <T> Class<T> |
getFirstSuperClassFromPackage(Class clazz,
String packageName)
Get the first super class matching the provided package name.
|
static Method |
getGetter(Object target,
String propertyName)
|
static Type |
getMemberGenericTypeOrNull(Class targetClass,
String memberName)
|
static Member |
getMemberOrNull(Class targetClass,
String memberName)
|
static Method |
getMethod(Class targetClass,
String methodName,
Class... parameterTypes)
|
static Method |
getMethod(Object target,
String methodName,
Class... parameterTypes)
|
static Method |
getMethodOrNull(Object target,
String methodName,
Class... parameterTypes)
|
static Method |
getSetter(Object target,
String propertyName,
Class<?> parameterType)
|
static Class<?> |
getWrapperClass(Class<?> clazz)
Get the Java Wrapper
Class associated to the given primitive type. |
static boolean |
hasMethod(Class<?> targetClass,
String methodName,
Class... parameterTypes)
Check if the provided Java
Class contains a method matching
the given signature (name and parameter types). |
static <T> T |
invokeGetter(Object target,
String propertyName)
Invoke the property getter with the provided name on the given Java
Object . |
static <T> T |
invokeMethod(Object target,
Method method,
Object... parameters)
|
static <T> T |
invokeMethod(Object target,
String methodName,
Object... parameters)
Invoke the method with the provided signature (name and parameter types)
on the given Java
Object . |
static void |
invokeSetter(Object target,
String propertyName,
boolean parameter)
Invoke the
boolean property setter with the provided name
on the given Java Object . |
static void |
invokeSetter(Object target,
String propertyName,
int parameter)
Invoke the
int property setter with the provided name
on the given Java Object . |
static void |
invokeSetter(Object target,
String propertyName,
Object parameter)
Invoke the property setter with the provided signature (name and parameter types)
on the given Java
Object . |
static <T> T |
invokeStaticMethod(Method method,
Object... parameters)
Invoke the
static Method with the provided parameters. |
static <T> T |
newInstance(Class clazz)
Instantiate a new
Object of the provided type. |
static <T> T |
newInstance(String className)
Instantiate a new
Object of the provided type. |
static void |
setFieldValue(Object target,
String fieldName,
Object value)
Set the value of the field matching the given name and belonging to target
Object . |
public static <T> T newInstance(String className)
Object
of the provided type.public static <T> T newInstance(Class clazz)
Object
of the provided type.public static <T> T getFieldValue(Object target, String fieldName)
Object
.T
- field typetarget
- target Object
whose field we are retrieving the value fromfieldName
- field namepublic static <T> T getFieldValueOrNull(Object target, String fieldName)
Object
or null
if no Field
was found..T
- field typetarget
- target Object
whose field we are retrieving the value fromfieldName
- field namenull
public static void setFieldValue(Object target, String fieldName, Object value)
Object
.target
- target objectfieldName
- field namevalue
- field valuepublic static Method getMethodOrNull(Object target, String methodName, Class... parameterTypes)
public static Method getMethod(Class targetClass, String methodName, Class... parameterTypes)
public static boolean hasMethod(Class<?> targetClass, String methodName, Class... parameterTypes)
Class
contains a method matching
the given signature (name and parameter types).public static Method getSetter(Object target, String propertyName, Class<?> parameterType)
public static <T> T invokeMethod(Object target, String methodName, Object... parameters)
Object
.T
- return value object typetarget
- target Object
whose method we are invokingmethodName
- method name to invokeparameters
- parameters passed to the method callpublic static <T> T invokeGetter(Object target, String propertyName)
Object
.T
- return value object typetarget
- target Object
whose property getter we are invokingpropertyName
- property name whose getter we are invokingpublic static void invokeSetter(Object target, String propertyName, Object parameter)
Object
.target
- target Object
whose property setter we are invokingpropertyName
- property name whose setter we are invokingparameter
- parameter passed to the setter callpublic static void invokeSetter(Object target, String propertyName, boolean parameter)
boolean
property setter with the provided name
on the given Java Object
.target
- target Object
whose property setter we are invokingpropertyName
- property name whose setter we are invokingparameter
- boolean
parameter passed to the setter callpublic static void invokeSetter(Object target, String propertyName, int parameter)
int
property setter with the provided name
on the given Java Object
.target
- target Object
whose property setter we are invokingpropertyName
- property name whose setter we are invokingparameter
- int
parameter passed to the setter callpublic static <T> T invokeStaticMethod(Method method, Object... parameters)
static
Method
with the provided parameters.T
- return value object typemethod
- target static
Method
to invokeparameters
- parameters passed to the method callpublic static <T> Class<T> getClass(String className)
Class
with the given fully-qualified name.public static Class<?> getWrapperClass(Class<?> clazz)
Class
associated to the given primitive type.clazz
- primitive classClass
public static <T> Class<T> getFirstSuperClassFromPackage(Class clazz, String packageName)
T
- class generic typeclazz
- Java classpackageName
- package namenull
.Copyright © 2020. All rights reserved.