public final class ReflectionUtil extends Object
Modifier and Type | Method and Description |
---|---|
static Object |
getPrivateField(Class fieldOwner,
Object fieldOwnerInstance,
String fieldName)
Gets the value of a private field declared by the provided Class from the provided instance.
|
static Object |
getPrivateField(Object fieldOwner,
String fieldName) |
static Object |
getPrivateFieldWithinSuperClasses(Object fieldOwner,
String fieldName) |
static Object |
invokePrivateMethod(Object o,
String methodName,
Object[] params)
Invoke private method of an object using reflection.
|
static Object |
invokePrivateStaticMethod(Class c,
String methodName,
Object[] params)
Invoke static private method of an object using reflection.
|
static <T> T |
loadClassInstance(String className,
Class<T> subType,
Object... classParams)
This methods returns an instance of the specified class and is of a given subtype.
|
static <T> T |
loadClassInstanceWithType(String className,
Class<T> subType,
Class<?> classType,
Object classParam)
This methods returns an instance of the specified class and is of a given subtype.
|
static <T> T |
loadClassInstanceWithTypes(String className,
Class<T> subType,
Class<?>[] constructorTypes,
Object... classParams)
This methods returns an instance of the specified class and is of a given subtype.
|
static void |
setPrivateField(Class fieldOwner,
Object fieldOwnerInstance,
String fieldName,
Object value)
Sets a value to a private field of the provided instance and which is declared by the provided Class.
|
static void |
setPrivateField(Object fieldOwner,
String fieldName,
Object value) |
public static <T> T loadClassInstanceWithType(String className, Class<T> subType, Class<?> classType, Object classParam) throws TridionReflectionException
T
- This is the expected return type of this class instantiatorclassName
- The name of the class to load from the classpathsubType
- The expected subtype of the instantiated classclassType
- The classType of the parameter in the constructorclassParam
- The parameters needed to be passed to a constructor, they will
be used to search for a valid constructor in the target classTridionReflectionException
- If unable to load the target class with the given parameterspublic static <T> T loadClassInstanceWithTypes(String className, Class<T> subType, Class<?>[] constructorTypes, Object... classParams) throws TridionReflectionException
T
- This is the expected return type of this class instantiatorclassName
- The name of the class to load from the classpathsubType
- The expected subtype of the instantiated classconstructorTypes
- The types expected in the constructorclassParams
- The parameters needed to be passed to a constructor, they will
be used to search for a valid constructor in the target classTridionReflectionException
- If unable to load the target class with the given parameterspublic static <T> T loadClassInstance(String className, Class<T> subType, Object... classParams) throws TridionReflectionException
T
- This is the expected return type of this class instantiatorclassName
- The name of the class to load from the classpathsubType
- The expected subtype of the instantiated classclassParams
- The parameters needed to be passed to a constructor, they will
be used to search for a valid constructor in the target classTridionReflectionException
- If unable to load the target class with the given parameterspublic static Object invokePrivateMethod(Object o, String methodName, Object[] params) throws TridionReflectionException
o
- The object with the private method to runmethodName
- The private method nameparams
- Parameter to be passed to the method callTridionReflectionException
- If unable to load the target method with the given parameterspublic static Object invokePrivateStaticMethod(Class c, String methodName, Object[] params) throws TridionReflectionException
c
- The class with the static private method to runmethodName
- The private method nameparams
- Parameter to be passed to the method callTridionReflectionException
- If unable to load the target method with the given parameterspublic static Object getPrivateField(Object fieldOwner, String fieldName) throws NoSuchFieldException, IllegalAccessException
public static Object getPrivateField(Class fieldOwner, Object fieldOwnerInstance, String fieldName) throws NoSuchFieldException, IllegalAccessException
fieldOwner
- The instance whose field value we want to get. null if it is static field.fieldOwnerInstance
- The Class that declares the desired field.fieldName
- The name of the field.NoSuchFieldException
IllegalAccessException
public static Object getPrivateFieldWithinSuperClasses(Object fieldOwner, String fieldName) throws IllegalAccessException, NoSuchFieldException
public static void setPrivateField(Object fieldOwner, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException
public static void setPrivateField(Class fieldOwner, Object fieldOwnerInstance, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException
fieldOwner
- the instance whose field value we want to set. null if it is static field.fieldOwnerInstance
- the Class that declares the desired fieldfieldName
- the name of the fieldvalue
- the field valueNoSuchFieldException
IllegalAccessException
Copyright (c) 2014-2021 All Rights Reserved by the RWS Group for and on behalf of its affiliates and subsidiaries