|
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 ObjectInterceptionInvocationHandler 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 |
69
| public ObjectInterceptionInvocationHandler( 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 |
69
| this.invocationHandler = invocationHandler;
|
|
44 |
69
| this.id = id;
|
|
45 |
69
| this.clazz = clazz;
|
|
46 |
69
| this.proxyFactory = proxyFactory;
|
|
47 |
69
| this.expectationsState = expectationsState;
|
|
48 |
69
| this.invocationListener = invocationListener;
|
|
49 |
69
| this.objectReferenceFromStacktrace = objectReferenceFromStacktrace;
|
|
50 |
| } |
|
51 |
| |
|
52 |
765
| public Object intercept(Object proxy, Method method, Object[] parameters, MethodProxy methodProxy) throws Throwable {
|
|
53 |
| |
|
54 |
765
| String methodName = method.getName();
|
|
55 |
765
| Class[] parameterTypes = method.getParameterTypes();
|
|
56 |
765
| MethodHandle methodHandle = new ForwardingMethodHandle(proxy, methodProxy, id, methodName, parameterTypes , method.getReturnType());
|
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
765
| if (methodName.equals("finalize") && parameters.length == 0) {
|
|
73 |
1
| return methodHandle.invoke(parameters);
|
|
74 |
| } |
|
75 |
| |
|
76 |
764
| MockMethodSignatureIdentity setEntry = new MockMethodSignatureIdentity( id, methodName, parameterTypes );
|
|
77 |
| |
|
78 |
764
| sendInvocationListenerEvent(proxy, parameters, method, invocationListener, objectReferenceFromStacktrace, expectationsState);
|
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
764
| if( expectationsState.isInVerifyState() && !invocations.contains( setEntry ) ) {
|
|
85 |
616
| if ( methodProxy != null) {
|
|
86 |
601
| return methodHandle.invoke( parameters );
|
|
87 |
| } |
|
88 |
| else { |
|
89 |
15
| return null;
|
|
90 |
| } |
|
91 |
| } |
|
92 |
| else { |
|
93 |
148
| invocations.add( setEntry );
|
|
94 |
148
| return invocationHandler.invocation( id, proxy.getClass(), methodName, parameters, methodHandle );
|
|
95 |
| } |
|
96 |
| } |
|
97 |
| } |
|
98 |
| |