package org.junit.jupiter.engine.execution;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static org.apiguardian.api.API.Status.INTERNAL;
import static org.junit.platform.commons.util.ReflectionUtils.isAssignableTo;
import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.List;
import java.util.Optional;
import org.apiguardian.api.API;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.InvocationInterceptor;
import org.junit.jupiter.api.extension.InvocationInterceptor.Invocation;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;
import org.junit.jupiter.api.extension.ReflectiveInvocationContext;
import org.junit.jupiter.engine.extension.ExtensionRegistry;
import org.junit.platform.commons.logging.Logger;
import org.junit.platform.commons.logging.LoggerFactory;
import org.junit.platform.commons.util.Preconditions;
import org.junit.platform.commons.util.StringUtils;
import org.junit.platform.commons.util.UnrecoverableExceptions;
@API(status = INTERNAL, since = "5.0")
public class ExecutableInvoker {
private static final Logger logger = LoggerFactory.getLogger(ExecutableInvoker.class);
private static final InvocationInterceptorChain interceptorChain = new InvocationInterceptorChain();
public <T> T invoke(Constructor<T> constructor, Optional<Object> outerInstance, ExtensionContext extensionContext,
ExtensionRegistry extensionRegistry, ReflectiveInterceptorCall<Constructor<T>, T> interceptorCall) {
Object[] arguments = resolveParameters(constructor, Optional.empty(), outerInstance, extensionContext,
extensionRegistry);
ConstructorInvocation<T> invocation = new ConstructorInvocation<>(constructor, arguments);
return invoke(invocation, invocation, extensionContext, extensionRegistry, interceptorCall);
}
public <T> T invoke(Method method, Object target, ExtensionContext extensionContext,
ExtensionRegistry extensionRegistry, ReflectiveInterceptorCall<Method, T> interceptorCall) {
@SuppressWarnings("unchecked")
Optional<Object> optionalTarget = (target instanceof Optional ? (Optional<Object>) target
: Optional.ofNullable(target));
Object[] arguments = resolveParameters(method, optionalTarget, extensionContext, extensionRegistry);
MethodInvocation<T> invocation = new MethodInvocation<>(method, optionalTarget, arguments);
return invoke(invocation, invocation, extensionContext, extensionRegistry, interceptorCall);
}
private <E extends Executable, T> T invoke(Invocation<T> originalInvocation,
ReflectiveInvocationContext<E> invocationContext, ExtensionContext extensionContext,
ExtensionRegistry extensionRegistry, ReflectiveInterceptorCall<E, T> call) {
return interceptorChain.invoke(originalInvocation, extensionRegistry, (interceptor,
wrappedInvocation) -> call.apply(interceptor, wrappedInvocation, invocationContext, extensionContext));
}
public interface ReflectiveInterceptorCall<E extends Executable, T> {
T apply(InvocationInterceptor interceptor, Invocation<T> invocation,
ReflectiveInvocationContext<E> invocationContext, ExtensionContext extensionContext) throws Throwable;
static ReflectiveInterceptorCall<Method, Void> ofVoidMethod(VoidMethodInterceptorCall call) {
return ((interceptorChain, invocation, invocationContext, extensionContext) -> {
call.apply(interceptorChain, invocation, invocationContext, extensionContext);
return null;
});
}
interface VoidMethodInterceptorCall {
void apply(InvocationInterceptor interceptor, Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext)
throws Throwable;
}
}
private Object[] resolveParameters(Method method, Optional<Object> target, ExtensionContext extensionContext,
ExtensionRegistry extensionRegistry) {
return resolveParameters(method, target, Optional.empty(), extensionContext, extensionRegistry);
}
private Object[] resolveParameters(Executable executable, Optional<Object> target, Optional<Object> outerInstance,
ExtensionContext extensionContext, ExtensionRegistry extensionRegistry) {
Preconditions.notNull(target, "target must not be null");
Parameter[] parameters = executable.getParameters();
Object[] values = new Object[parameters.length];
int start = 0;
if (outerInstance.isPresent()) {
values[0] = outerInstance.get();
start = 1;
}
for (int i = start; i < parameters.length; i++) {
ParameterContext parameterContext = new DefaultParameterContext(parameters[i], i, target);
values[i] = resolveParameter(parameterContext, executable, extensionContext, extensionRegistry);
}
return values;
}
private Object resolveParameter(ParameterContext parameterContext, Executable executable,
ExtensionContext extensionContext, ExtensionRegistry extensionRegistry) {
try {
List<ParameterResolver> matchingResolvers = extensionRegistry.stream(ParameterResolver.class)
.filter(resolver -> resolver.supportsParameter(parameterContext, extensionContext))
.collect(toList());
if (matchingResolvers.isEmpty()) {
throw new ParameterResolutionException(
String.format("No ParameterResolver registered for parameter [%s] in %s [%s].",
parameterContext.getParameter(), asLabel(executable), executable.toGenericString()));
}
if (matchingResolvers.size() > 1) {
String resolvers = matchingResolvers.stream()
.map(StringUtils::defaultToString)
.collect(joining(", "));
throw new ParameterResolutionException(
String.format("Discovered multiple competing ParameterResolvers for parameter [%s] in %s [%s]: %s",
parameterContext.getParameter(), asLabel(executable), executable.toGenericString(), resolvers));
}
ParameterResolver resolver = matchingResolvers.get(0);
Object value = resolver.resolveParameter(parameterContext, extensionContext);
validateResolvedType(parameterContext.getParameter(), value, executable, resolver);
logger.trace(() -> String.format(
"ParameterResolver [%s] resolved a value of type [%s] for parameter [%s] in %s [%s].",
resolver.getClass().getName(), (value != null ? value.getClass().getName() : null),
parameterContext.getParameter(), asLabel(executable), executable.toGenericString()));
return value;
}
catch (ParameterResolutionException ex) {
throw ex;
}
catch (Throwable throwable) {
UnrecoverableExceptions.rethrowIfUnrecoverable(throwable);
String message = String.format("Failed to resolve parameter [%s] in %s [%s]",
parameterContext.getParameter(), asLabel(executable), executable.toGenericString());
if (StringUtils.isNotBlank(throwable.getMessage())) {
message += ": " + throwable.getMessage();
}
throw new ParameterResolutionException(message, throwable);
}
}
private void validateResolvedType(Parameter parameter, Object value, Executable executable,
ParameterResolver resolver) {
Class<?> type = parameter.getType();
if (!isAssignableTo(value, type)) {
String message;
if (value == null && type.isPrimitive()) {
message = String.format(
"ParameterResolver [%s] resolved a null value for parameter [%s] "
+ "in %s [%s], but a primitive of type [%s] is required.",
resolver.getClass().getName(), parameter, asLabel(executable), executable.toGenericString(),
type.getName());
}
else {
message = String.format(
"ParameterResolver [%s] resolved a value of type [%s] for parameter [%s] "
+ "in %s [%s], but a value assignment compatible with [%s] is required.",
resolver.getClass().getName(), (value != null ? value.getClass().getName() : null), parameter,
asLabel(executable), executable.toGenericString(), type.getName());
}
throw new ParameterResolutionException(message);
}
}
private static String asLabel(Executable executable) {
return executable instanceof Constructor ? "constructor" : "method";
}
}