forked from gogap/aop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aop.go
200 lines (156 loc) · 4.08 KB
/
aop.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package aop
import (
"github.com/rs/xid"
"reflect"
)
var (
errorType = reflect.TypeOf((*error)(nil)).Elem()
)
type AOP struct {
aspects []*Aspect
beanFactory BeanFactory
}
func NewAOP() *AOP {
return &AOP{}
}
func (p *AOP) SetBeanFactory(factory BeanFactory) {
p.beanFactory = factory
}
func (p *AOP) AddAspect(aspect *Aspect) *AOP {
p.aspects = append(p.aspects, aspect)
return p
}
func (p *AOP) funcWrapper(bean *Bean, methodName string, methodType reflect.Type) func([]reflect.Value) []reflect.Value {
beanValue := reflect.ValueOf(bean.instance)
return func(inputs []reflect.Value) (ret []reflect.Value) {
callID := xid.New().String()
var err error
defer func() {
if err != nil {
panic(err)
}
}()
var args Args
for _, arg := range inputs {
args = append(args, arg.Interface())
}
joinPoint := JoinPoint{
callID: callID,
args: args,
target: bean,
}
errOutIndex := -1
outLen := methodType.NumOut()
if outLen > 0 {
for i := 0; i < outLen; i++ {
if methodType.Out(i) == errorType {
errOutIndex = i
}
}
}
ret = make([]reflect.Value, outLen)
for i := 0; i < outLen; i++ {
ret[i] = reflect.Zero(methodType.Out(i))
}
var advicesGroup []map[AdviceOrdering][]*Advice
for _, aspect := range p.aspects {
var advices map[AdviceOrdering][]*Advice
if advices, err = aspect.GetMatchedAdvices(bean, methodName, args); err != nil {
return
}
advicesGroup = append(advicesGroup, advices)
}
callAdvicesFunc := func(order AdviceOrdering, resultValues ...reflect.Value) (e error) {
for _, advices := range advicesGroup {
if e = invokeAdvices(&joinPoint, advices[order], methodName, resultValues); e != nil {
if errOutIndex >= 0 {
ret[errOutIndex] = reflect.ValueOf(&e).Elem()
}
err = e
return
}
}
return
}
//@Before
if err = callAdvicesFunc(Before); err != nil {
return
}
//@Real func
var retValues []reflect.Value
funcInSturctName := getFuncNameByStructFuncName(methodName)
realFunc := func(args ...interface{}) Result {
values := []reflect.Value{}
for _, arg := range args {
values = append(values, reflect.ValueOf(arg))
}
return beanValue.MethodByName(funcInSturctName).Call(values)
}
//@Around
var aroundAdvice *Advice
for _, advices := range advicesGroup {
if aroundAdvices, exist := advices[Around]; exist && len(aroundAdvices) > 0 {
aroundAdvice = aroundAdvices[0]
break
}
}
if aroundAdvice != nil {
pjp := ProceedingJoinPoint{JoinPointer: &joinPoint, method: realFunc}
if err = invokeAdvices(&pjp, []*Advice{aroundAdvice}, methodName, nil); err != nil {
if errOutIndex >= 0 {
ret[errOutIndex] = reflect.ValueOf(&err).Elem()
}
return
}
} else {
retValues = realFunc(inputs)
}
if IsTracing() {
var metadata MethodMetadata
if metadata, err = bean.methodMetadata(funcInSturctName); err != nil {
return
}
appendTraceItem(callID, metadata.File, metadata.Line, "*"+funcInSturctName, methodName, bean.id)
}
defer func() {
//@AfterPanic
if e := recover(); e != nil {
callAdvicesFunc(AfterPanic)
}
//@After
callAdvicesFunc(After)
}()
if err != nil {
//@AfterError
callAdvicesFunc(AfterError)
} else {
//@AfterReturning
callAdvicesFunc(AfterReturning, retValues...)
}
return retValues
}
}
func (p *AOP) GetProxy(beanID string) (proxy *Proxy, err error) {
var bean *Bean
if bean, err = p.beanFactory.GetBean(beanID); err != nil {
return
}
tmpProxy := NewProxy(beanID)
beanValue := reflect.ValueOf(bean.instance)
beanType := reflect.TypeOf(bean.instance)
for i := 0; i < beanValue.NumMethod(); i++ {
methodV := beanValue.Method(i)
methodT := beanType.Method(i)
mType := methodV.Type()
var metadata MethodMetadata
if metadata, err = getMethodMetadata(methodT); err != nil {
return
}
newFunc := p.funcWrapper(bean, metadata.Method.Name, mType)
funcV := reflect.MakeFunc(mType, newFunc)
metadata.Method.Func = funcV // rewrite to new proxy func
tmpProxy.registryFunc(metadata)
}
proxy = tmpProxy
return
}