1 /* Copyright (c) 2000-2003, jMock.org. See bundled LICENSE.txt */
2 package org.jmock.dynamic;
3
4 import java.lang.reflect.Method;
5 import java.lang.reflect.Proxy;
6 import java.util.*;
7
8
9 /***
10 * An object that holds information about a call dispatched to
11 * a Mock object for err... mocking.
12 * <p/>
13 * TODO Simplify this (smgf)
14 */
15 public class Invocation {
16 private String name;
17 private List parameterTypes;
18 private Class returnType;
19 private List parameterValues;
20
21 public Invocation(String name, Class[] parameterTypes,
22 Class returnType, Object[] parameterValues) {
23 this.name = name;
24 this.parameterTypes = Arrays.asList(parameterTypes);
25 this.returnType = returnType;
26 if (parameterValues == null) {
27 this.parameterValues = new ArrayList(0);
28 } else {
29 this.parameterValues = Arrays.asList(parameterValues);
30 }
31 }
32
33 public Invocation(Method method, Object[] parameterValues) {
34 this(method.getName(), method.getParameterTypes(),
35 method.getReturnType(), parameterValues);
36 }
37
38 public String getMethodName() {
39 return name;
40 }
41
42 public List getParameterTypes() {
43 return Collections.unmodifiableList(parameterTypes);
44 }
45
46 public List getParameterValues() {
47 return Collections.unmodifiableList(parameterValues);
48 }
49
50 public Class getReturnType() {
51 return returnType;
52 }
53
54 public String toString() {
55 return DynamicUtil.methodToString(name, parameterValues.toArray());
56 }
57
58 public boolean equals(Object o) {
59 if (o instanceof Invocation) {
60 return this.equals((Invocation) o);
61 } else {
62 return false;
63 }
64 }
65
66 public int hashCode() {
67 return name.hashCode() ^
68 listHashCode(parameterTypes) ^
69 returnType.hashCode() ^
70 listHashCode(parameterValues);
71 }
72
73 private int listHashCode(List array) {
74 int hashCode = 0;
75 for (Iterator i = array.iterator(); i.hasNext();) {
76 hashCode ^= i.next().hashCode();
77 }
78 return hashCode;
79 }
80
81 public boolean equals(Invocation call) {
82 return call != null
83 && name.equals(call.name)
84 && parameterTypes.equals(call.parameterTypes)
85 && returnType.equals(call.returnType)
86 && parameterValues.equals(call.parameterValues);
87 }
88
89 boolean isCheckingEqualityOnProxy() {
90 return name.equals("equals")
91 && parameterValues.size() == 1
92 && parameterValues.get(0) != null
93 && Proxy.isProxyClass(parameterValues.get(0).getClass());
94 }
95
96 boolean isMockNameGetter() {
97 return name.equals("getMockName")
98 && parameterValues.size() == 0;
99 }
100 }
This page was automatically generated by Maven