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 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;
import java.lang.annotation.*;
import java.lang.reflect.*;
import java.util.*;
import javax.annotation.*;
import javax.ejb.*;
import javax.enterprise.inject.*;
import javax.enterprise.util.*;
import javax.inject.*;
import javax.persistence.*;
import javax.servlet.*;
import static java.lang.Character.toUpperCase;
import static mockit.internal.util.ClassLoad.*;
import static mockit.internal.reflection.MethodReflection.*;
import static mockit.internal.reflection.ParameterReflection.*;
import static mockit.internal.util.Utilities.*;
public final class InjectionPoint
{
public enum KindOfInjectionPoint { NotAnnotated, Required, Optional, WithValue }
@Nullable public static final Class extends Annotation> INJECT_CLASS;
@Nullable private static final Class extends Annotation> INSTANCE_CLASS;
@Nullable private static final Class extends Annotation> EJB_CLASS;
@Nullable public static final Class extends Annotation> PERSISTENCE_UNIT_CLASS;
@Nullable public static final Class> SERVLET_CLASS;
@Nullable public static final Class> CONVERSATION_CLASS;
static
{
INJECT_CLASS = searchTypeInClasspath("javax.inject.Inject");
INSTANCE_CLASS = searchTypeInClasspath("javax.enterprise.inject.Instance");
EJB_CLASS = searchTypeInClasspath("javax.ejb.EJB");
SERVLET_CLASS = searchTypeInClasspath("javax.servlet.Servlet");
CONVERSATION_CLASS = searchTypeInClasspath("javax.enterprise.context.Conversation");
Class extends Annotation> entity = searchTypeInClasspath("javax.persistence.Entity");
if (entity == null) {
PERSISTENCE_UNIT_CLASS = null;
}
else {
PERSISTENCE_UNIT_CLASS = searchTypeInClasspath("javax.persistence.PersistenceUnit");
}
}
@Nonnull public final Type type;
@Nullable public final String name;
@Nullable private final String normalizedName;
public final boolean qualified;
public InjectionPoint(@Nonnull Type type) { this(type, null, false); }
public InjectionPoint(@Nonnull Type type, @Nullable String name) { this(type, name, false); }
public InjectionPoint(@Nonnull Type type, @Nullable String name, boolean qualified)
{
this.type = type;
this.name = name;
normalizedName = name == null ? null : convertToLegalJavaIdentifierIfNeeded(name);
this.qualified = qualified;
}
public InjectionPoint(@Nonnull Type type, @Nonnull String name, @Nullable String qualifiedName)
{
this.type = type;
this.name = qualifiedName == null ? name : qualifiedName;
normalizedName = this.name;
qualified = qualifiedName != null;
}
@Nonnull
public static String convertToLegalJavaIdentifierIfNeeded(@Nonnull String name)
{
if (name.indexOf('-') < 0 && name.indexOf('.') < 0) {
return name;
}
StringBuilder identifier = new StringBuilder(name);
for (int i = name.length() - 1; i >= 0; i--) {
char c = identifier.charAt(i);
if (c == '-' || c == '.') {
identifier.deleteCharAt(i);
c = identifier.charAt(i);
identifier.setCharAt(i, toUpperCase(c));
}
}
return identifier.toString();
}
@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 = normalizedName;
return type.equals(otherIP.type) && (thisName == null || thisName.equals(otherIP.normalizedName));
}
@Override
public int hashCode() { return 31 * type.hashCode() + (normalizedName == null ? 0 : normalizedName.hashCode()); }
boolean hasSameName(InjectionPoint otherIP)
{
String thisName = normalizedName;
return thisName != null && thisName.equals(otherIP.normalizedName);
}
public static boolean isServlet(@Nonnull Class> aClass)
{
return SERVLET_CLASS != null && Servlet.class.isAssignableFrom(aClass);
}
@Nonnull
public static Object wrapInProviderIfNeeded(@Nonnull Type type, @Nonnull final Object value)
{
if (INJECT_CLASS != null && type instanceof ParameterizedType && !(value instanceof Provider)) {
Type parameterizedType = ((ParameterizedType) type).getRawType();
if (parameterizedType == Provider.class) {
return new Provider
© 2015 - 2024 Weber Informatics LLC | Privacy Policy