Package com.tridion.util
Class ReflectionUtil
java.lang.Object
com.tridion.util.ReflectionUtil
This is a utility class that contains utility methods used to instantiate
classes with reflection and deals with the exception handling.
-
Method Summary
Modifier and TypeMethodDescriptionstatic ObjectgetPrivateField(Class fieldOwner, Object fieldOwnerInstance, String fieldName) Gets the value of a private field declared by the provided Class from the provided instance.static ObjectgetPrivateField(Object fieldOwner, String fieldName) static ObjectgetPrivateFieldWithinSuperClasses(Object fieldOwner, String fieldName) static ObjectinvokePrivateMethod(Object o, String methodName, Object[] params) Invoke private method of an object using reflection.static ObjectinvokePrivateStaticMethod(Class c, String methodName, Object[] params) Invoke static private method of an object using reflection.static <T> TloadClassInstance(String className, Class<T> subType, Object... classParams) This methods returns an instance of the specified class and is of a given subtype.static <T> TloadClassInstanceWithType(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> TloadClassInstanceWithTypes(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 voidsetPrivateField(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 voidsetPrivateField(Object fieldOwner, String fieldName, Object value)
-
Method Details
-
loadClassInstanceWithType
public static <T> T loadClassInstanceWithType(String className, Class<T> subType, Class<?> classType, Object classParam) throws TridionReflectionException This methods returns an instance of the specified class and is of a given subtype.- Type Parameters:
T- This is the expected return type of this class instantiator- Parameters:
className- 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 class- Returns:
- The instance of the requested class
- Throws:
TridionReflectionException- If unable to load the target class with the given parameters
-
loadClassInstanceWithTypes
public static <T> T loadClassInstanceWithTypes(String className, Class<T> subType, Class<?>[] constructorTypes, Object... classParams) throws TridionReflectionException This methods returns an instance of the specified class and is of a given subtype.- Type Parameters:
T- This is the expected return type of this class instantiator- Parameters:
className- 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 class- Returns:
- The instance of the requested class
- Throws:
TridionReflectionException- If unable to load the target class with the given parameters
-
loadClassInstance
public static <T> T loadClassInstance(String className, Class<T> subType, Object... classParams) throws TridionReflectionException This methods returns an instance of the specified class and is of a given subtype.- Type Parameters:
T- This is the expected return type of this class instantiator- Parameters:
className- 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 class- Returns:
- The instance of the requested class
- Throws:
TridionReflectionException- If unable to load the target class with the given parameters
-
invokePrivateMethod
public static Object invokePrivateMethod(Object o, String methodName, Object[] params) throws TridionReflectionException Invoke private method of an object using reflection.- Parameters:
o- The object with the private method to runmethodName- The private method nameparams- Parameter to be passed to the method call- Returns:
- An object returned by the method
- Throws:
TridionReflectionException- If unable to load the target method with the given parameters
-
invokePrivateStaticMethod
public static Object invokePrivateStaticMethod(Class c, String methodName, Object[] params) throws TridionReflectionException Invoke static private method of an object using reflection.- Parameters:
c- The class with the static private method to runmethodName- The private method nameparams- Parameter to be passed to the method call- Returns:
- An object returned by the method
- Throws:
TridionReflectionException- If unable to load the target method with the given parameters
-
getPrivateField
public static Object getPrivateField(Object fieldOwner, String fieldName) throws NoSuchFieldException, IllegalAccessException -
getPrivateField
public static Object getPrivateField(Class fieldOwner, Object fieldOwnerInstance, String fieldName) throws NoSuchFieldException, IllegalAccessException Gets the value of a private field declared by the provided Class from the provided instance.- Parameters:
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.- Returns:
- the private field
- Throws:
NoSuchFieldExceptionIllegalAccessException
-
getPrivateFieldWithinSuperClasses
public static Object getPrivateFieldWithinSuperClasses(Object fieldOwner, String fieldName) throws IllegalAccessException, NoSuchFieldException -
setPrivateField
public static void setPrivateField(Object fieldOwner, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException -
setPrivateField
public static void setPrivateField(Class fieldOwner, Object fieldOwnerInstance, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException Sets a value to a private field of the provided instance and which is declared by the provided Class.- Parameters:
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 value- Throws:
NoSuchFieldExceptionIllegalAccessException
-