From ab30d891d4b1d3a78cc457884ddb1af21e2c840d Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Thu, 16 Jan 2020 21:01:11 +0800 Subject: [PATCH] refactor: merge jmethod call codecall --- .../analysis/java_full_app_test.go | 8 ++- .../refactor/rename/rename_method.go | 12 +++- pkg/application/tbs/tbs_app.go | 18 +++--- pkg/domain/core_domain/code_call.go | 36 +++++++++++ pkg/domain/core_domain/code_property.go | 4 +- pkg/domain/jdomain/jclass_node.go | 2 +- pkg/domain/jdomain/jmethod.go | 2 +- pkg/domain/jdomain/jmethod_call.go | 58 ----------------- .../ast/full/java_full_converter.go | 21 ++++--- .../ast/full/java_full_listener.go | 62 ++++++++++--------- 10 files changed, 109 insertions(+), 114 deletions(-) diff --git a/pkg/application/analysis/java_full_app_test.go b/pkg/application/analysis/java_full_app_test.go index 9dc0b02..90107c3 100644 --- a/pkg/application/analysis/java_full_app_test.go +++ b/pkg/application/analysis/java_full_app_test.go @@ -2,6 +2,7 @@ package analysis import ( . "github.com/onsi/gomega" + "github.com/phodal/coca/pkg/domain/core_domain" "github.com/phodal/coca/pkg/domain/jdomain" "path/filepath" "testing" @@ -195,14 +196,15 @@ func Test_ShouldGetMethodCallParameters(t *testing.T) { callNodes := getCallNodes(codePath) - methodCallMap := make(map[string]jdomain.JMethodCall) + methodCallMap := make(map[string]core_domain.CodeCall) for _, method := range callNodes[0].Methods { for _, call := range method.MethodCalls { methodCallMap[call.MethodName] = call } } - g.Expect(methodCallMap["assertEquals"].Parameters).To(Equal([]string{"true", "true"})) + g.Expect(methodCallMap["assertEquals"].Parameters[0].TypeValue).To(Equal("true")) + g.Expect(methodCallMap["assertEquals"].Parameters[1].TypeValue).To(Equal("true")) } func Test_BuilderCallSplitIssue(t *testing.T) { @@ -213,7 +215,7 @@ func Test_BuilderCallSplitIssue(t *testing.T) { callNodes := getCallNodes(codePath) - methodCallMap := make(map[string]jdomain.JMethodCall) + methodCallMap := make(map[string]core_domain.CodeCall) for _, method := range callNodes[0].Methods { for _, call := range method.MethodCalls { methodCallMap[call.MethodName] = call diff --git a/pkg/application/refactor/rename/rename_method.go b/pkg/application/refactor/rename/rename_method.go index 7f22cd1..8b78fe6 100644 --- a/pkg/application/refactor/rename/rename_method.go +++ b/pkg/application/refactor/rename/rename_method.go @@ -2,6 +2,7 @@ package unused import ( "github.com/phodal/coca/pkg/application/refactor/rename/support" + "github.com/phodal/coca/pkg/domain/core_domain" "github.com/phodal/coca/pkg/domain/jdomain" "io/ioutil" "log" @@ -52,8 +53,15 @@ func startParse(nodes []jdomain.JClassNode, relates []support.RefactorChangeRela } } -func methodCallToMethodModel(call jdomain.JMethodCall) jdomain.JMethod { - return jdomain.JMethod{Name: call.MethodName, Type: call.Type, StartLine: call.StartLine, StartLinePosition: call.StartLinePosition, StopLine: call.StopLine, StopLinePosition: call.StopLinePosition} +func methodCallToMethodModel(call core_domain.CodeCall) jdomain.JMethod { + return jdomain.JMethod{ + Name: call.MethodName, + Type: call.Type, + StartLine: call.Position.StartLine, + StartLinePosition: call.Position.StartLinePosition, + StopLine: call.Position.StopLine, + StopLinePosition: call.Position.StopLinePosition, + } } func updateSelfRefs(node jdomain.JClassNode, method jdomain.JMethod, info *support.PackageClassInfo) { diff --git a/pkg/application/tbs/tbs_app.go b/pkg/application/tbs/tbs_app.go index 9b71e96..43328e3 100644 --- a/pkg/application/tbs/tbs_app.go +++ b/pkg/application/tbs/tbs_app.go @@ -38,7 +38,7 @@ func (a TbsApp) AnalysisPath(deps []jdomain.JClassNode, identifiersMap map[strin checkEmptyTest(clz.FilePath, annotation, &results, method, &testType) } - var methodCallMap = make(map[string][]jdomain.JMethodCall) + var methodCallMap = make(map[string][]core_domain.CodeCall) var hasAssert = false for index, methodCall := range currentMethodCalls { if methodCall.MethodName == "" { @@ -85,7 +85,7 @@ func checkAssert(hasAssert bool, clz jdomain.JClassNode, method jdomain.JMethod, } } -func updateMethodCallsForSelfCall(method jdomain.JMethod, clz jdomain.JClassNode, callMethodMap map[string]jdomain.JMethod) []jdomain.JMethodCall { +func updateMethodCallsForSelfCall(method jdomain.JMethod, clz jdomain.JClassNode, callMethodMap map[string]jdomain.JMethod) []core_domain.CodeCall { currentMethodCalls := method.MethodCalls for _, methodCall := range currentMethodCalls { if methodCall.Class == clz.Class { @@ -98,10 +98,10 @@ func updateMethodCallsForSelfCall(method jdomain.JMethod, clz jdomain.JClassNode return currentMethodCalls } -func checkRedundantAssertionTest(path string, call jdomain.JMethodCall, method jdomain.JMethod, results *[]TestBadSmell, testType *string) { +func checkRedundantAssertionTest(path string, call core_domain.CodeCall, method jdomain.JMethod, results *[]TestBadSmell, testType *string) { TWO_PARAMETERS := 2 if len(call.Parameters) == TWO_PARAMETERS { - if call.Parameters[0] == call.Parameters[1] { + if call.Parameters[0].TypeValue == call.Parameters[1].TypeValue { *testType = "RedundantAssertionTest" tbs := TestBadSmell{ FileName: path, @@ -115,7 +115,7 @@ func checkRedundantAssertionTest(path string, call jdomain.JMethodCall, method j } } -func checkDuplicateAssertTest(clz jdomain.JClassNode, results *[]TestBadSmell, methodCallMap map[string][]jdomain.JMethodCall, method jdomain.JMethod, testType *string) { +func checkDuplicateAssertTest(clz jdomain.JClassNode, results *[]TestBadSmell, methodCallMap map[string][]core_domain.CodeCall, method jdomain.JMethod, testType *string) { var isDuplicateAssert = false for _, methodCall := range methodCallMap { if len(methodCall) >= constants.DuplicatedAssertionLimitLength { @@ -138,28 +138,28 @@ func checkDuplicateAssertTest(clz jdomain.JClassNode, results *[]TestBadSmell, m } } -func checkSleepyTest(path string, method jdomain.JMethodCall, jMethod jdomain.JMethod, results *[]TestBadSmell, testType *string) { +func checkSleepyTest(path string, method core_domain.CodeCall, jMethod jdomain.JMethod, results *[]TestBadSmell, testType *string) { if method.IsThreadSleep() { *testType = "SleepyTest" tbs := TestBadSmell{ FileName: path, Type: *testType, Description: "", - Line: method.StartLine, + Line: method.Position.StartLine, } *results = append(*results, tbs) } } -func checkRedundantPrintTest(path string, mCall jdomain.JMethodCall, results *[]TestBadSmell, testType *string) { +func checkRedundantPrintTest(path string, mCall core_domain.CodeCall, results *[]TestBadSmell, testType *string) { if mCall.IsSystemOutput() { *testType = "RedundantPrintTest" tbs := TestBadSmell{ FileName: path, Type: *testType, Description: "", - Line: mCall.StartLine, + Line: mCall.Position.StartLine, } *results = append(*results, tbs) diff --git a/pkg/domain/core_domain/code_call.go b/pkg/domain/core_domain/code_call.go index 2abf4ba..79f9806 100644 --- a/pkg/domain/core_domain/code_call.go +++ b/pkg/domain/core_domain/code_call.go @@ -1,5 +1,10 @@ package core_domain +import ( + "github.com/phodal/coca/pkg/infrastructure/constants" + "strings" +) + type CodeCall struct { Package string Type string @@ -8,3 +13,34 @@ type CodeCall struct { Parameters []CodeProperty Position CodePosition } + +func NewCodeMethodCall() CodeCall { + return CodeCall{} +} + +func (c *CodeCall) BuildFullMethodName() string { + isConstructor := c.MethodName == "" + if isConstructor { + return c.Package + "." + c.Class + } + return c.Package + "." + c.Class + "." + c.MethodName +} + +func (c *CodeCall) IsSystemOutput() bool { + return c.Class == "System.out" && (c.MethodName == "println" || c.MethodName == "printf" || c.MethodName == "print") +} + +func (c *CodeCall) IsThreadSleep() bool { + return c.MethodName == "sleep" && c.Class == "Thread" +} + +func (c *CodeCall) HasAssertion() bool { + methodName := strings.ToLower(c.MethodName) + for _, assertion := range constants.ASSERTION_LIST { + if strings.HasPrefix(methodName, assertion) { + return true + } + } + + return false +} diff --git a/pkg/domain/core_domain/code_property.go b/pkg/domain/core_domain/code_property.go index 6cb55c6..3f9157f 100644 --- a/pkg/domain/core_domain/code_property.go +++ b/pkg/domain/core_domain/code_property.go @@ -9,9 +9,9 @@ type CodeProperty struct { Parameters []CodeProperty } -func NewCodeParameter(typeName string, typeType string) CodeProperty { +func NewCodeParameter(typeValue string, typeType string) CodeProperty { return CodeProperty{ - TypeValue: typeName, + TypeValue: typeValue, TypeType: typeType, } } diff --git a/pkg/domain/jdomain/jclass_node.go b/pkg/domain/jdomain/jclass_node.go index 55003ce..8e48ed3 100644 --- a/pkg/domain/jdomain/jclass_node.go +++ b/pkg/domain/jdomain/jclass_node.go @@ -12,7 +12,7 @@ type JClassNode struct { FilePath string Fields []core_domain.CodeField Methods []JMethod - MethodCalls []JMethodCall + MethodCalls []core_domain.CodeCall Extend string Implements []string Annotations []core_domain.CodeAnnotation diff --git a/pkg/domain/jdomain/jmethod.go b/pkg/domain/jdomain/jmethod.go index e69967a..9b16237 100644 --- a/pkg/domain/jdomain/jmethod.go +++ b/pkg/domain/jdomain/jmethod.go @@ -14,7 +14,7 @@ type JMethod struct { StopLine int StopLinePosition int Parameters []core_domain.CodeProperty - MethodCalls []JMethodCall + MethodCalls []core_domain.CodeCall Override bool Annotations []core_domain.CodeAnnotation IsConstructor bool diff --git a/pkg/domain/jdomain/jmethod_call.go b/pkg/domain/jdomain/jmethod_call.go index 0d0d7d5..ce424f0 100644 --- a/pkg/domain/jdomain/jmethod_call.go +++ b/pkg/domain/jdomain/jmethod_call.go @@ -1,60 +1,2 @@ package jdomain -import ( - "github.com/phodal/coca/pkg/infrastructure/constants" - "strings" -) - -type JMethodCall struct { - Package string - Type string - Class string - MethodName string - Parameters []string - StartLine int - StartLinePosition int - StopLine int - StopLinePosition int -} - -func NewJMethodCall() JMethodCall { - return JMethodCall{ - Package: "", - Type: "", - Class: "", - MethodName: "", - Parameters: nil, - StartLine: 0, - StartLinePosition: 0, - StopLine: 0, - StopLinePosition: 0, - } -} - -func (c *JMethodCall) BuildFullMethodName() string { - isConstructor := c.MethodName == "" - if isConstructor { - return c.Package + "." + c.Class - } - return c.Package + "." + c.Class + "." + c.MethodName -} - -func (c *JMethodCall) IsSystemOutput() bool { - return c.Class == "System.out" && (c.MethodName == "println" || c.MethodName == "printf" || c.MethodName == "print") -} - -func (c *JMethodCall) IsThreadSleep() bool { - return c.MethodName == "sleep" && c.Class == "Thread" -} - -func (c *JMethodCall) HasAssertion() bool { - methodName := strings.ToLower(c.MethodName) - for _, assertion := range constants.ASSERTION_LIST { - if strings.HasPrefix(methodName, assertion) { - return true - } - } - - return false -} - diff --git a/pkg/infrastructure/ast/full/java_full_converter.go b/pkg/infrastructure/ast/full/java_full_converter.go index 4b13682..7e1b82c 100644 --- a/pkg/infrastructure/ast/full/java_full_converter.go +++ b/pkg/infrastructure/ast/full/java_full_converter.go @@ -3,7 +3,6 @@ package full import ( "github.com/phodal/coca/languages/java" "github.com/phodal/coca/pkg/domain/core_domain" - "github.com/phodal/coca/pkg/domain/jdomain" "strings" ) @@ -23,7 +22,7 @@ func BuildMethodParameters(parameters parser.IFormalParametersContext) []core_do return methodParams } -func BuildMethodCallMethods(jMethodCall *jdomain.JMethodCall, callee string, targetType string, ctx *parser.MethodCallContext) { +func BuildMethodCallMethods(jMethodCall *core_domain.CodeCall, callee string, targetType string, ctx *parser.MethodCallContext) { methodName := callee packageName := currentPkg @@ -53,19 +52,21 @@ func BuildMethodCallMethods(jMethodCall *jdomain.JMethodCall, callee string, tar jMethodCall.Class = targetType } -func BuildMethodCallLocation(jMethodCall *jdomain.JMethodCall, ctx *parser.MethodCallContext, callee string) { - jMethodCall.StartLine = ctx.GetStart().GetLine() - jMethodCall.StartLinePosition = ctx.GetStart().GetColumn() - jMethodCall.StopLine = ctx.GetStop().GetLine() - jMethodCall.StopLinePosition = jMethodCall.StartLinePosition + len(callee) +func BuildMethodCallLocation(jMethodCall *core_domain.CodeCall, ctx *parser.MethodCallContext, callee string) { + jMethodCall.Position.StartLine = ctx.GetStart().GetLine() + jMethodCall.Position.StartLinePosition = ctx.GetStart().GetColumn() + jMethodCall.Position.StopLine = ctx.GetStop().GetLine() + jMethodCall.Position.StopLinePosition = jMethodCall.Position.StartLinePosition + len(callee) } -func BuildMethodCallParameters(jMethodCall *jdomain.JMethodCall, ctx *parser.MethodCallContext) { +func BuildMethodCallParameters(jMethodCall *core_domain.CodeCall, ctx *parser.MethodCallContext) { if ctx.ExpressionList() != nil { - var parameters []string + var parameters []core_domain.CodeProperty for _, expression := range ctx.ExpressionList().(*parser.ExpressionListContext).AllExpression() { expressionCtx := expression.(*parser.ExpressionContext) - parameters = append(parameters, expressionCtx.GetText()) + + parameter := core_domain.NewCodeParameter(expressionCtx.GetText(), "") + parameters = append(parameters, parameter) } jMethodCall.Parameters = parameters } diff --git a/pkg/infrastructure/ast/full/java_full_listener.go b/pkg/infrastructure/ast/full/java_full_listener.go index 6f2f16b..902543e 100644 --- a/pkg/infrastructure/ast/full/java_full_listener.go +++ b/pkg/infrastructure/ast/full/java_full_listener.go @@ -16,7 +16,7 @@ var clzs []string var currentPkg string var currentClz string var fields []core_domain.CodeField -var methodCalls []jdomain.JMethodCall +var methodCalls []core_domain.CodeCall var currentType string var mapFields = make(map[string]string) @@ -226,9 +226,9 @@ func (s *JavaFullListener) EnterFieldDeclaration(ctx *parser.FieldDeclarationCon for _, declarator := range decelerators.(*parser.VariableDeclaratorsContext).AllVariableDeclarator() { var typeCtx *parser.ClassOrInterfaceTypeContext = nil - typeCtx = BuildTypeCtxByIndex(typeType, typeCtx,0) + typeCtx = BuildTypeCtxByIndex(typeType, typeCtx, 0) if typeType.GetChildCount() > 1 { - typeCtx = BuildTypeCtxByIndex(typeType, typeCtx,1) + typeCtx = BuildTypeCtxByIndex(typeType, typeCtx, 1) } if typeCtx == nil { @@ -469,23 +469,26 @@ func buildCreatedCall(createdName string, ctx *parser.CreatorContext) { method := methodMap[getMethodMapName(currentMethod)] fullType, _ := WarpTargetFullType(createdName) - jMethodCall := &jdomain.JMethodCall{ - Package: RemoveTarget(fullType), - Type: "CreatorClass", - Class: createdName, - MethodName: "", + position := core_domain.CodePosition{ StartLine: ctx.GetStart().GetLine(), StartLinePosition: ctx.GetStart().GetColumn(), StopLine: ctx.GetStop().GetLine(), StopLinePosition: ctx.GetStop().GetColumn(), } + jMethodCall := &core_domain.CodeCall{ + Package: RemoveTarget(fullType), + Type: "CreatorClass", + Class: createdName, + Position: position, + } + method.MethodCalls = append(method.MethodCalls, *jMethodCall) methodMap[getMethodMapName(currentMethod)] = method } func (s *JavaFullListener) EnterMethodCall(ctx *parser.MethodCallContext) { - var jMethodCall = jdomain.NewJMethodCall() + var jMethodCall = core_domain.NewCodeMethodCall() targetCtx := ctx.GetParent().GetChild(0).(antlr.ParseTree) var targetType = ParseTargetType(targetCtx.GetText()) @@ -506,7 +509,7 @@ func (s *JavaFullListener) EnterMethodCall(ctx *parser.MethodCallContext) { sendResultToMethodCallMap(jMethodCall) } -func sendResultToMethodCallMap(jMethodCall jdomain.JMethodCall) { +func sendResultToMethodCallMap(jMethodCall core_domain.CodeCall) { methodCalls = append(methodCalls, jMethodCall) method := methodMap[getMethodMapName(currentMethod)] @@ -544,21 +547,21 @@ func (s *JavaFullListener) EnterExpression(ctx *parser.ExpressionContext) { fullType, _ := WarpTargetFullType(targetType) - startLine := ctx.GetStart().GetLine() startLinePosition := ctx.GetStart().GetColumn() - stopLine := ctx.GetStop().GetLine() - stopLinePosition := startLinePosition + len(text) - - jMethodCall := &jdomain.JMethodCall{ - Package: RemoveTarget(fullType), - Type: "lambda", - Class: targetType, - MethodName: methodName, - Parameters: nil, - StartLine: startLine, + + position := core_domain.CodePosition{ + StartLine: ctx.GetStart().GetLine(), StartLinePosition: startLinePosition, - StopLine: stopLine, - StopLinePosition: stopLinePosition, + StopLine: ctx.GetStop().GetLine(), + StopLinePosition: startLinePosition + len(text), + } + + jMethodCall := &core_domain.CodeCall{ + Package: RemoveTarget(fullType), + Type: "lambda", + Class: targetType, + MethodName: methodName, + Position: position, } sendResultToMethodCallMap(*jMethodCall) } @@ -578,17 +581,20 @@ func buildExtend(extendName string) { func buildFieldCall(typeType string, ctx *parser.FieldDeclarationContext) { target, _ := WarpTargetFullType(typeType) if target != "" { - jMethodCall := &jdomain.JMethodCall{ - Package: RemoveTarget(target), - Type: "field", - Class: typeType, - MethodName: "", + position := core_domain.CodePosition{ StartLine: ctx.GetStart().GetLine(), StartLinePosition: ctx.GetStart().GetColumn(), StopLine: ctx.GetStop().GetLine(), StopLinePosition: ctx.GetStop().GetColumn(), } + jMethodCall := &core_domain.CodeCall{ + Package: RemoveTarget(target), + Type: "field", + Class: typeType, + Position: position, + } + currentNode.MethodCalls = append(currentNode.MethodCalls, *jMethodCall) } } -- GitLab