mockit.internal.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.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
{
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");
}
@Nonnull final Type type;
@Nullable final String name;
InjectionPoint(@Nonnull Type type) { this(type, null); }
InjectionPoint(@Nonnull Type type, @Nullable String name)
{
this.type = type;
this.name = name;
}
@Override @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
public boolean equals(Object other)
{
if (this == other) return true;
InjectionPoint otherIP = (InjectionPoint) other;
if (type instanceof TypeVariable> || otherIP.type instanceof TypeVariable>) {
return false;
}
String thisName = name;
String otherName = otherIP.name;
if (thisName != null && !thisName.equals(otherName)) {
return false;
}
Class> thisClass = getClassType(type);
Class> otherClass = getClassType(otherIP.type);
return thisClass.isAssignableFrom(otherClass);
}
@Override
public int hashCode() { return 31 * type.hashCode() + (name != null ? name.hashCode() : 0); }
static boolean isServlet(@Nonnull Class> aClass)
{
return SERVLET_CLASS != null && Servlet.class.isAssignableFrom(aClass);
}
@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