mockit.internal.expectations.injection.FullInjection 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 mocking/faking APIs and a code coverage tool, supporting both JUnit and TestNG.
The mocking APIs allow all kinds of Java code, without testability restrictions, to be tested
in isolation from selected dependencies.
/*
* Copyright (c) 2006-2015 Rogério Liesenfeld
* This file is subject to the terms of the MIT license (see LICENSE.txt).
*/
package mockit.internal.expectations.injection;
import java.lang.annotation.*;
import java.lang.reflect.*;
import java.util.logging.*;
import javax.annotation.*;
import javax.enterprise.context.*;
import javax.inject.*;
import static java.lang.reflect.Modifier.*;
import mockit.internal.startup.*;
import mockit.internal.util.*;
import static mockit.external.asm.Opcodes.*;
import static mockit.internal.expectations.injection.InjectionPoint.*;
import static mockit.internal.util.ConstructorReflection.*;
import static mockit.internal.util.Utilities.*;
/**
* Responsible for recursive injection of dependencies as requested by a {@code @Tested(fullyInitialized = true)} field.
*/
final class FullInjection
{
private static final int INVALID_TYPES = ACC_ABSTRACT + ACC_ANNOTATION + ACC_ENUM;
@Nonnull private final InjectionState injectionState;
@Nullable private final ServletDependencies servletDependencies;
@Nullable private final JPADependencies jpaDependencies;
FullInjection(@Nonnull InjectionState injectionState)
{
this.injectionState = injectionState;
servletDependencies = SERVLET_CLASS == null ? null : new ServletDependencies(injectionState);
jpaDependencies = PERSISTENCE_UNIT_CLASS == null ? null : new JPADependencies(injectionState);
}
@Nullable
Object newInstance(
@Nonnull TestedClass testedClass, @Nonnull Injector injector, @Nonnull InjectionPointProvider injectionProvider,
@Nullable String qualifiedName)
{
Object dependencyKey = getDependencyKey(injectionProvider, qualifiedName);
Object dependency = injectionState.getInstantiatedDependency(dependencyKey);
if (dependency != null) {
return dependency;
}
Class> typeToInject = injectionProvider.getClassOfDeclaredType();
if (typeToInject == Logger.class) {
return Logger.getLogger(testedClass.nameOfTestedClass);
}
if (!isInstantiableType(typeToInject)) {
return null;
}
if (INJECT_CLASS != null && typeToInject == Provider.class) {
dependency = createProviderInstance(injectionProvider, dependencyKey);
}
else if (servletDependencies != null && ServletDependencies.isApplicable(typeToInject)) {
dependency = servletDependencies.createAndRegisterDependency(typeToInject);
}
else if (CONVERSATION_CLASS != null && typeToInject == Conversation.class) {
dependency = createAndRegisterConversationInstance();
}
else {
dependency = createAndRegisterNewInstance(testedClass, injector, injectionProvider, dependencyKey);
}
return dependency;
}
private static boolean isInstantiableType(@Nonnull Class> type)
{
if (type.isPrimitive() || type.isArray() || type.isAnnotation()) {
return false;
}
if (!type.isInterface()) {
int typeModifiers = type.getModifiers();
if ((typeModifiers & INVALID_TYPES) != 0 || !isStatic(typeModifiers) && type.isMemberClass()) {
return false;
}
}
return true;
}
@Nonnull
private Object getDependencyKey(@Nonnull InjectionPointProvider injectionProvider, @Nullable String qualifiedName)
{
Class> dependencyClass = injectionProvider.getClassOfDeclaredType();
if (qualifiedName != null && !qualifiedName.isEmpty()) {
return dependencyKey(dependencyClass, qualifiedName);
}
if (jpaDependencies != null && JPADependencies.isApplicable(dependencyClass)) {
for (Annotation annotation : injectionProvider.getAnnotations()) {
String id = JPADependencies.getDependencyIdIfAvailable(annotation);
if (id != null && !id.isEmpty()) {
return dependencyKey(dependencyClass, id);
}
}
}
return injectionState.typeOfInjectionPoint;
}
@Nonnull
private Object createProviderInstance(
@Nonnull InjectionPointProvider injectionProvider, @Nonnull final Object dependencyKey)
{
ParameterizedType genericType = (ParameterizedType) injectionProvider.getDeclaredType();
final Class> providedClass = (Class>) genericType.getActualTypeArguments()[0];
if (providedClass.isAnnotationPresent(Singleton.class)) {
return new Provider