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

mockit.internal.expectations.transformation.ArgumentCapturing Maven / Gradle / Ivy

/*
 * 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.transformation;

import java.util.*;

import org.jetbrains.annotations.*;

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

import mockit.internal.expectations.transformation.InvocationBlockModifier.Capture;

final class ArgumentCapturing
{
   boolean justAfterWithCaptureInvocation;
   @Nullable private List captures;
   private boolean parameterForCapture;
   @Nullable private String capturedTypeDesc;

   void registerCapturingMatcherIfApplicable(@NotNull String methodName, @NotNull String methodDesc)
   {
      justAfterWithCaptureInvocation = "withCapture".equals(methodName);
      parameterForCapture = justAfterWithCaptureInvocation && !methodDesc.contains("List");
   }

   void registerTypeToCaptureIfApplicable(int opcode, @NotNull String type)
   {
      if (opcode == CHECKCAST && parameterForCapture) {
         capturedTypeDesc = type;
      }
   }

   void registerAssignmentToCaptureVariableIfApplicable(@NotNull InvocationBlockModifier modifier, int opcode, int var)
   {
      if (opcode >= ISTORE && opcode <= ASTORE && parameterForCapture) {
         Capture capture = modifier.createCapture(opcode, var, capturedTypeDesc);
         addCapture(capture);
         parameterForCapture = false;
         capturedTypeDesc = null;
      }
   }

   private void addCapture(@NotNull Capture capture)
   {
      if (captures == null) {
         captures = new ArrayList();
      }

      captures.add(capture);
   }

   void updateCaptureIfAny(int originalIndex, int newIndex)
   {
      if (captures != null) {
         for (int i = captures.size() - 1; i >= 0; i--) {
            Capture capture = captures.get(i);

            if (capture.fixParameterIndex(originalIndex, newIndex)) {
               break;
            }
         }
      }
   }

   void generateCallsToSetArgumentTypesToCaptureIfAny()
   {
      if (captures != null) {
         for (Capture capture : captures) {
            capture.generateCallToSetArgumentTypeIfNeeded();
         }
      }
   }

   void generateCallsToCaptureMatchedArgumentsIfPending()
   {
      if (captures != null) {
         for (Capture capture : captures) {
            capture.generateCodeToStoreCapturedValue();
         }

         captures = null;
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy