-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
145 lines (115 loc) · 2.98 KB
/
utils.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
package aop
import (
"reflect"
"runtime"
"strings"
)
func getMethodMetadata(method reflect.Method) (metadata MethodMetadata, err error) {
if method.Func.IsNil() {
err = ErrMethodIsNil.New()
return
}
iMethod := method.Func.Interface()
if reflect.TypeOf(iMethod).Kind() != reflect.Func {
err = ErrBadMethodType.New()
return
}
v := reflect.ValueOf(iMethod)
pc := runtime.FuncForPC(v.Pointer())
name := strings.TrimRight(pc.Name(), "-fm")
name = strings.Replace(name, "*", "", 1)
metadata.Method = method
metadata.Method.Name = name
metadata.File, metadata.Line = pc.FileLine(v.Pointer())
return
}
func getFuncMetadata(fn interface{}) (metadata MethodMetadata, err error) {
if fn == nil {
err = ErrMethodIsNil.New()
return
}
if reflect.TypeOf(fn).Kind() != reflect.Func {
err = ErrBadMethodType.New()
return
}
v := reflect.ValueOf(fn)
pc := runtime.FuncForPC(v.Pointer())
name := strings.TrimRight(pc.Name(), "-fm")
name = strings.Replace(name, "*", "", 1)
m := reflect.Method{
Name: name,
PkgPath: "",
Type: reflect.TypeOf(fn),
Func: v,
Index: -1,
}
metadata.File, metadata.Line = pc.FileLine(v.Pointer())
metadata.Method = m
return
}
func invokeAdvices(joinPoint JoinPointer, advices []*Advice, methodName string, result Result) (err error) {
for _, advice := range advices {
var retFunc func()
if IsTracing() {
var metadata MethodMetadata
if metadata, err = advice.beanRef.methodMetadata(advice.Method); err != nil {
return
}
appendTraceItem(joinPoint.CallID(), metadata.File, metadata.Line, advice.Method, methodName, advice.beanRef.ID())
}
var adviceMethodMeta MethodMetadata
if adviceMethodMeta, err = advice.beanRef.methodMetadata(advice.Method); err != nil {
return
}
var invokeArgs Args
if adviceMethodMeta.IsEqual(joinPointFuncType) ||
adviceMethodMeta.IsEqual(proceedingJoinPointType) {
invokeArgs = Args{joinPoint}
} else if adviceMethodMeta.IsEqual(joinPointWithResultFuncType) {
invokeArgs = Args{joinPoint, result}
} else {
invokeArgs = joinPoint.Args()
}
if _, err = advice.beanRef.Invoke(advice.Method, invokeArgs, func(values ...interface{}) {
if values != nil {
for _, v := range values {
if errV, ok := v.(error); ok {
err = errV
}
}
}
if err != nil {
return
}
}); err != nil {
return
}
if retFunc != nil {
retFunc()
}
}
return
}
func getFuncArgsType(bean *Bean, methodName string) (types []reflect.Type) {
method := reflect.ValueOf(bean.instance).MethodByName(methodName)
for i := 0; i < method.Type().NumIn(); i++ {
types = append(types, method.Type().In(i))
}
return
}
func getFuncNameByStructFuncName(name string) string {
if name == "" {
return ""
}
index := strings.LastIndex(name, ".")
if index > 0 {
return name[index+1:]
}
return ""
}
func getFullStructName(v interface{}) (name string, err error) {
beanT := reflect.TypeOf(v)
name = beanT.String()
name = strings.Replace(name, "*", "", 1)
return
}