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.
The newest version!
/*
* Copyright (c) 2006 JMockit developers
* This file is subject to the terms of the MIT license (see LICENSE.txt).
*/
package mockit.internal.injection;
import static java.lang.Character.toUpperCase;
import static mockit.internal.reflection.AnnotationReflection.readAnnotationAttribute;
import static mockit.internal.reflection.AnnotationReflection.readAnnotationAttributeIfAvailable;
import static mockit.internal.reflection.MethodReflection.invokePublicIfAvailable;
import static mockit.internal.reflection.ParameterReflection.NO_PARAMETERS;
import static mockit.internal.util.ClassLoad.searchTypeInClasspath;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.lang.annotation.Annotation;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.enterprise.inject.Instance;
import javax.enterprise.util.TypeLiteral;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;
import javax.servlet.Servlet;
public final class InjectionPoint {
public enum KindOfInjectionPoint {
NotAnnotated, Required, Optional
}
@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);
char d = identifier.charAt(i);
identifier.setCharAt(i, toUpperCase(d));
}
}
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);
}
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 (Provider
© 2015 - 2025 Weber Informatics LLC | Privacy Policy