-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathpointcut.go
155 lines (125 loc) · 2.76 KB
/
pointcut.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
package aop
import (
"regexp"
"runtime"
"strings"
"sync"
)
type Pointcut struct {
id string
executionExprs []*regexp.Regexp
notExecutionExprs []*regexp.Regexp
byBeans []*regexp.Regexp
notByBeans []*regexp.Regexp
withIn []*regexp.Regexp
notWithIn []*regexp.Regexp
exprLocker sync.Mutex
}
func (p *Pointcut) Execution(expr string) *Pointcut {
p.exprLocker.Lock()
defer p.exprLocker.Unlock()
p.executionExprs = append(p.executionExprs, regexp.MustCompile(expr))
return p
}
func (p *Pointcut) NotExecution(expr string) *Pointcut {
p.exprLocker.Lock()
defer p.exprLocker.Unlock()
p.notExecutionExprs = append(p.notExecutionExprs, regexp.MustCompile(expr))
return p
}
func (p *Pointcut) Bean(expr string) *Pointcut {
p.exprLocker.Lock()
defer p.exprLocker.Unlock()
p.byBeans = append(p.byBeans, regexp.MustCompile(expr))
return p
}
func (p *Pointcut) NotBean(expr string) *Pointcut {
p.exprLocker.Lock()
defer p.exprLocker.Unlock()
p.notByBeans = append(p.notByBeans, regexp.MustCompile(expr))
return p
}
func (p *Pointcut) Within(expr string) *Pointcut {
p.exprLocker.Lock()
defer p.exprLocker.Unlock()
p.withIn = append(p.withIn, regexp.MustCompile(expr))
return p
}
func (p *Pointcut) NotWithin(expr string) *Pointcut {
p.exprLocker.Lock()
defer p.exprLocker.Unlock()
p.notWithIn = append(p.notWithIn, regexp.MustCompile(expr))
return p
}
func NewPointcut(id string) *Pointcut {
return &Pointcut{
id: id,
}
}
func (p *Pointcut) ID() string {
return p.id
}
func (p *Pointcut) IsMatch(bean *Bean, methodName string, args Args) bool {
// not execution
for _, notExecRegex := range p.notExecutionExprs {
if notExecRegex.MatchString(methodName) {
return false
}
}
// execution
execGot := false
for _, execRegex := range p.executionExprs {
if execRegex.MatchString(methodName) {
execGot = true
break
}
}
if !execGot {
return false
}
// not bean
for _, notBeanRegex := range p.notByBeans {
if notBeanRegex.MatchString(bean.id) {
return false
}
}
// bean
if len(p.byBeans) > 0 {
beanGot := false
for _, beanRegex := range p.byBeans {
if beanRegex.MatchString(bean.id) {
beanGot = true
break
}
}
if !beanGot {
return false
}
}
stacks := ""
if len(p.notWithIn) > 0 || len(p.withIn) > 0 {
buf := make([]byte, 4096)
runtime.Stack(buf, false)
stacks = strings.Replace(string(buf), "*", "", -1)
}
// not within
for _, notWithInRegex := range p.notWithIn {
if notWithInRegex.MatchString(stacks) {
return false
}
}
// with in
if len(p.withIn) > 0 {
withInGot := false
for _, withInRegex := range p.withIn {
if withInRegex.MatchString(stacks) {
withInGot = true
break
}
}
if !withInGot {
return false
}
}
return true
}