/
interface.go
85 lines (74 loc) 路 1.99 KB
/
interface.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
package parse
import (
"go/ast"
"go/types"
"golang.org/x/tools/go/packages"
)
type (
Interface interface {
Name() string
IsExported() bool
File() string
TypeParameters() []Type
Methods() []Method
}
interface_ struct {
p *packages.Package
file string
typeSpec *ast.TypeSpec
interfaceType *ast.InterfaceType
}
)
func newInterface(p *packages.Package, file string, typeSpec *ast.TypeSpec, interfaceType *ast.InterfaceType) Interface {
return &interface_{p: p, file: file, typeSpec: typeSpec, interfaceType: interfaceType}
}
func (i *interface_) Name() string {
return i.typeSpec.Name.Name
}
func (i *interface_) IsExported() bool {
return i.typeSpec.Name.IsExported()
}
func (i *interface_) File() string {
return i.file
}
func (i *interface_) TypeParameters() (typeParameters []Type) {
if i.typeSpec.TypeParams != nil {
for _, tp := range i.typeSpec.TypeParams.List {
for _, ident := range tp.Names {
typeParameters = append(typeParameters, newType(i.p, ident, tp.Type))
}
}
}
return
}
func (i *interface_) Methods() []Method {
return i.methods(i.interfaceType)
}
func (i *interface_) methods(it *ast.InterfaceType) (methods []Method) {
for _, m := range it.Methods.List {
switch t := m.Type.(type) {
case *ast.FuncType:
for _, n := range m.Names {
o, _, _ := types.LookupFieldOrMethod(i.p.Types.Scope().Lookup(i.Name()).Type(), true, i.p.Types, n.Name)
methods = append(methods, newASTMethod(i.p, n, t, o.Type().Underlying().(*types.Signature).Variadic()))
}
case *ast.Ident:
switch dt := t.Obj.Decl.(type) {
case *ast.TypeSpec:
switch t := dt.Type.(type) {
case *ast.InterfaceType:
methods = append(methods, i.methods(t)...)
}
}
case *ast.SelectorExpr:
if tv, ok := i.p.TypesInfo.Types[t]; ok {
if ti, ok := tv.Type.Underlying().(*types.Interface); ok {
for j := 0; j < ti.NumMethods(); j++ {
methods = append(methods, newTypesMethod(ti.Method(j)))
}
}
}
}
}
return
}