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