1 5 package org.easymock.internal; 6 7 import java.lang.reflect.Method ; 8 9 import org.easymock.ArgumentsMatcher; 10 import org.easymock.MockControl; 11 12 public class MethodCall { 13 private final Method method; 14 15 private final Object [] arguments; 16 17 public MethodCall(Method method, Object [] args) { 18 this.method = method; 19 this.arguments = expandVarArgs(method.isVarArgs(), args); 20 } 21 22 private static Object [] expandVarArgs(final boolean isVarArgs, 23 final Object [] args) { 24 if (!isVarArgs || isVarArgs && args[args.length - 1] != null && !args[args.length - 1].getClass().isArray()) { 25 return args; 26 } 27 Object [] varArgs = ArrayMatcher 28 .createObjectArray(args[args.length - 1]); 29 final int nonVarArgsCount = args.length - 1; 30 final int varArgsCount = varArgs.length; 31 Object [] newArgs = new Object [nonVarArgsCount + varArgsCount]; 32 System.arraycopy(args, 0, newArgs, 0, nonVarArgsCount); 33 System.arraycopy(varArgs, 0, newArgs, nonVarArgsCount, varArgsCount); 34 return newArgs; 35 } 36 37 public Method getMethod() { 38 return method; 39 } 40 41 public Object [] getArguments() { 42 return arguments; 43 } 44 45 public boolean equals(Object o) { 46 if (o == null || !o.getClass().equals(this.getClass())) 47 return false; 48 49 MethodCall other = (MethodCall) o; 50 return this.method.equals(other.method) 51 && this.equalArguments(other.arguments); 52 } 53 54 public int hashCode() { 55 throw new UnsupportedOperationException ("hashCode() is not implemented"); 56 } 57 58 private boolean equalArguments(Object [] arguments) { 59 if (this.arguments == null && arguments == null) { 60 return true; 61 } 62 63 if (this.arguments.length != arguments.length) { 64 return false; 65 } 66 for (int i = 0; i < this.arguments.length; i++) { 67 Object myArgument = this.arguments[i]; 68 Object otherArgument = arguments[i]; 69 if (method.getParameterTypes()[i].isPrimitive()) { 70 if (!myArgument.equals(otherArgument)) { 71 return false; 72 } 73 } else { 74 if (myArgument != otherArgument) { 75 return false; 76 } 77 } 78 } 79 return true; 80 } 81 82 public boolean matches(MethodCall actual, ArgumentsMatcher matcher) { 83 return this.method.equals(actual.method) 84 && matcher.matches(this.arguments, actual.arguments); 85 } 86 87 public String toString(ArgumentsMatcher matcher) { 88 return method.getName() + "(" + matcher.toString(arguments) + ")"; 89 } 90 } 91 | Popular Tags |