1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| package org.easymock.internal; |
6 |
| |
7 |
| import java.lang.reflect.InvocationHandler; |
8 |
| import java.lang.reflect.Method; |
9 |
| import java.lang.reflect.Proxy; |
10 |
| |
11 |
| public class ObjectMethodsFilter implements InvocationHandler { |
12 |
| private final Method equalsMethod; |
13 |
| |
14 |
| private final Method hashCodeMethod; |
15 |
| |
16 |
| private final Method toStringMethod; |
17 |
| |
18 |
| private final MockInvocationHandler delegate; |
19 |
| |
20 |
367
| public ObjectMethodsFilter(MockInvocationHandler delegate) {
|
21 |
367
| try {
|
22 |
367
| equalsMethod = Object.class.getMethod("equals",
|
23 |
| new Class[] { Object.class }); |
24 |
367
| hashCodeMethod = Object.class.getMethod("hashCode", (Class[]) null);
|
25 |
367
| toStringMethod = Object.class.getMethod("toString", (Class[]) null);
|
26 |
| } catch (NoSuchMethodException e) { |
27 |
| |
28 |
| throw new RuntimeException("An Object method could not be found!"); |
29 |
| |
30 |
| } |
31 |
367
| this.delegate = delegate;
|
32 |
| } |
33 |
| |
34 |
3134
| public final Object invoke(Object proxy, Method method, Object[] args)
|
35 |
| throws Throwable { |
36 |
3134
| if (equalsMethod.equals(method)) {
|
37 |
2144
| return proxy == args[0] ? Boolean.TRUE : Boolean.FALSE;
|
38 |
| } |
39 |
990
| if (hashCodeMethod.equals(method)) {
|
40 |
2
| return new Integer(System.identityHashCode(proxy));
|
41 |
| } |
42 |
988
| if (toStringMethod.equals(method)) {
|
43 |
4
| return mockToString(proxy);
|
44 |
| } |
45 |
984
| return delegate.invoke(proxy, method, args);
|
46 |
| } |
47 |
| |
48 |
4
| private String mockToString(Object proxy) {
|
49 |
4
| return "EasyMock for " + mockType(proxy);
|
50 |
| } |
51 |
| |
52 |
4
| private String mockType(Object proxy) {
|
53 |
4
| if (Proxy.isProxyClass(proxy.getClass()))
|
54 |
3
| return proxy.getClass()
|
55 |
| .getInterfaces()[0].toString(); |
56 |
| else |
57 |
1
| return proxy.getClass()
|
58 |
| .getSuperclass().toString(); |
59 |
| } |
60 |
| |
61 |
104
| public MockInvocationHandler getDelegate() {
|
62 |
104
| return delegate;
|
63 |
| } |
64 |
| } |