|
1 |
| package com.agical.rmock.extension.cglib; |
|
2 |
| |
|
3 |
| import java.lang.reflect.Method; |
|
4 |
| import java.util.HashSet; |
|
5 |
| import java.util.Set; |
|
6 |
| |
|
7 |
| import net.sf.cglib.proxy.MethodInterceptor; |
|
8 |
| import net.sf.cglib.proxy.MethodProxy; |
|
9 |
| |
|
10 |
| import com.agical.rmock.core.MethodHandle; |
|
11 |
| import com.agical.rmock.core.ProxyFactory; |
|
12 |
| import com.agical.rmock.core.expectation.ExpectationsState; |
|
13 |
| import com.agical.rmock.core.expectation.InvocationListener; |
|
14 |
| import com.agical.rmock.core.util.ForwardingMethodHandle; |
|
15 |
| import com.agical.rmock.core.util.impl.MockMethodSignatureIdentity; |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| public class ObjectMockInvocationHandler extends BaseInvocationHandler |
|
24 |
| implements MethodInterceptor { |
|
25 |
| |
|
26 |
| private final com.agical.rmock.core.InvocationHandler invocationHandler; |
|
27 |
| private final String id; |
|
28 |
| private final Class clazz; |
|
29 |
| private final ProxyFactory proxyFactory; |
|
30 |
| private Object proxy; |
|
31 |
| private final ExpectationsState expectationsState; |
|
32 |
| private Set invocations = new HashSet(); |
|
33 |
| private final InvocationListener invocationListener; |
|
34 |
| private final ObjectReferenceRetriever objectReferenceFromStacktrace; |
|
35 |
| |
|
36 |
15
| public ObjectMockInvocationHandler( com.agical.rmock.core.InvocationHandler invocationHandler,
|
|
37 |
| String id, |
|
38 |
| Class clazz, |
|
39 |
| ProxyFactory proxyFactory, |
|
40 |
| ExpectationsState expectationsState, |
|
41 |
| InvocationListener invocationListener, |
|
42 |
| ObjectReferenceRetriever objectReferenceFromStacktrace) { |
|
43 |
15
| this.invocationHandler = invocationHandler;
|
|
44 |
15
| this.id = id;
|
|
45 |
15
| this.clazz = clazz;
|
|
46 |
15
| this.proxyFactory = proxyFactory;
|
|
47 |
15
| this.expectationsState = expectationsState;
|
|
48 |
15
| this.invocationListener = invocationListener;
|
|
49 |
15
| this.objectReferenceFromStacktrace = objectReferenceFromStacktrace;
|
|
50 |
| } |
|
51 |
| |
|
52 |
25
| public Object intercept(Object proxy, Method method, Object[] parameters, MethodProxy methodProxy) throws Throwable {
|
|
53 |
25
| String methodName = method.getName();
|
|
54 |
25
| Class[] parameterTypes = method.getParameterTypes();
|
|
55 |
25
| MethodHandle methodHandle = new ForwardingMethodHandle(proxy, methodProxy, id, methodName, parameterTypes , method.getReturnType());
|
|
56 |
| |
|
57 |
| |
|
58 |
25
| if (methodName.equals("finalize") && parameters.length == 0) {
|
|
59 |
4
| return methodHandle.invoke(parameters);
|
|
60 |
| } |
|
61 |
| |
|
62 |
21
| sendInvocationListenerEvent(proxy, parameters, method, invocationListener, objectReferenceFromStacktrace, expectationsState);
|
|
63 |
| |
|
64 |
21
| MockMethodSignatureIdentity setEntry = new MockMethodSignatureIdentity( id, methodName, parameterTypes );
|
|
65 |
21
| return invocationHandler.invocation( id, proxy.getClass(), methodName, parameters, methodHandle );
|
|
66 |
| } |
|
67 |
| } |