-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmethod.go
64 lines (59 loc) · 1.68 KB
/
method.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
package gop
import (
"fmt"
"reflect"
"bou.ke/monkey"
)
const methodLocation = "%s.%s.%s"
// register method point
func RegisterMethodPoint(pointType reflect.Type) {
pkgPth := pointType.PkgPath()
receiverName := pointType.Name()
if pointType.Kind() == reflect.Ptr {
pkgPth = pointType.Elem().PkgPath()
receiverName = pointType.Elem().Name()
}
for i := 0; i < pointType.NumMethod(); i++ {
method := pointType.Method(i)
location := fmt.Sprintf(methodLocation, pkgPth, receiverName, method.Name)
putMatchAspect(location)
if len(getAspect(location)) < 1 {
continue
}
var guard *monkey.PatchGuard
var proxy = func(in []reflect.Value) []reflect.Value {
guard.Unpatch()
defer guard.Restore()
receiver := in[0]
point := newMethodJoinPoint(receiver, in[1:], method)
defer finallyProcessed(point, location)
if !beforeProcessed(point, location) {
return point.Result
}
point.Result = receiver.MethodByName(method.Name).Call(in[1:])
afterProcessed(point, location)
return point.Result
}
// dynamic create proxy function
proxyFn := reflect.MakeFunc(method.Func.Type(), proxy)
// change original function call to proxy call
guard = monkey.PatchInstanceMethod(pointType, method.Name, proxyFn.Interface())
}
}
// new method join point
func newMethodJoinPoint(receiver interface{}, params []reflect.Value, method reflect.Method) *JoinPoint {
point := &JoinPoint{
Receiver: receiver,
Params: params,
Method: method,
Func: method.Func,
}
fn := method.Func
fnType := fn.Type()
nout := fnType.NumOut()
point.Result = make([]reflect.Value, nout)
for i := 0; i < nout; i++ {
point.Result[i] = reflect.Zero(fnType.Out(i))
}
return point
}