Home | History | Annotate | Download | only in invocation
      1 /*
      2  * Copyright (c) 2007 Mockito contributors
      3  * This program is made available under the terms of the MIT License.
      4  */
      5 package org.mockito.internal.invocation;
      6 
      7 import org.mockito.ArgumentMatcher;
      8 import org.mockito.internal.matchers.ArrayEquals;
      9 import org.mockito.internal.matchers.Equals;
     10 
     11 import java.util.ArrayList;
     12 import java.util.Arrays;
     13 import java.util.List;
     14 
     15 /**
     16  * by Szczepan Faber, created at: 3/31/12
     17  */
     18 public class ArgumentsProcessor {
     19     // drops hidden synthetic parameters (last continuation parameter from Kotlin suspending functions)
     20     // and expands varargs
     21     public static Object[] expandArgs(MockitoMethod method, Object[] args) {
     22         int nParams = method.getParameterTypes().length;
     23         if (args != null && args.length > nParams)
     24             args = Arrays.copyOf(args, nParams); // drop extra args (currently -- Kotlin continuation synthetic arg)
     25         return expandVarArgs(method.isVarArgs(), args);
     26     }
     27 
     28     // expands array varArgs that are given by runtime (1, [a, b]) into true
     29     // varArgs (1, a, b);
     30     private static Object[] expandVarArgs(final boolean isVarArgs, final Object[] args) {
     31         if (!isVarArgs || isNullOrEmpty(args) || args[args.length - 1] != null && !args[args.length - 1].getClass().isArray()) {
     32             return args == null ? new Object[0] : args;
     33         }
     34 
     35         final int nonVarArgsCount = args.length - 1;
     36         Object[] varArgs;
     37         if (args[nonVarArgsCount] == null) {
     38             // in case someone deliberately passed null varArg array
     39             varArgs = new Object[] { null };
     40         } else {
     41             varArgs = ArrayEquals.createObjectArray(args[nonVarArgsCount]);
     42         }
     43         final int varArgsCount = varArgs.length;
     44         Object[] newArgs = new Object[nonVarArgsCount + varArgsCount];
     45         System.arraycopy(args, 0, newArgs, 0, nonVarArgsCount);
     46         System.arraycopy(varArgs, 0, newArgs, nonVarArgsCount, varArgsCount);
     47         return newArgs;
     48     }
     49 
     50     private static <T> boolean isNullOrEmpty(T[] array) {
     51         return array == null || array.length == 0;
     52     }
     53 
     54     public static List<ArgumentMatcher> argumentsToMatchers(Object[] arguments) {
     55         List<ArgumentMatcher> matchers = new ArrayList<ArgumentMatcher>(arguments.length);
     56         for (Object arg : arguments) {
     57             if (arg != null && arg.getClass().isArray()) {
     58                 matchers.add(new ArrayEquals(arg));
     59             } else {
     60                 matchers.add(new Equals(arg));
     61             }
     62         }
     63         return matchers;
     64     }
     65 
     66 
     67 }
     68