-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
178 lines (150 loc) · 4.66 KB
/
main.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"fmt"
"reflect"
"strings"
"github.com/rishabh-arya95/antlr_poc/parser"
"github.com/antlr/antlr4/runtime/Go/antlr"
)
type CodePosition struct {
StartLine int
StartLinePosition int
StopLine int
StopLinePosition int
}
type CodeFunction struct {
Name string
ReturnType string
Modifiers []string
Position CodePosition
Parameters []CodeProperty
}
type CodeProperty struct {
// Modifiers []string
ParamName string
TypeValue string
TypeType string
// ReturnTypes []CodeProperty
// Parameters []CodeProperty
}
func NewCodeParameter(typeType string, typeValue string) CodeProperty {
return CodeProperty{
TypeValue: typeValue,
TypeType: typeType,
}
}
type TreeShapeListener struct {
*parser.BaseJavaParserListener
}
var isTest, isIgnore = false, false
var imports []string
var testFunctions []CodeFunction
func NewTreeShapeListener() *TreeShapeListener {
return new(TreeShapeListener)
}
func GetNodeIndex(node antlr.ParseTree) int {
if node == nil || node.GetParent() == nil {
return -1
}
parent := node.GetParent()
for i := 0; i < parent.GetChildCount(); i++ {
if parent.GetChild(i) == node {
return i
}
}
return 0
}
func (t *TreeShapeListener) EnterImportDeclaration(ctx *parser.ImportDeclarationContext) {
importText := ctx.QualifiedName().GetText()
imports = append(imports, importText)
}
func (t *TreeShapeListener) EnterAnnotation(ctx *parser.AnnotationContext) {
if ctx.QualifiedName() == nil {
return
}
if ctx.QualifiedName().GetText() == "Test" || ctx.QualifiedName().GetText() == "ParameterizedTest" {
isTest = true
}
if ctx.QualifiedName().GetText() == "Ignore" {
isIgnore = true
}
}
// // EnterFormalParameters(ctx *parser.FormalParametersContext) {}
// func (t *TreeShapeListener) EnterFormalParameterList(ctx *parser.FormalParameterListContext) {
// formalParameter := ctx.AllFormalParameter()
// for _, param := range formalParameter {
// paramContext := param.(*parser.FormalParameterContext)
// paramType := paramContext.TypeType().GetText()
// paramValue := paramContext.VariableDeclaratorId().(*parser.VariableDeclaratorIdContext).Identifier().GetText()
// fmt.Println(paramType, paramValue)
// }
// }
func (t *TreeShapeListener) EnterMethodDeclaration(ctx *parser.MethodDeclarationContext) {
if isTest {
startLine := ctx.GetStart().GetLine()
startLinePosition := ctx.GetStart().GetColumn()
stopLine := ctx.GetStop().GetLine()
stopLinePosition := ctx.GetStop().GetColumn()
name := ""
parameters := ctx.FormalParameters()
if ctx.Identifier() != nil {
name = ctx.Identifier().GetText()
}
typeType := ctx.TypeTypeOrVoid().GetText()
position := CodePosition{
StartLine: startLine,
StartLinePosition: startLinePosition,
StopLine: stopLine,
StopLinePosition: stopLinePosition,
}
currentMethod := CodeFunction{
Name: name,
ReturnType: typeType,
Position: position,
Parameters: BuildMethodParameters(parameters),
}
if reflect.TypeOf(ctx.GetParent().GetParent()).String() == "*parser.ClassBodyDeclarationContext" {
bodyCtx := ctx.GetParent().GetParent().(*parser.ClassBodyDeclarationContext)
for _, modifier := range bodyCtx.AllModifier() {
if !strings.Contains(modifier.GetText(), "@") {
currentMethod.Modifiers = append(currentMethod.Modifiers, modifier.GetText())
}
}
}
testFunctions = append(testFunctions, currentMethod)
}
isTest = false
isIgnore = false
}
func main() {
input, err := antlr.NewFileStream("./testdata/CalculatorTest.java")
if err != nil {
panic(err)
}
lexer := parser.NewJavaLexer(input)
stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
p := parser.NewJavaParser(stream)
antlr.NewParseTreeWalker().Walk(NewTreeShapeListener(), p.CompilationUnit())
fmt.Printf("imports: %+v\n", imports)
fmt.Printf("Test Cases: %+v\n", testFunctions)
fmt.Printf("Total Executable Test Cases: %+v\n", len(testFunctions))
}
func BuildMethodParameters(parameters parser.IFormalParametersContext) []CodeProperty {
var methodParams []CodeProperty = nil
if parameters == nil {
return methodParams
}
parameterList, isParamList := parameters.GetChild(1).(*parser.FormalParameterListContext)
if !isParamList {
return methodParams
}
formalParameter := parameterList.AllFormalParameter()
for _, param := range formalParameter {
paramContext := param.(*parser.FormalParameterContext)
paramType := paramContext.TypeType().GetText()
paramValue := paramContext.VariableDeclaratorId().(*parser.VariableDeclaratorIdContext).Identifier().GetText()
parameter := NewCodeParameter(paramType, paramValue)
methodParams = append(methodParams, parameter)
}
return methodParams
}