Class ReflectionUtils


  • public class ReflectionUtils
    extends Object
    Utility class that provides helper methods for reflection related tasks
    • Method Detail

      • createNewInstance

        public static <T> T createNewInstance​(Class<T> clazz,
                                              Object... constructorArgs)
        Creates a new instance of a provided class.

        Wrapper around ReflectionSupport.newInstance(Class, Object...) for more meaningful exceptions in case of failure

        Type Parameters:
        T - the type of the class to instantiate
        Parameters:
        clazz - the class to instantiate
        constructorArgs - the arguments of the constructor
        Returns:
        the instantiated class
        See Also:
        ReflectionSupport.newInstance(Class, Object...)
      • invokeMethod

        public static Object invokeMethod​(Method method,
                                          Object target,
                                          Object... args)
        Invokes the method on the target with the provided arguments

        Wrapper around ReflectionSupport.invokeMethod(Method, Object, Object...) for more meaningful exceptions in case of failure

        Parameters:
        method - the method to invoked
        target - the instance on which to invoke the method
        args - the arguments of the method
        Returns:
        the value the method returns
        See Also:
        ReflectionSupport.invokeMethod(Method, Object, Object...)
      • findRequiredMethod

        public static Method findRequiredMethod​(Class<?> clazz,
                                                String methodName,
                                                Class<?>... parameterTypes)
        Finds a method with provided parameter type in a class

        Wrapper around ReflectionSupport.findMethod(Class, String, Class[]) for more meaningful exceptions in case of failure

        Parameters:
        clazz - the class where to find the method
        methodName - the name of the method
        parameterTypes - the type of the parameters of the method
        Returns:
        the searched method
      • findQualifiedMethod

        public static Method findQualifiedMethod​(Class<?> clazz,
                                                 String methodName)
        Attempts to find the method defined by the given name. If the methodName is blank, a method in the class with the same name as the test method is returned. Otherwise, either the method with the given name in the class (if the methodName is not fully qualified) or the method in another class (if the method name is fully qualified) is returned. To fully qualify a method name, put the class name before the method name and separate them with a"#".
        Parameters:
        clazz - the class where a non-qualified method is found
        methodName - the name of the method which should be returned
        Returns:
        the method defined by the name with the rules explained above