Class ReflectionUtil

java.lang.Object
com.tridion.util.ReflectionUtil

public final class ReflectionUtil extends Object
This is a utility class that contains utility methods used to instantiate classes with reflection and deals with the exception handling.
  • 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 classpath
      subType - The expected subtype of the instantiated class
      classType - The classType of the parameter in the constructor
      classParam - 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 classpath
      subType - The expected subtype of the instantiated class
      constructorTypes - The types expected in the constructor
      classParams - 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 classpath
      subType - The expected subtype of the instantiated class
      classParams - 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 run
      methodName - The private method name
      params - 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 run
      methodName - The private method name
      params - 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
      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:
      NoSuchFieldException
      IllegalAccessException
    • getPrivateFieldWithinSuperClasses

      public static Object getPrivateFieldWithinSuperClasses(Object fieldOwner, String fieldName) throws IllegalAccessException, NoSuchFieldException
      Throws:
      IllegalAccessException
      NoSuchFieldException
    • setPrivateField

      public static void setPrivateField(Object fieldOwner, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException
      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 field
      fieldName - the name of the field
      value - the field value
      Throws:
      NoSuchFieldException
      IllegalAccessException