/
main_aspect.go
42 lines (35 loc) · 1022 Bytes
/
main_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
package main
import (
"fmt"
"regexp"
asp "github.com/AkihiroSuda/aspectgo/aspect"
)
// SAspect won't be woven, because it's not an "execution" pointcut
type SAspect struct {
}
func (a *SAspect) Pointcut() asp.Pointcut {
s := regexp.QuoteMeta("(*github.com/AkihiroSuda/aspectgo/example/receiver.S).Foo")
return asp.NewCallPointcutFromRegexp(s)
}
func (a *SAspect) Advice(ctx asp.Context) []interface{} {
return advice("SAspect", ctx)
}
// IAspect will be woven
type IAspect struct {
}
func (a *IAspect) Pointcut() asp.Pointcut {
s := regexp.QuoteMeta("(github.com/AkihiroSuda/aspectgo/example/receiver.I).Foo")
return asp.NewCallPointcutFromRegexp(s)
}
func (a *IAspect) Advice(ctx asp.Context) []interface{} {
return advice("IAspect", ctx)
}
func advice(name string, ctx asp.Context) []interface{} {
args, recv := ctx.Args(), ctx.Receiver()
fmt.Printf("%s BEFORE call (args=%+v, recv=%+v)\n",
name, args, recv)
res := ctx.Call(args)
fmt.Printf("%s AFTER call (res=%+v)\n",
name, res)
return res
}