![JAR search and dependency download from the Maven repository](/logo.png)
mockit.internal.mockups.InterfaceImplementationGenerator 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 developer (unit/integration) testing.
It contains mocking APIs and other tools, 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-2013 Rogério Liesenfeld
* This file is subject to the terms of the MIT license (see LICENSE.txt).
*/
package mockit.internal.mockups;
import java.util.*;
import org.jetbrains.annotations.*;
import mockit.external.asm4.*;
import mockit.internal.*;
import static mockit.external.asm4.Opcodes.*;
public final class InterfaceImplementationGenerator extends BaseClassModifier
{
private final List implementedMethods;
private final String implementationClassName;
private String[] initialSuperInterfaces;
public InterfaceImplementationGenerator(@NotNull ClassReader classReader, @NotNull String implementationClassName)
{
super(classReader);
implementedMethods = new ArrayList();
this.implementationClassName = implementationClassName.replace('.', '/');
}
@Override
public void visit(
int version, int access, @NotNull String name, @Nullable String signature, @Nullable String superName,
@Nullable String[] interfaces)
{
initialSuperInterfaces = interfaces;
String[] implementedInterfaces = {name};
super.visit(
version, ACC_PUBLIC + ACC_FINAL, implementationClassName, signature, superName, implementedInterfaces);
generateDefaultConstructor();
}
private void generateDefaultConstructor()
{
mw = cw.visitMethod(ACC_PUBLIC, "", "()V", null, null);
mw.visitVarInsn(ALOAD, 0);
mw.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "", "()V");
generateEmptyImplementation();
}
@Override
public void visitInnerClass(String name, String outerName, String innerName, int access) {}
@Override
public void visitOuterClass(String owner, String name, String desc) {}
@Override
public void visitAttribute(Attribute attr) {}
@Override
public void visitSource(String file, String debug) {}
@Override
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { return null; }
@Override
public MethodVisitor visitMethod(
int access, @NotNull String name, @NotNull String desc, @Nullable String signature, @Nullable String[] exceptions)
{
if (name.charAt(0) != '<') { // ignores an eventual "" class initialization "method"
generateMethodImplementation(name, desc, signature, exceptions);
}
return null;
}
private void generateMethodImplementation(
@NotNull String name, @NotNull String desc, @Nullable String signature, @Nullable String[] exceptions)
{
String methodNameAndDesc = name + desc;
if (!implementedMethods.contains(methodNameAndDesc)) {
mw = cw.visitMethod(ACC_PUBLIC, name, desc, signature, exceptions);
generateEmptyImplementation(desc);
implementedMethods.add(methodNameAndDesc);
}
}
@Override
public void visitEnd()
{
for (String superInterface : initialSuperInterfaces) {
new MethodGeneratorForImplementedSuperInterface(superInterface);
}
}
private final class MethodGeneratorForImplementedSuperInterface extends ClassVisitor
{
String[] superInterfaces;
MethodGeneratorForImplementedSuperInterface(String interfaceName)
{
ClassFile.visitClass(interfaceName, this);
}
@Override
public void visit(
int version, int access, @NotNull String name, String signature, String superName, String[] interfaces)
{
superInterfaces = interfaces;
}
@Override
public FieldVisitor visitField(
int access, String name, String desc, String signature, Object value) { return null; }
@Override
public MethodVisitor visitMethod(
int access, @NotNull String name, @NotNull String desc,
@Nullable String signature, @Nullable String[] exceptions)
{
generateMethodImplementation(name, desc, signature, exceptions);
return null;
}
@Override
public void visitEnd()
{
for (String superInterface : superInterfaces) {
new MethodGeneratorForImplementedSuperInterface(superInterface);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy