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

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

Go to download

JMockit is a Java toolkit for automated developer testing. It contains mocking/faking APIs and a code coverage tool, 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.

There is a newer version: 1.49
Show newest version
/*
 * Copyright (c) 2006-2015 Rogério Liesenfeld
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
 */
package mockit.internal.expectations.mocking;

import javax.annotation.*;

import mockit.internal.util.*;

public abstract class InstanceFactory
{
   @Nonnull final Class concreteClass;
   @Nullable Object lastInstance;

   InstanceFactory(@Nonnull Class concreteClass) { this.concreteClass = concreteClass; }

   @Nonnull public abstract Object create();

   @Nullable public final Object getLastInstance() { return lastInstance; }
   public abstract void clearLastInstance();

   static final class InterfaceInstanceFactory extends InstanceFactory
   {
      @Nullable private Object emptyProxy;

      InterfaceInstanceFactory(@Nonnull Object emptyProxy)
      {
         super(emptyProxy.getClass());
         this.emptyProxy = emptyProxy;
      }

      @Nonnull @Override
      public Object create()
      {
         if (emptyProxy == null) {
            emptyProxy = ConstructorReflection.newUninitializedInstance(concreteClass);
         }

         lastInstance = emptyProxy;
         return emptyProxy;
      }

      @Override
      public void clearLastInstance()
      {
         emptyProxy = null;
         lastInstance = null;
      }
   }

   static final class ClassInstanceFactory extends InstanceFactory
   {
      ClassInstanceFactory(@Nonnull Class concreteClass) { super(concreteClass); }

      @Override @Nonnull
      public Object create()
      {
         lastInstance = ConstructorReflection.newUninitializedInstance(concreteClass);
         return lastInstance;
      }

      @Override
      public void clearLastInstance() { lastInstance = null; }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy