JavaRefactorListener.go 5.8 KB
Newer Older
P
Phodal HUANG 已提交
1 2 3
package base

import (
P
Phodal Huang 已提交
4 5
	. "coca/language/java"
	. "coca/refactor/base/models"
P
Phodal HUANG 已提交
6 7
	"strings"
	"unicode"
P
Phodal HUANG 已提交
8 9 10 11 12 13 14 15 16 17
)

var node *JFullIdentifier;

type JavaRefactorListener struct {
	BaseJavaParserListener
}

func (s *JavaRefactorListener) EnterPackageDeclaration(ctx *PackageDeclarationContext) {
	node.Pkg = ctx.QualifiedName().GetText()
P
Phodal HUANG 已提交
18 19 20 21 22 23

	startLine := ctx.GetStart().GetLine()
	stopLine := ctx.GetStop().GetLine()

	pkgInfo := &JPkgInfo{node.Pkg, startLine, stopLine}
	node.SetPkgInfo(*pkgInfo)
P
Phodal HUANG 已提交
24 25
}

P
Phodal HUANG 已提交
26 27
func (s *JavaRefactorListener) EnterImportDeclaration(ctx *ImportDeclarationContext) {
	importText := ctx.QualifiedName().GetText()
P
Phodal HUANG 已提交
28 29 30
	if ctx.MUL() != nil {
		importText = importText + ".*"
	}
P
Phodal HUANG 已提交
31 32 33 34 35 36 37 38
	startLine := ctx.GetStart().GetLine()
	stopLine := ctx.GetStop().GetLine()

	jImport := &JImport{importText, startLine, stopLine}

	node.AddImport(*jImport)
}

P
Phodal HUANG 已提交
39 40 41 42 43
func (s *JavaRefactorListener) EnterClassDeclaration(ctx *ClassDeclarationContext) {
	node.Type = "Class"
	node.Name = ctx.IDENTIFIER().GetText()
}

P
Phodal HUANG 已提交
44 45 46 47 48 49 50 51 52
// throws
func (s *JavaRefactorListener) EnterQualifiedNameList(ctx *QualifiedNameListContext) {
	for _, qualified := range ctx.AllQualifiedName() {
		startLine := ctx.GetStart().GetLine()
		stopLine := ctx.GetStop().GetLine()
		field := &JField{qualified.GetText(), node.Pkg, startLine, stopLine}
		node.AddField(*field)
	}
}
P
Phodal HUANG 已提交
53

P
Phodal HUANG 已提交
54 55 56 57 58 59 60
func (s *JavaRefactorListener) EnterCatchType(ctx *CatchTypeContext) {
	for _, qualified := range ctx.AllQualifiedName() {
		startLine := ctx.GetStart().GetLine()
		stopLine := ctx.GetStop().GetLine()
		field := &JField{qualified.GetText(), node.Pkg, startLine, stopLine}
		node.AddField(*field)
	}
P
Phodal HUANG 已提交
61 62
}

P
Phodal HUANG 已提交
63
func (s *JavaRefactorListener) EnterInterfaceMethodDeclaration(ctx *InterfaceMethodDeclarationContext) {
P
Phodal HUANG 已提交
64
	//fmt.Println(ctx.TypeTypeOrVoid())
P
Phodal HUANG 已提交
65
	startLine := ctx.GetStart().GetLine()
P
Phodal HUANG 已提交
66
	startLinePosition := ctx.GetStart().GetColumn()
P
Phodal HUANG 已提交
67
	stopLine := ctx.GetStop().GetLine()
P
Phodal HUANG 已提交
68
	stopLinePosition := ctx.GetStop().GetColumn()
P
Phodal HUANG 已提交
69 70 71 72 73 74 75 76 77
	name := ctx.IDENTIFIER().GetText()
	//XXX: find the start position of {, not public
	method := &JFullMethod{name, startLine, startLinePosition, stopLine, stopLinePosition}
	node.AddMethod(*method)
}

func (s *JavaRefactorListener) EnterInterfaceDeclaration(ctx *InterfaceDeclarationContext) {
	node.Type = "Interface"
	node.Name = ctx.IDENTIFIER().GetText()
P
Phodal HUANG 已提交
78
}
P
Phodal HUANG 已提交
79

P
Phodal HUANG 已提交
80
func (s *JavaRefactorListener) EnterTypeType(ctx *TypeTypeContext) {
P
Phodal HUANG 已提交
81 82
	startLine := ctx.GetStart().GetLine()
	stopLine := ctx.GetStop().GetLine()
P
Phodal HUANG 已提交
83
	field := &JField{ctx.GetText(), node.Pkg, startLine, stopLine}
P
Phodal HUANG 已提交
84
	node.AddField(*field)
P
Phodal HUANG 已提交
85
}
P
Phodal HUANG 已提交
86

