mockit.internal.faking.FakedImplementationClass 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.faking;
import static java.lang.reflect.Modifier.isPublic;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import mockit.MockUp;
import mockit.asm.classes.ClassReader;
import mockit.asm.classes.ClassVisitor;
import mockit.internal.classGeneration.ImplementationClass;
import mockit.internal.expectations.mocking.InterfaceImplementationGenerator;
import mockit.internal.util.Utilities;
public final class FakedImplementationClass {
private static final ClassLoader THIS_CL = FakedImplementationClass.class.getClassLoader();
@NonNull
private final MockUp> fakeInstance;
@Nullable
private ImplementationClass implementationClass;
private Class generatedClass;
public FakedImplementationClass(@NonNull MockUp> fakeInstance) {
this.fakeInstance = fakeInstance;
}
@NonNull
public Class createImplementation(@NonNull Class interfaceToBeFaked, @Nullable Type typeToFake) {
createImplementation(interfaceToBeFaked);
byte[] generatedBytecode = implementationClass == null ? null : implementationClass.getGeneratedBytecode();
FakeClassSetup fakeClassSetup = new FakeClassSetup(generatedClass, typeToFake, fakeInstance, generatedBytecode);
fakeClassSetup.redefineMethodsInGeneratedClass();
return generatedClass;
}
@NonNull
Class createImplementation(@NonNull Class interfaceToBeFaked) {
if (isPublic(interfaceToBeFaked.getModifiers())) {
generateImplementationForPublicInterface(interfaceToBeFaked);
} else {
// noinspection unchecked
generatedClass = (Class) Proxy.getProxyClass(interfaceToBeFaked.getClassLoader(), interfaceToBeFaked);
}
return generatedClass;
}
private void generateImplementationForPublicInterface(@NonNull Class interfaceToBeFaked) {
implementationClass = new ImplementationClass(interfaceToBeFaked) {
@NonNull
@Override
protected ClassVisitor createMethodBodyGenerator(@NonNull ClassReader typeReader) {
return new InterfaceImplementationGenerator(typeReader, interfaceToBeFaked, generatedClassName);
}
};
generatedClass = implementationClass.generateClass();
}
@NonNull
public Class createImplementation(@NonNull Type[] interfacesToBeFaked) {
Class>[] interfacesToFake = new Class>[interfacesToBeFaked.length];
for (int i = 0; i < interfacesToFake.length; i++) {
interfacesToFake[i] = Utilities.getClassType(interfacesToBeFaked[i]);
}
// noinspection unchecked
generatedClass = (Class) Proxy.getProxyClass(THIS_CL, interfacesToFake);
new FakeClassSetup(generatedClass, null, fakeInstance, null).redefineMethods();
return generatedClass;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy