/
get.go
106 lines (79 loc) · 1.91 KB
/
get.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
package weave
import (
"go/ast"
"go/parser"
"go/token"
"strings"
)
// applyGetJP applies any advice for get joinpoints
func (w *Weave) applyGetJP(fname string, stuff string) string {
rout := stuff
importsNeeded := []string{}
for i := 0; i < len(w.aspects); i++ {
aspect := w.aspects[i]
if !aspect.pointkut.isGet() {
continue
}
pk := aspect.pointkut.def
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, fname, rout, parser.Mode(0))
if err != nil {
w.flog.Println("Failed to parse source: %s", err.Error())
}
linecnt := 0
for _, decl := range file.Decls {
fn, ok := decl.(*ast.FuncDecl)
if !ok {
continue
}
for x := 0; x < len(fn.Body.List); x++ {
as, ok2 := fn.Body.List[x].(*ast.ExprStmt)
if !ok2 {
continue
}
blah, ok3 := as.X.(*ast.CallExpr)
if !ok3 {
continue
}
// can either be a unary || a ident (so far)
fn2, ok4 := blah.Args[0].(*ast.UnaryExpr)
if !ok4 {
// look for ident
fn3, ok5 := blah.Args[0].(*ast.Ident)
if !ok5 {
continue
}
if pk != fn3.Name {
continue
}
} else {
// look for channel
blah2, ok4 := fn2.X.(*ast.Ident)
if !ok4 {
continue
}
if pk != blah2.Name {
continue
}
}
begin := fset.Position(as.Pos()).Line - 1
after := fset.Position(as.End()).Line + 1
before_advice := aspect.advize.before
after_advice := aspect.advize.after
if before_advice != "" {
rout = w.writeAtLine(fname, begin+linecnt, before_advice)
linecnt += strings.Count(before_advice, "\n") + 1
}
if after_advice != "" {
rout = w.writeAtLine(fname, after+linecnt-1, after_advice)
linecnt += strings.Count(after_advice, "\n") + 1
}
}
}
}
if len(importsNeeded) > 0 {
// add any imports for this piece of advice
rout = w.writeMissingImports(fname, rout, importsNeeded)
}
return rout
}