forked from gogap/aop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aspect.go
91 lines (71 loc) · 1.8 KB
/
aspect.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
package aop
import (
"github.com/gogap/errors"
)
type Aspect struct {
id string
beanRefID string
beanFactory BeanFactory
pointcutIDs []string
pointcuts map[string]*Pointcut
advices []*Advice
}
func NewAspect(id, beanRefID string) *Aspect {
if id == "" {
panic(ErrAspectIDShouldNotBeEmpty.New())
}
if beanRefID == "" {
panic(ErrBeanIDShouldNotBeEmpty.New())
}
return &Aspect{
id: id,
beanRefID: beanRefID,
pointcuts: make(map[string]*Pointcut),
}
}
func (p *Aspect) ID() string {
return p.id
}
func (p *Aspect) BeanRefID() string {
return p.beanRefID
}
func (p *Aspect) AddPointcut(pointcut *Pointcut) *Aspect {
if _, exist := p.pointcuts[pointcut.ID()]; !exist {
p.pointcuts[pointcut.ID()] = pointcut
p.pointcutIDs = append(p.pointcutIDs, pointcut.ID())
}
return p
}
func (p *Aspect) AddAdvice(advice *Advice) *Aspect {
var beanRef *Bean
var err error
if beanRef, err = p.beanFactory.GetBean(p.beanRefID); err != nil {
panic(err)
}
if advice.PointcutRefID == "" {
err = ErrEmptyPointcutRefID.New()
panic(err)
}
if pointcut, exist := p.pointcuts[advice.PointcutRefID]; exist {
advice.pointcut = pointcut
} else {
panic(ErrPointcutNotExist.New(errors.Params{"id": advice.PointcutRefID}))
}
advice.beanRef = beanRef
p.advices = append(p.advices, advice)
return p
}
func (p *Aspect) SetBeanFactory(factory BeanFactory) {
p.beanFactory = factory
return
}
func (p *Aspect) GetMatchedAdvices(bean *Bean, methodName string, args Args) (advices map[AdviceOrdering][]*Advice, err error) {
var advs map[AdviceOrdering][]*Advice = make(map[AdviceOrdering][]*Advice)
for _, advice := range p.advices {
if advice.pointcut.IsMatch(bean, methodName, args) {
advs[advice.Ordering] = append(advs[advice.Ordering], advice)
}
}
advices = advs
return
}