mockit.internal.expectations.injection.InjectionPoint 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 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 javax.annotation.*;
import javax.ejb.*;
import javax.inject.*;
import javax.persistence.*;
import javax.servlet.*;
import static mockit.internal.util.ClassLoad.*;
import static mockit.internal.util.MethodReflection.*;
import static mockit.internal.util.ParameterReflection.*;
import static mockit.internal.util.Utilities.*;
final class InjectionPoint
{
enum KindOfInjectionPoint { NotAnnotated, Required, Optional, WithValue }
@Nullable static final Class extends Annotation> INJECT_CLASS;
@Nullable private static final Class extends Annotation> EJB_CLASS;
@Nullable static final Class extends Annotation> PERSISTENCE_UNIT_CLASS;
@Nullable static final Class> SERVLET_CLASS;
@Nullable static final Class> CONVERSATION_CLASS;
static final boolean WITH_INJECTION_API_IN_CLASSPATH;
static
{
INJECT_CLASS = searchTypeInClasspath("javax.inject.Inject");
EJB_CLASS = searchTypeInClasspath("javax.ejb.EJB");
PERSISTENCE_UNIT_CLASS = searchTypeInClasspath("javax.persistence.PersistenceUnit");
SERVLET_CLASS = searchTypeInClasspath("javax.servlet.Servlet");
CONVERSATION_CLASS = searchTypeInClasspath("javax.enterprise.context.Conversation");
WITH_INJECTION_API_IN_CLASSPATH = INJECT_CLASS != null || PERSISTENCE_UNIT_CLASS != null;
}
static boolean isServlet(@Nonnull Class> aClass)
{
return SERVLET_CLASS != null && Servlet.class.isAssignableFrom(aClass);
}
private InjectionPoint() {}
@Nonnull
static Object wrapInProviderIfNeeded(@Nonnull Type type, @Nonnull final Object value)
{
if (
INJECT_CLASS != null && type instanceof ParameterizedType && !(value instanceof Provider) &&
((ParameterizedType) type).getRawType() == Provider.class
) {
return new Provider