Class ReflectionUtils


  • public final class ReflectionUtils
    extends Object
    Author:
    Kristian Rosenvold
    • Method Detail

      • tryGetMethod

        public static Method tryGetMethod​(Class<?> clazz,
                                          String methodName,
                                          Class<?>... parameters)
      • invokeGetter

        public static <T> T invokeGetter​(Object instance,
                                         String methodName)
      • invokeGetter

        public static <T> T invokeGetter​(Class<?> instanceType,
                                         Object instance,
                                         String methodName)
      • getConstructor

        public static Constructor<?> getConstructor​(Class<?> clazz,
                                                    Class<?>... arguments)
      • tryGetConstructor

        public static <T> Constructor<T> tryGetConstructor​(Class<T> clazz,
                                                           Class<?>... arguments)
      • newInstance

        public static <T> T newInstance​(Constructor<?> constructor,
                                        Object... params)
      • instantiate

        public static <T> T instantiate​(ClassLoader classLoader,
                                        String classname,
                                        Class<T> returnType)
      • instantiateOneArg

        public static <T> T instantiateOneArg​(ClassLoader classLoader,
                                              String className,
                                              Class<?> param1Class,
                                              Object param1)
      • invokeSetter

        public static <T> T invokeSetter​(Object target,
                                         Method method,
                                         Object value)
      • invokeMethodWithArray

        public static <T> T invokeMethodWithArray​(Object target,
                                                  Method method,
                                                  Object... args)
      • invokeStaticMethod

        public static <T> T invokeStaticMethod​(Class<?> clazz,
                                               String methodName,
                                               Class<?>[] parameterTypes,
                                               Object[] parameters)
        Invoker of public static no-argument method.
        Parameters:
        clazz - class on which public static no-argument methodName is invoked
        methodName - public static no-argument method to be called
        parameterTypes - method parameter types
        parameters - method parameters
        Returns:
        value returned by methodName
        Throws:
        RuntimeException - if no such method found
        SurefireReflectionException - if the method could not be called or threw an exception. It has original cause Exception.
      • invokeMethodChain

        public static <T> T invokeMethodChain​(Class<?>[] classesChain,
                                              String[] noArgMethodNames,
                                              Object fallback)
        Method chain invoker.
        Parameters:
        classesChain - classes to invoke on method chain
        noArgMethodNames - chain of public methods to call
        fallback - returned value if a chain could not be invoked due to an error
        Returns:
        successfully returned value from the last method call; fallback otherwise
        Throws:
        IllegalArgumentException - if classes and noArgMethodNames have different array length