mockit.internal.injection.constructor.ConstructorInjection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jmockit Show documentation
Show all versions of jmockit Show documentation
JMockit is a Java toolkit for automated developer testing.
It contains APIs for the creation of the objects to be tested, for mocking dependencies, and for faking external
APIs; JUnit (4 & 5) and TestNG test runners are supported.
It also contains an advanced code coverage tool.
/*
* Copyright (c) 2006 Rogério Liesenfeld
* This file is subject to the terms of the MIT license (see LICENSE.txt).
*/
package mockit.internal.injection.constructor;
import java.lang.reflect.*;
import java.util.*;
import javax.annotation.*;
import mockit.internal.expectations.mocking.*;
import mockit.internal.injection.*;
import mockit.internal.injection.full.*;
import mockit.internal.state.*;
import mockit.internal.util.*;
import static mockit.internal.injection.InjectionPoint.*;
import static mockit.internal.injection.InjectionProvider.NULL;
import static mockit.internal.reflection.ConstructorReflection.*;
import static mockit.internal.util.Utilities.*;
public final class ConstructorInjection extends Injector
{
@Nonnull private final Constructor> constructor;
public ConstructorInjection(
@Nonnull InjectionState injectionState, @Nullable FullInjection fullInjection,
@Nonnull Constructor> constructor)
{
super(injectionState, fullInjection);
this.constructor = constructor;
}
@Nonnull
public Object instantiate(@Nonnull List parameterProviders, @Nonnull TestedClass testedClass)
{
Type[] parameterTypes = constructor.getGenericParameterTypes();
int n = parameterTypes.length;
List consumedInjectables = n == 0 ? null : injectionState.saveConsumedInjectionProviders();
Object[] arguments = n == 0 ? NO_ARGS : new Object[n];
boolean varArgs = constructor.isVarArgs();
if (varArgs) {
n--;
}
for (int i = 0; i < n; i++) {
@Nonnull InjectionProvider parameterProvider = parameterProviders.get(i);
Object value;
if (parameterProvider instanceof ConstructorParameter) {
value = createOrReuseArgumentValue((ConstructorParameter) parameterProvider);
}
else {
value = getArgumentValueToInject(parameterProvider, i);
}
if (value != null) {
Type parameterType = parameterTypes[i];
arguments[i] = wrapInProviderIfNeeded(parameterType, value);
}
}
if (varArgs) {
Type parameterType = parameterTypes[n];
arguments[n] = obtainInjectedVarargsArray(parameterType, testedClass);
}
if (consumedInjectables != null) {
injectionState.restoreConsumedInjectionProviders(consumedInjectables);
}
return invokeConstructor(arguments);
}
@Nonnull
private Object createOrReuseArgumentValue(@Nonnull ConstructorParameter constructorParameter)
{
Object value = constructorParameter.getValue(null);
if (value != null) {
return value;
}
Type parameterType = constructorParameter.getDeclaredType();
injectionState.setTypeOfInjectionPoint(parameterType);
String qualifiedName = getQualifiedName(constructorParameter.getAnnotations());
Class> parameterClass = constructorParameter.getClassOfDeclaredType();
TestedClass nextTestedClass = new TestedClass(parameterType, parameterClass);
assert fullInjection != null;
value = fullInjection.createOrReuseInstance(nextTestedClass, this, constructorParameter, qualifiedName);
if (value == null) {
String parameterName = constructorParameter.getName();
String message =
"Missing @Tested or @Injectable" + missingValueDescription(parameterName) +
"\r\n when initializing " + fullInjection;
throw new IllegalStateException(message);
}
return value;
}
@Nullable
private Object getArgumentValueToInject(@Nonnull InjectionProvider injectable, int parameterIndex)
{
Object argument = injectionState.getValueToInject(injectable);
if (argument == null) {
String classDesc = getClassDesc();
String constructorDesc = getConstructorDesc();
String parameterName = ParameterNames.getName(classDesc, constructorDesc, parameterIndex);
if (parameterName == null) {
parameterName = injectable.getName();
}
throw new IllegalArgumentException("No injectable value available" + missingValueDescription(parameterName));
}
return argument == NULL ? null : argument;
}
@Nonnull
private String getClassDesc() { return mockit.external.asm.Type.getInternalName(constructor.getDeclaringClass()); }
@Nonnull
private String getConstructorDesc()
{
return "" + mockit.external.asm.Type.getConstructorDescriptor(constructor);
}
@Nonnull
private Object obtainInjectedVarargsArray(@Nonnull Type parameterType, @Nonnull TestedClass testedClass)
{
Type varargsElementType = getTypeOfInjectionPointFromVarargsParameter(parameterType);
injectionState.setTypeOfInjectionPoint(varargsElementType);
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy