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

mockit.internal.expectations.invocation.DelegatedResult 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.

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

import java.lang.reflect.*;

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

final class DelegatedResult extends DynamicInvocationResult
{
   DelegatedResult(Delegate delegate)
   {
      super(delegate, findDelegateMethodIfSingle(delegate));
   }

   private static Method findDelegateMethodIfSingle(Delegate delegate)
   {
      Method[] declaredMethods = delegate.getClass().getDeclaredMethods();
      return declaredMethods.length == 1 ? declaredMethods[0] : null;
   }

   @Override
   Object produceResult(
      Object invokedObject, ExpectedInvocation invocation, InvocationConstraints constraints, Object[] args)
      throws Throwable
   {
      if (methodToInvoke == null) {
         String methodName = adaptNameAndArgumentsForDelegate(invocation, args);
         methodToInvoke = findCompatibleMethod(targetObject.getClass(), methodName, args);
         determineWhetherMethodToInvokeHasInvocationParameter();
      }

      return invokeMethodOnTargetObject(invokedObject, invocation, constraints, args);
   }

   private String adaptNameAndArgumentsForDelegate(ExpectedInvocation invocation, Object[] args)
   {
      String methodNameAndDesc = invocation.getMethodNameAndDescription();
      int leftParen = methodNameAndDesc.indexOf('(');

      replaceNullArgumentsWithClassObjectsIfAny(methodNameAndDesc, leftParen, args);

      String methodName = methodNameAndDesc.substring(0, leftParen);

      if ("".equals(methodName)) {
         methodName = "$init";
      }

      return methodName;
   }

   private void replaceNullArgumentsWithClassObjectsIfAny(String methodNameAndDesc, int leftParen, Object[] args)
   {
      Type[] argTypes = null;

      for (int i = 0; i < args.length; i++) {
         if (args[i] == null) {
            if (argTypes == null) {
               String methodDesc = methodNameAndDesc.substring(leftParen);
               argTypes = Type.getArgumentTypes(methodDesc);
            }

            args[i] = TypeDescriptor.getClassForType(argTypes[i]);
         }
      }
   }

   private Method findCompatibleMethod(Class theClass, String methodName, Object[] args)
   {
      Class[] argTypes = ParameterReflection.getArgumentTypesFromArgumentValues(args);
      return MethodReflection.findCompatibleMethod(theClass, methodName, argTypes);
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy