/** * Invoke the supplied {@code static} method with dynamic parameter resolution. * * @param method the method to invoke and resolve parameters for * @param extensionContext the current {@code ExtensionContext} * @param extensionRegistry the {@code ExtensionRegistry} to retrieve * {@code ParameterResolvers} from */ public Object invoke(Method method, ExtensionContext extensionContext, ExtensionRegistry extensionRegistry) { return ReflectionUtils.invokeMethod(method, null, resolveParameters(method, Optional.empty(), extensionContext, extensionRegistry)); }
@Override public Stream<Arguments> provideArguments(ExtensionContext context) { Object testInstance = context.getTestInstance().orElse(null); // @formatter:off return Arrays.stream(this.methodNames) .map(factoryMethodName -> getMethod(context, factoryMethodName)) .map(method -> ReflectionUtils.invokeMethod(method, testInstance)) .flatMap(CollectionUtils::toStream) .map(MethodArgumentsProvider::toArguments); // @formatter:on }
/** * Invoke the supplied method on the supplied target object with dynamic parameter * resolution. * * @param method the method to invoke and resolve parameters for * @param target the object on which the method will be invoked; should be * {@code null} for static methods * @param extensionContext the current {@code ExtensionContext} * @param extensionRegistry the {@code ExtensionRegistry} to retrieve * {@code ParameterResolvers} from */ public Object invoke(Method method, Object target, ExtensionContext extensionContext, ExtensionRegistry extensionRegistry) { @SuppressWarnings("unchecked") Optional<Object> optionalTarget = (target instanceof Optional ? (Optional<Object>) target : Optional.ofNullable(target)); return ReflectionUtils.invokeMethod(method, target, resolveParameters(method, optionalTarget, extensionContext, extensionRegistry)); }
Object version = ReflectionUtils.invokeMethod(versionMethod, null); Method majorMethod = version.getClass().getMethod("major"); int major = (int) ReflectionUtils.invokeMethod(majorMethod, version); switch (major) { case 9:
private static Function<String, Object> findFactoryExecutable(Class<?> targetType) { return factoryExecutableCache.computeIfAbsent(targetType, type -> { Method factoryMethod = findFactoryMethod(type); if (factoryMethod != null) { return source -> invokeMethod(factoryMethod, null, source); } Constructor<?> constructor = findFactoryConstructor(type); if (constructor != null) { return source -> newInstance(constructor, source); } return NULL_EXECUTABLE; }); }
public TestRuleAnnotatedMethod(Object testInstance, Method method) { super((TestRule) ReflectionUtils.invokeMethod(method, testInstance)); }
TestRuleAnnotatedMethod(Object testInstance, Method method) { super((TestRule) ReflectionUtils.invokeMethod(method, testInstance)); }
protected Object executeMethod(String methodName, Class<?>[] parameterTypes, Object... arguments) { Method method = findMethod(this.target.getClass(), methodName, parameterTypes).orElseThrow( () -> new JUnitException(String.format("Failed to find method %s(%s) in class %s", methodName, ClassUtils.nullSafeToString(parameterTypes), this.target.getClass().getName()))); return invokeMethod(method, this.target, arguments); }
protected Object executeMethod(String methodName, Class<?>[] parameterTypes, Object... arguments) { Method method = findMethod(this.target.getClass(), methodName, parameterTypes).orElseThrow( () -> new JUnitException(String.format("Failed to find method %s(%s) in class %s", methodName, StringUtils.nullSafeToString(parameterTypes), this.target.getClass().getName()))); return invokeMethod(method, this.target, arguments); }