KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > easymock > internal > MethodCall


1 /*
2  * Copyright (c) 2001-2005 OFFIS. This program is made available under the terms of
3  * the MIT License.
4  */

5 package org.easymock.internal;
6
7 import java.lang.reflect.Method JavaDoc;
8
9 import org.easymock.ArgumentsMatcher;
10 import org.easymock.MockControl;
11
12 public class MethodCall {
13     private final Method JavaDoc method;
14
15     private final Object JavaDoc[] arguments;
16
17     public MethodCall(Method JavaDoc method, Object JavaDoc[] args) {
18         this.method = method;
19         this.arguments = expandVarArgs(method.isVarArgs(), args);
20     }
21
22     private static Object JavaDoc[] expandVarArgs(final boolean isVarArgs,
23             final Object JavaDoc[] args) {
24         if (!isVarArgs || isVarArgs && args[args.length - 1] != null && !args[args.length - 1].getClass().isArray()) {
25             return args;
26         }
27         Object JavaDoc[] varArgs = ArrayMatcher
28                 .createObjectArray(args[args.length - 1]);
29         final int nonVarArgsCount = args.length - 1;
30         final int varArgsCount = varArgs.length;
31         Object JavaDoc[] newArgs = new Object JavaDoc[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 JavaDoc getMethod() {
38         return method;
39     }
40
41     public Object JavaDoc[] getArguments() {
42         return arguments;
43     }
44
45     public boolean equals(Object JavaDoc 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 JavaDoc("hashCode() is not implemented");
56     }
57
58     private boolean equalArguments(Object JavaDoc[] 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 JavaDoc myArgument = this.arguments[i];
68             Object JavaDoc 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 JavaDoc toString(ArgumentsMatcher matcher) {
88         return method.getName() + "(" + matcher.toString(arguments) + ")";
89     }
90 }
91
Popular Tags