P
Phodal HUANG 已提交
87 88 89 90 91 92 93
func (s *JavaRefactorListener) EnterClassOrInterfaceType(ctx *ClassOrInterfaceTypeContext) {
	identifiers := ctx.AllIDENTIFIER()
	for index, _ := range identifiers {
		context := ctx.IDENTIFIER(index)
		name := context.GetText()
		startLine := ctx.GetStart().GetLine()
		stopLine := ctx.GetStop().GetLine()
P
Phodal HUANG 已提交
94

P
Phodal HUANG 已提交
95 96
		field := &JField{name, node.Pkg, startLine, stopLine}
		node.AddField(*field)
P
Phodal HUANG 已提交
97
	}
P
Phodal HUANG 已提交
98 99
}

P
Phodal HUANG 已提交
100 101 102 103 104 105 106 107 108 109
func (s *JavaRefactorListener) EnterAnnotation(ctx *AnnotationContext) {
	annotation := ctx.QualifiedName().GetText()

	startLine := ctx.GetStart().GetLine()
	stopLine := ctx.GetStop().GetLine()

	field := &JField{annotation, node.Pkg, startLine, stopLine}
	node.AddField(*field)
}

P
Phodal HUANG 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
func (s *JavaRefactorListener) EnterLambdaParameters(ctx *LambdaParametersContext) {
	identifiers := ctx.AllIDENTIFIER()
	for index, _ := range identifiers {
		context := ctx.IDENTIFIER(index)
		name := context.GetText()
		startLine := ctx.GetStart().GetLine()
		stopLine := ctx.GetStop().GetLine()

		field := &JField{name, node.Pkg, startLine, stopLine}
		node.AddField(*field)
	}
}

func (s *JavaRefactorListener) EnterMethodCall(ctx *MethodCallContext) {
	text := ctx.IDENTIFIER().GetText()
	startLine := ctx.GetStart().GetLine()
	stopLine := ctx.GetStop().GetLine()
	field := &JField{text, node.Pkg, startLine, stopLine}
	node.AddField(*field)
}

P
Phodal HUANG 已提交
131 132 133 134 135 136 137 138 139 140 141 142
func (s *JavaRefactorListener) EnterExpressionList(ctx *ExpressionListContext) {
	for _, expression := range ctx.AllExpression() {
		expText := expression.GetText()
		if isUppercaseText(expText) {
			startLine := ctx.GetStart().GetLine()
			stopLine := ctx.GetStop().GetLine()
			field := &JField{expText, node.Pkg, startLine, stopLine}
			node.AddField(*field)
		}
	}
}

143 144 145 146 147 148 149 150 151 152 153 154
func (s *JavaRefactorListener) EnterStatement(ctx *StatementContext) {
	for _, expression := range ctx.AllExpression() {
		expText := expression.GetText()
		if isUppercaseText(expText) {
			startLine := ctx.GetStart().GetLine()
			stopLine := ctx.GetStop().GetLine()
			field := &JField{expText, node.Pkg, startLine, stopLine}
			node.AddField(*field)
		}
	}
}

P
Phodal HUANG 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168
func (s *JavaRefactorListener) EnterCreatedName(ctx *CreatedNameContext) {
	identifiers := ctx.AllIDENTIFIER()
	for index, _ := range identifiers {
		context := ctx.IDENTIFIER(index)
		name := context.GetText()
		startLine := ctx.GetStart().GetLine()
		stopLine := ctx.GetStop().GetLine()

		field := &JField{name, node.Pkg, startLine, stopLine}
		node.AddField(*field)
	}
}

func (s *JavaRefactorListener) EnterExpression(ctx *ExpressionContext) {
P
Phodal HUANG 已提交
169 170 171 172 173 174 175 176 177 178 179
	if ctx.Expression(0) != nil {
		expText := ctx.Expression(0).GetText()

		if isUppercaseText(expText) {
			startLine := ctx.GetStart().GetLine()
			stopLine := ctx.GetStop().GetLine()
			field := &JField{expText, node.Pkg, startLine, stopLine}
			node.AddField(*field)
		}
	}

P
Phodal HUANG 已提交
180 181 182 183 184 185 186 187
	if ctx.GetBop() == nil {
		return
	}

	if ctx.GetBop().GetText() != "." {
		return
	}

P
Phodal HUANG 已提交
188 189 190 191 192 193 194 195 196 197
	if ctx.Expression(0) != nil {
		expText := ctx.Expression(0).GetText()
		// UUID.toString 形式的直接调用
		if ctx.MethodCall() != nil {
			if isUppercaseText(expText) {
				startLine := ctx.GetStart().GetLine()
				stopLine := ctx.GetStop().GetLine()
				field := &JField{expText, node.Pkg, startLine, stopLine}
				node.AddField(*field)
			}
P
Phodal HUANG 已提交
198 199 200 201
		}
	}
}

P
Phodal HUANG 已提交
202 203 204 205
func isUppercaseText(text string) bool {
	return !strings.Contains(text, ".") && unicode.IsUpper([]rune(text)[0])
}

P
Phodal HUANG 已提交
206 207 208
func (s *JavaRefactorListener) InitNode(identifier *JFullIdentifier) {
	node = identifier
}