All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mockit.internal.expectations.mocking.InterfaceImplementationGenerator Maven / Gradle / Ivy

Go to download

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.

The newest version!
/*
 * Copyright (c) 2006-2013 Rogério Liesenfeld
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
 */
package mockit.internal.expectations.mocking;

import java.lang.reflect.Type;
import java.util.*;

import org.jetbrains.annotations.*;

import static mockit.external.asm4.Opcodes.*;

import mockit.external.asm4.*;
import mockit.internal.*;

final class InterfaceImplementationGenerator extends MockedTypeModifier
{
   @NotNull private final List implementedMethods;
   @NotNull private final String implementationClassName;
   @NotNull private final MockedTypeInfo mockedTypeInfo;
   private String interfaceName;
   private String[] initialSuperInterfaces;

   InterfaceImplementationGenerator(
      @NotNull ClassReader classReader, @NotNull Type mockedType, @NotNull String implementationClassName)
   {
      super(classReader);
      implementedMethods = new ArrayList();
      this.implementationClassName = implementationClassName.replace('.', '/');
      mockedTypeInfo = new MockedTypeInfo(mockedType, "Ljava/lang/Object;");
   }

   @Override
   public void visit(
      int version, int access, @NotNull String name, @Nullable String signature, @Nullable String superName,
      @Nullable String[] interfaces)
   {
      interfaceName = name;
      initialSuperInterfaces = interfaces;

      super.visit(
         version, ACC_PUBLIC + ACC_FINAL, implementationClassName, mockedTypeInfo.implementationSignature,
         superName, new String[] {name});

      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, @NotNull String name, @NotNull 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(access, name, desc, signature, exceptions);
      }

      return null;
   }

   @SuppressWarnings("AssignmentToMethodParameter")
   private void generateMethodImplementation(
      int access, @NotNull String name, @NotNull String desc, @Nullable String signature, @Nullable String[] exceptions)
   {
      String methodNameAndDesc = name + desc;

      if (!implementedMethods.contains(methodNameAndDesc)) {
         if (signature != null) {
            signature = mockedTypeInfo.genericTypeMap.resolveReturnType(signature);
         }

         mw = super.visitMethod(ACC_PUBLIC, name, desc, signature, exceptions);
         generateDirectCallToHandler(interfaceName, access, name, desc, signature, exceptions, 0);
         generateReturnWithObjectAtTopOfTheStack(desc);
         mw.visitMaxs(1, 0);

         implementedMethods.add(methodNameAndDesc);
      }
   }

   @Override
   public void visitEnd()
   {
      for (String superInterface : initialSuperInterfaces) {
         new MethodGeneratorForImplementedSuperInterface(superInterface);
      }
   }

   private final class MethodGeneratorForImplementedSuperInterface extends ClassVisitor
   {
      @Nullable String[] superInterfaces;

      MethodGeneratorForImplementedSuperInterface(@NotNull String interfaceName)
      {
         ClassFile.visitClass(interfaceName, this);
      }

      @Override
      public void visit(
         int version, int access, @NotNull String name, @Nullable String signature, @Nullable String superName,
         @Nullable String[] interfaces)
      {
         superInterfaces = interfaces;
      }

      @Override
      public FieldVisitor visitField(
         int access, @NotNull String name, @NotNull String desc, String signature, Object value) { return null; }

      @Override
      public MethodVisitor visitMethod(
         int access, @NotNull String name, @NotNull String desc, String signature, String[] exceptions)
      {
         generateMethodImplementation(access, name, desc, signature, exceptions);
         return null;
      }

      @Override
      public void visitEnd()
      {
         assert superInterfaces != null;

         for (String superInterface : superInterfaces) {
            new MethodGeneratorForImplementedSuperInterface(superInterface);
         }
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy