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

mockit.internal.mockups.MockedImplementationClass 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-2014 Rogério Liesenfeld
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
 */
package mockit.internal.mockups;

import java.lang.reflect.Type;
import static java.lang.reflect.Modifier.*;

import org.jetbrains.annotations.*;

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

public final class MockedImplementationClass
{
   @NotNull private final MockUp mockInstance;

   public MockedImplementationClass(@NotNull MockUp mockInstance) { this.mockInstance = mockInstance; }

   @NotNull public T generate(@NotNull Class interfaceToBeMocked, @Nullable Type typeToMock)
   {
      if (!isPublic(interfaceToBeMocked.getModifiers())) {
         T proxy = EmptyProxy.Impl.newEmptyProxy(interfaceToBeMocked.getClassLoader(), interfaceToBeMocked);
         new MockClassSetup(proxy.getClass(), typeToMock, mockInstance, null).redefineMethods();
         return proxy;
      }

      ImplementationClass implementationClass = new ImplementationClass(interfaceToBeMocked) {
         @Override
         @NotNull
         protected ClassVisitor createMethodBodyGenerator(@NotNull ClassReader typeReader, @NotNull String className)
         {
            return new InterfaceImplementationGenerator(typeReader, className);
         }
      };

      Class generatedClass = implementationClass.generateNewMockImplementationClassForInterface();
      byte[] generatedBytecode = implementationClass.getGeneratedBytecode();

      T proxy = ConstructorReflection.newInstanceUsingDefaultConstructor(generatedClass);

      new MockClassSetup(generatedClass, typeToMock, mockInstance, generatedBytecode).redefineMethods();

      return proxy;
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy