From 919940bff93a7d88673935045e39fe3cacdff42b Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Tue, 7 Jan 2020 13:37:18 +0800 Subject: [PATCH] refactor: extract full listener --- cmd/evaluate_test.go | 4 +- .../ast/full/java_full_listener.go | 48 +++++++++++-------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/cmd/evaluate_test.go b/cmd/evaluate_test.go index a639aac..497c0fd 100644 --- a/cmd/evaluate_test.go +++ b/cmd/evaluate_test.go @@ -5,11 +5,9 @@ import ( ) func TestEvaluate(t *testing.T) { - abs := "../_fixtures/arch" - analysis := []cmdTestCase{{ name: "analysis", - cmd: "analysis -p " + abs, + cmd: "analysis -p ../_fixtures/arch", golden: "", }} runTestCmd(t, analysis) diff --git a/core/infrastructure/ast/full/java_full_listener.go b/core/infrastructure/ast/full/java_full_listener.go index 2380e94..1529c12 100644 --- a/core/infrastructure/ast/full/java_full_listener.go +++ b/core/infrastructure/ast/full/java_full_listener.go @@ -510,10 +510,27 @@ func (s *JavaFullListener) EnterMethodCall(ctx *parser.MethodCallContext) { callee := ctx.GetChild(0).(antlr.ParseTree).GetText() - jMethodCall.StartLine = ctx.GetStart().GetLine() - jMethodCall.StartLinePosition = ctx.GetStart().GetColumn() - jMethodCall.StopLine = ctx.GetStop().GetLine() - jMethodCall.StopLinePosition = jMethodCall.StartLinePosition + len(callee) + buildMethodCallLocation(&jMethodCall, ctx, callee) + buildMethodCallMethod(&jMethodCall, callee, targetType, ctx) + buildMethodCallParameters(&jMethodCall, ctx) + + addMethodCallToMap(jMethodCall) +} + +func buildMethodCallParameters(jMethodCall *domain.JMethodCall, ctx *parser.MethodCallContext) { + if ctx.ExpressionList() != nil { + var parameters []string + for _, expression := range ctx.ExpressionList().(*parser.ExpressionListContext).AllExpression() { + expressionCtx := expression.(*parser.ExpressionContext) + parameters = append(parameters, expressionCtx.GetText()) + } + jMethodCall.Parameters = parameters + } +} + +func buildMethodCallMethod(jMethodCall *domain.JMethodCall, callee string, targetType string, ctx *parser.MethodCallContext) { + methodName := callee + packageName := currentPkg fullType, callType := warpTargetFullType(targetType) if targetType == "super" || callee == "super" { @@ -522,9 +539,6 @@ func (s *JavaFullListener) EnterMethodCall(ctx *parser.MethodCallContext) { } jMethodCall.Type = callType - methodName := callee - packageName := currentPkg - if fullType != "" { packageName = removeTarget(fullType) methodName = callee @@ -556,20 +570,16 @@ func (s *JavaFullListener) EnterMethodCall(ctx *parser.MethodCallContext) { jMethodCall.Package = packageName jMethodCall.MethodName = methodName jMethodCall.Class = targetType +} - if ctx.ExpressionList() != nil { - var parameters []string - for _, expression := range ctx.ExpressionList().(*parser.ExpressionListContext).AllExpression() { - expressionCtx := expression.(*parser.ExpressionContext) - parameters = append(parameters, expressionCtx.GetText()) - } - jMethodCall.Parameters = parameters - } - - addMethodCall(jMethodCall) +func buildMethodCallLocation(jMethodCall *domain.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 addMethodCall(jMethodCall domain.JMethodCall) { +func addMethodCallToMap(jMethodCall domain.JMethodCall) { methodCalls = append(methodCalls, jMethodCall) method := methodMap[getMethodMapName(currentMethod)] @@ -644,7 +654,7 @@ func (s *JavaFullListener) EnterExpression(ctx *parser.ExpressionContext) { StopLine: stopLine, StopLinePosition: stopLinePosition, } - addMethodCall(*jMethodCall) + addMethodCallToMap(*jMethodCall) } } -- GitLab