forked from code4craft/tiny-spring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectiveMethodInvocation.java
More file actions
63 lines (50 loc) · 1.37 KB
/
Copy pathReflectiveMethodInvocation.java
File metadata and controls
63 lines (50 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.ysj.tinySpring.aop;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInvocation;
/**
* 封装被代理对象的方法
*
*/
public class ReflectiveMethodInvocation implements MethodInvocation {
// 原对象
protected Object target;
protected Method method;
protected Object[] arguments;
public ReflectiveMethodInvocation(Object target, Method method, Object[] arguments) {
this.target = target;
this.method = method;
this.arguments = arguments;
}
@Override
public Object[] getArguments() {
return arguments;
}
@Override
public AccessibleObject getStaticPart() {
return method;
}
@Override
public Object getThis() {
return target;
}
/**
* 调用被拦截对象的方法
*/
@Override
public Object proceed() throws Throwable {
// tiny-spring这里是调用原始对象的方法
// 不支持拦截器链
/*
为了支持拦截器链,可以做出以下修改:
将 proceed() 方法修改为调用代理对象的方法 method.invoke(proxy,args)。
在代理对象的 InvocationHandler 的 invoke 函数中,查看拦截器列表,如果有拦截器,则调用第一个拦截器并
返回,否则调用原始对象的方法。
*/
return method.invoke(target, arguments);
}
@Override
public Method getMethod() {
return method;
}
}