refactor: merge jmethod call codecall

上级 bb7dfa0a
...@@ -2,6 +2,7 @@ package analysis ...@@ -2,6 +2,7 @@ package analysis
import ( import (
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/phodal/coca/pkg/domain/core_domain"
"github.com/phodal/coca/pkg/domain/jdomain" "github.com/phodal/coca/pkg/domain/jdomain"
"path/filepath" "path/filepath"
"testing" "testing"
...@@ -195,14 +196,15 @@ func Test_ShouldGetMethodCallParameters(t *testing.T) { ...@@ -195,14 +196,15 @@ func Test_ShouldGetMethodCallParameters(t *testing.T) {
callNodes := getCallNodes(codePath) callNodes := getCallNodes(codePath)
methodCallMap := make(map[string]jdomain.JMethodCall) methodCallMap := make(map[string]core_domain.CodeCall)
for _, method := range callNodes[0].Methods { for _, method := range callNodes[0].Methods {
for _, call := range method.MethodCalls { for _, call := range method.MethodCalls {
methodCallMap[call.MethodName] = call 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) { func Test_BuilderCallSplitIssue(t *testing.T) {
...@@ -213,7 +215,7 @@ func Test_BuilderCallSplitIssue(t *testing.T) { ...@@ -213,7 +215,7 @@ func Test_BuilderCallSplitIssue(t *testing.T) {
callNodes := getCallNodes(codePath) callNodes := getCallNodes(codePath)
methodCallMap := make(map[string]jdomain.JMethodCall) methodCallMap := make(map[string]core_domain.CodeCall)
for _, method := range callNodes[0].Methods { for _, method := range callNodes[0].Methods {
for _, call := range method.MethodCalls { for _, call := range method.MethodCalls {
methodCallMap[call.MethodName] = call methodCallMap[call.MethodName] = call
......
...@@ -2,6 +2,7 @@ package unused ...@@ -2,6 +2,7 @@ package unused
import ( import (
"github.com/phodal/coca/pkg/application/refactor/rename/support" "github.com/phodal/coca/pkg/application/refactor/rename/support"
"github.com/phodal/coca/pkg/domain/core_domain"
"github.com/phodal/coca/pkg/domain/jdomain" "github.com/phodal/coca/pkg/domain/jdomain"
"io/ioutil" "io/ioutil"
"log" "log"
...@@ -52,8 +53,15 @@ func startParse(nodes []jdomain.JClassNode, relates []support.RefactorChangeRela ...@@ -52,8 +53,15 @@ func startParse(nodes []jdomain.JClassNode, relates []support.RefactorChangeRela
} }
} }
func methodCallToMethodModel(call jdomain.JMethodCall) jdomain.JMethod { func methodCallToMethodModel(call core_domain.CodeCall) jdomain.JMethod {
return jdomain.JMethod{Name: call.MethodName, Type: call.Type, StartLine: call.StartLine, StartLinePosition: call.StartLinePosition, StopLine: call.StopLine, StopLinePosition: call.StopLinePosition} 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) { func updateSelfRefs(node jdomain.JClassNode, method jdomain.JMethod, info *support.PackageClassInfo) {
......
...@@ -38,7 +38,7 @@ func (a TbsApp) AnalysisPath(deps []jdomain.JClassNode, identifiersMap map[strin ...@@ -38,7 +38,7 @@ func (a TbsApp) AnalysisPath(deps []jdomain.JClassNode, identifiersMap map[strin
checkEmptyTest(clz.FilePath, annotation, &results, method, &testType) 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 var hasAssert = false
for index, methodCall := range currentMethodCalls { for index, methodCall := range currentMethodCalls {
if methodCall.MethodName == "" { if methodCall.MethodName == "" {
...@@ -85,7 +85,7 @@ func checkAssert(hasAssert bool, clz jdomain.JClassNode, method jdomain.JMethod, ...@@ -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 currentMethodCalls := method.MethodCalls
for _, methodCall := range currentMethodCalls { for _, methodCall := range currentMethodCalls {
if methodCall.Class == clz.Class { if methodCall.Class == clz.Class {
...@@ -98,10 +98,10 @@ func updateMethodCallsForSelfCall(method jdomain.JMethod, clz jdomain.JClassNode ...@@ -98,10 +98,10 @@ func updateMethodCallsForSelfCall(method jdomain.JMethod, clz jdomain.JClassNode
return currentMethodCalls 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 TWO_PARAMETERS := 2
if len(call.Parameters) == TWO_PARAMETERS { if len(call.Parameters) == TWO_PARAMETERS {
if call.Parameters[0] == call.Parameters[1] { if call.Parameters[0].TypeValue == call.Parameters[1].TypeValue {
*testType = "RedundantAssertionTest" *testType = "RedundantAssertionTest"
tbs := TestBadSmell{ tbs := TestBadSmell{
FileName: path, FileName: path,
...@@ -115,7 +115,7 @@ func checkRedundantAssertionTest(path string, call jdomain.JMethodCall, method j ...@@ -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 var isDuplicateAssert = false
for _, methodCall := range methodCallMap { for _, methodCall := range methodCallMap {
if len(methodCall) >= constants.DuplicatedAssertionLimitLength { if len(methodCall) >= constants.DuplicatedAssertionLimitLength {
...@@ -138,28 +138,28 @@ func checkDuplicateAssertTest(clz jdomain.JClassNode, results *[]TestBadSmell, m ...@@ -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() { if method.IsThreadSleep() {
*testType = "SleepyTest" *testType = "SleepyTest"
tbs := TestBadSmell{ tbs := TestBadSmell{
FileName: path, FileName: path,
Type: *testType, Type: *testType,
Description: "", Description: "",
Line: method.StartLine, Line: method.Position.StartLine,
} }
*results = append(*results, tbs) *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() { if mCall.IsSystemOutput() {
*testType = "RedundantPrintTest" *testType = "RedundantPrintTest"
tbs := TestBadSmell{ tbs := TestBadSmell{
FileName: path, FileName: path,
Type: *testType, Type: *testType,
Description: "", Description: "",
Line: mCall.StartLine, Line: mCall.Position.StartLine,
} }
*results = append(*results, tbs) *results = append(*results, tbs)
......
package core_domain package core_domain
import (
"github.com/phodal/coca/pkg/infrastructure/constants"
"strings"
)
type CodeCall struct { type CodeCall struct {
Package string Package string
Type string Type string
...@@ -8,3 +13,34 @@ type CodeCall struct { ...@@ -8,3 +13,34 @@ type CodeCall struct {
Parameters []CodeProperty Parameters []CodeProperty
Position CodePosition 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
}
...@@ -9,9 +9,9 @@ type CodeProperty struct { ...@@ -9,9 +9,9 @@ type CodeProperty struct {
Parameters []CodeProperty Parameters []CodeProperty
} }
func NewCodeParameter(typeName string, typeType string) CodeProperty { func NewCodeParameter(typeValue string, typeType string) CodeProperty {
return CodeProperty{ return CodeProperty{
TypeValue: typeName, TypeValue: typeValue,
TypeType: typeType, TypeType: typeType,
} }
} }
......
...@@ -12,7 +12,7 @@ type JClassNode struct { ...@@ -12,7 +12,7 @@ type JClassNode struct {
FilePath string FilePath string
Fields []core_domain.CodeField Fields []core_domain.CodeField
Methods []JMethod Methods []JMethod
MethodCalls []JMethodCall MethodCalls []core_domain.CodeCall
Extend string Extend string
Implements []string Implements []string
Annotations []core_domain.CodeAnnotation Annotations []core_domain.CodeAnnotation
......
...@@ -14,7 +14,7 @@ type JMethod struct { ...@@ -14,7 +14,7 @@ type JMethod struct {
StopLine int StopLine int
StopLinePosition int StopLinePosition int
Parameters []core_domain.CodeProperty Parameters []core_domain.CodeProperty
MethodCalls []JMethodCall MethodCalls []core_domain.CodeCall
Override bool Override bool
Annotations []core_domain.CodeAnnotation Annotations []core_domain.CodeAnnotation
IsConstructor bool IsConstructor bool
......
package jdomain 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
}
...@@ -3,7 +3,6 @@ package full ...@@ -3,7 +3,6 @@ package full
import ( import (
"github.com/phodal/coca/languages/java" "github.com/phodal/coca/languages/java"
"github.com/phodal/coca/pkg/domain/core_domain" "github.com/phodal/coca/pkg/domain/core_domain"
"github.com/phodal/coca/pkg/domain/jdomain"
"strings" "strings"
) )
...@@ -23,7 +22,7 @@ func BuildMethodParameters(parameters parser.IFormalParametersContext) []core_do ...@@ -23,7 +22,7 @@ func BuildMethodParameters(parameters parser.IFormalParametersContext) []core_do
return methodParams 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 methodName := callee
packageName := currentPkg packageName := currentPkg
...@@ -53,19 +52,21 @@ func BuildMethodCallMethods(jMethodCall *jdomain.JMethodCall, callee string, tar ...@@ -53,19 +52,21 @@ func BuildMethodCallMethods(jMethodCall *jdomain.JMethodCall, callee string, tar
jMethodCall.Class = targetType jMethodCall.Class = targetType
} }
func BuildMethodCallLocation(jMethodCall *jdomain.JMethodCall, ctx *parser.MethodCallContext, callee string) { func BuildMethodCallLocation(jMethodCall *core_domain.CodeCall, ctx *parser.MethodCallContext, callee string) {
jMethodCall.StartLine = ctx.GetStart().GetLine() jMethodCall.Position.StartLine = ctx.GetStart().GetLine()
jMethodCall.StartLinePosition = ctx.GetStart().GetColumn() jMethodCall.Position.StartLinePosition = ctx.GetStart().GetColumn()
jMethodCall.StopLine = ctx.GetStop().GetLine() jMethodCall.Position.StopLine = ctx.GetStop().GetLine()
jMethodCall.StopLinePosition = jMethodCall.StartLinePosition + len(callee) 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 { if ctx.ExpressionList() != nil {
var parameters []string var parameters []core_domain.CodeProperty
for _, expression := range ctx.ExpressionList().(*parser.ExpressionListContext).AllExpression() { for _, expression := range ctx.ExpressionList().(*parser.ExpressionListContext).AllExpression() {
expressionCtx := expression.(*parser.ExpressionContext) expressionCtx := expression.(*parser.ExpressionContext)
parameters = append(parameters, expressionCtx.GetText())
parameter := core_domain.NewCodeParameter(expressionCtx.GetText(), "")
parameters = append(parameters, parameter)
} }
jMethodCall.Parameters = parameters jMethodCall.Parameters = parameters
} }
......
...@@ -16,7 +16,7 @@ var clzs []string ...@@ -16,7 +16,7 @@ var clzs []string
var currentPkg string var currentPkg string
var currentClz string var currentClz string
var fields []core_domain.CodeField var fields []core_domain.CodeField
var methodCalls []jdomain.JMethodCall var methodCalls []core_domain.CodeCall
var currentType string var currentType string
var mapFields = make(map[string]string) var mapFields = make(map[string]string)
...@@ -226,9 +226,9 @@ func (s *JavaFullListener) EnterFieldDeclaration(ctx *parser.FieldDeclarationCon ...@@ -226,9 +226,9 @@ func (s *JavaFullListener) EnterFieldDeclaration(ctx *parser.FieldDeclarationCon
for _, declarator := range decelerators.(*parser.VariableDeclaratorsContext).AllVariableDeclarator() { for _, declarator := range decelerators.(*parser.VariableDeclaratorsContext).AllVariableDeclarator() {
var typeCtx *parser.ClassOrInterfaceTypeContext = nil var typeCtx *parser.ClassOrInterfaceTypeContext = nil
typeCtx = BuildTypeCtxByIndex(typeType, typeCtx,0) typeCtx = BuildTypeCtxByIndex(typeType, typeCtx, 0)
if typeType.GetChildCount() > 1 { if typeType.GetChildCount() > 1 {
typeCtx = BuildTypeCtxByIndex(typeType, typeCtx,1) typeCtx = BuildTypeCtxByIndex(typeType, typeCtx, 1)
} }
if typeCtx == nil { if typeCtx == nil {
...@@ -469,23 +469,26 @@ func buildCreatedCall(createdName string, ctx *parser.CreatorContext) { ...@@ -469,23 +469,26 @@ func buildCreatedCall(createdName string, ctx *parser.CreatorContext) {
method := methodMap[getMethodMapName(currentMethod)] method := methodMap[getMethodMapName(currentMethod)]
fullType, _ := WarpTargetFullType(createdName) fullType, _ := WarpTargetFullType(createdName)
jMethodCall := &jdomain.JMethodCall{ position := core_domain.CodePosition{
Package: RemoveTarget(fullType),
Type: "CreatorClass",
Class: createdName,
MethodName: "",
StartLine: ctx.GetStart().GetLine(), StartLine: ctx.GetStart().GetLine(),
StartLinePosition: ctx.GetStart().GetColumn(), StartLinePosition: ctx.GetStart().GetColumn(),
StopLine: ctx.GetStop().GetLine(), StopLine: ctx.GetStop().GetLine(),
StopLinePosition: ctx.GetStop().GetColumn(), StopLinePosition: ctx.GetStop().GetColumn(),
} }
jMethodCall := &core_domain.CodeCall{
Package: RemoveTarget(fullType),
Type: "CreatorClass",
Class: createdName,
Position: position,
}
method.MethodCalls = append(method.MethodCalls, *jMethodCall) method.MethodCalls = append(method.MethodCalls, *jMethodCall)
methodMap[getMethodMapName(currentMethod)] = method methodMap[getMethodMapName(currentMethod)] = method
} }
func (s *JavaFullListener) EnterMethodCall(ctx *parser.MethodCallContext) { func (s *JavaFullListener) EnterMethodCall(ctx *parser.MethodCallContext) {
var jMethodCall = jdomain.NewJMethodCall() var jMethodCall = core_domain.NewCodeMethodCall()
targetCtx := ctx.GetParent().GetChild(0).(antlr.ParseTree) targetCtx := ctx.GetParent().GetChild(0).(antlr.ParseTree)
var targetType = ParseTargetType(targetCtx.GetText()) var targetType = ParseTargetType(targetCtx.GetText())
...@@ -506,7 +509,7 @@ func (s *JavaFullListener) EnterMethodCall(ctx *parser.MethodCallContext) { ...@@ -506,7 +509,7 @@ func (s *JavaFullListener) EnterMethodCall(ctx *parser.MethodCallContext) {
sendResultToMethodCallMap(jMethodCall) sendResultToMethodCallMap(jMethodCall)
} }
func sendResultToMethodCallMap(jMethodCall jdomain.JMethodCall) { func sendResultToMethodCallMap(jMethodCall core_domain.CodeCall) {
methodCalls = append(methodCalls, jMethodCall) methodCalls = append(methodCalls, jMethodCall)
method := methodMap[getMethodMapName(currentMethod)] method := methodMap[getMethodMapName(currentMethod)]
...@@ -544,21 +547,21 @@ func (s *JavaFullListener) EnterExpression(ctx *parser.ExpressionContext) { ...@@ -544,21 +547,21 @@ func (s *JavaFullListener) EnterExpression(ctx *parser.ExpressionContext) {
fullType, _ := WarpTargetFullType(targetType) fullType, _ := WarpTargetFullType(targetType)
startLine := ctx.GetStart().GetLine()
startLinePosition := ctx.GetStart().GetColumn() startLinePosition := ctx.GetStart().GetColumn()
stopLine := ctx.GetStop().GetLine()
stopLinePosition := startLinePosition + len(text) position := core_domain.CodePosition{
StartLine: ctx.GetStart().GetLine(),
jMethodCall := &jdomain.JMethodCall{
Package: RemoveTarget(fullType),
Type: "lambda",
Class: targetType,
MethodName: methodName,
Parameters: nil,
StartLine: startLine,
StartLinePosition: startLinePosition, StartLinePosition: startLinePosition,
StopLine: stopLine, StopLine: ctx.GetStop().GetLine(),
StopLinePosition: stopLinePosition, StopLinePosition: startLinePosition + len(text),
}
jMethodCall := &core_domain.CodeCall{
Package: RemoveTarget(fullType),
Type: "lambda",
Class: targetType,
MethodName: methodName,
Position: position,
} }
sendResultToMethodCallMap(*jMethodCall) sendResultToMethodCallMap(*jMethodCall)
} }
...@@ -578,17 +581,20 @@ func buildExtend(extendName string) { ...@@ -578,17 +581,20 @@ func buildExtend(extendName string) {
func buildFieldCall(typeType string, ctx *parser.FieldDeclarationContext) { func buildFieldCall(typeType string, ctx *parser.FieldDeclarationContext) {
target, _ := WarpTargetFullType(typeType) target, _ := WarpTargetFullType(typeType)
if target != "" { if target != "" {
jMethodCall := &jdomain.JMethodCall{ position := core_domain.CodePosition{
Package: RemoveTarget(target),
Type: "field",
Class: typeType,
MethodName: "",
StartLine: ctx.GetStart().GetLine(), StartLine: ctx.GetStart().GetLine(),
StartLinePosition: ctx.GetStart().GetColumn(), StartLinePosition: ctx.GetStart().GetColumn(),
StopLine: ctx.GetStop().GetLine(), StopLine: ctx.GetStop().GetLine(),
StopLinePosition: ctx.GetStop().GetColumn(), StopLinePosition: ctx.GetStop().GetColumn(),
} }
jMethodCall := &core_domain.CodeCall{
Package: RemoveTarget(target),
Type: "field",
Class: typeType,
Position: position,
}
currentNode.MethodCalls = append(currentNode.MethodCalls, *jMethodCall) currentNode.MethodCalls = append(currentNode.MethodCalls, *jMethodCall)
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册