From ef6358cc655ad9a647321aa9d28b461ad5dd6f93 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Thu, 16 Jan 2020 21:51:33 +0800 Subject: [PATCH] refactor: keep identifier & ds: --- cmd/analysis.go | 2 +- cmd/deps.go | 2 +- cmd/rcall.go | 2 +- cmd/tbs.go | 2 +- cocatest/testhelper/generate.go | 2 +- .../cocafile/file_analysis_helper_test.go | 2 +- .../analysis/java_full_app_test.go | 4 +- .../analysis/java_identifier_app_test.go | 26 +++++------ pkg/application/evaluate/analyser.go | 4 +- .../evaluate/evaluator/null_exception.go | 4 +- .../refactor/rename/rename_method_test.go | 2 +- pkg/application/tbs/tbs_app_test.go | 2 +- pkg/domain/core_domain/code_data_struct.go | 43 +++++++++++++++++++ pkg/domain/core_domain/jidentifier.go | 43 ------------------- .../ast/api/java_api_listener.go | 2 +- .../identifier/java_identifier_listener.go | 20 ++++----- 16 files changed, 81 insertions(+), 81 deletions(-) diff --git a/cmd/analysis.go b/cmd/analysis.go index f667bd1..d509a0a 100644 --- a/cmd/analysis.go +++ b/cmd/analysis.go @@ -32,7 +32,7 @@ var analysisCmd = &cobra.Command{ var classes []string = nil for _, node := range iNodes { - classes = append(classes, node.Package+"."+node.ClassName) + classes = append(classes, node.Package+"."+node.NodeName) } callApp := analysis.NewJavaFullApp() diff --git a/cmd/deps.go b/cmd/deps.go index abbe09e..aa9cd70 100644 --- a/cmd/deps.go +++ b/cmd/deps.go @@ -40,7 +40,7 @@ var depsCmd = &cobra.Command{ var classes []string = nil for _, node := range iNodes { - classes = append(classes, node.Package+"."+node.ClassName) + classes = append(classes, node.Package+"."+node.NodeName) } callApp := analysis.NewJavaFullApp() diff --git a/cmd/rcall.go b/cmd/rcall.go index 3ee86d2..5a55f55 100644 --- a/cmd/rcall.go +++ b/cmd/rcall.go @@ -31,7 +31,7 @@ var reverseCmd = &cobra.Command{ remove := reverseConfig.RemovePackage if className == "" { - log.Fatal("lost ClassName") + log.Fatal("lost NodeName") } analyser := rcall.NewRCallGraph() diff --git a/cmd/tbs.go b/cmd/tbs.go index b0e2bcd..a96abb2 100644 --- a/cmd/tbs.go +++ b/cmd/tbs.go @@ -33,7 +33,7 @@ var tbsCmd = &cobra.Command{ var classes []string = nil for _, node := range identifiers { - classes = append(classes, node.Package+"."+node.ClassName) + classes = append(classes, node.Package+"."+node.NodeName) } analysisApp := analysis.NewJavaFullApp() diff --git a/cocatest/testhelper/generate.go b/cocatest/testhelper/generate.go index 63b883b..d9e8302 100644 --- a/cocatest/testhelper/generate.go +++ b/cocatest/testhelper/generate.go @@ -13,7 +13,7 @@ func BuildAnalysisDeps(codePath string) ([]core_domain.CodeDataStruct, map[strin identifiers := identifierApp.AnalysisPath(codePath) var classes []string = nil for _, node := range identifiers { - classes = append(classes, node.Package+"."+node.ClassName) + classes = append(classes, node.Package+"."+node.NodeName) } callApp := analysis.NewJavaFullApp() diff --git a/pkg/adapter/cocafile/file_analysis_helper_test.go b/pkg/adapter/cocafile/file_analysis_helper_test.go index 2a2b2ea..1d4380a 100644 --- a/pkg/adapter/cocafile/file_analysis_helper_test.go +++ b/pkg/adapter/cocafile/file_analysis_helper_test.go @@ -29,5 +29,5 @@ public class DataClass { antlr.NewParseTreeWalker().Walk(listener, context) identifiers := listener.GetNodes() - g.Expect(identifiers[0].ClassName).To(Equal("DataClass")) + g.Expect(identifiers[0].NodeName).To(Equal("DataClass")) } diff --git a/pkg/application/analysis/java_full_app_test.go b/pkg/application/analysis/java_full_app_test.go index 256568e..693b784 100644 --- a/pkg/application/analysis/java_full_app_test.go +++ b/pkg/application/analysis/java_full_app_test.go @@ -17,7 +17,7 @@ func TestJavaCallApp_AnalysisPath(t *testing.T) { iNodes := identifierApp.AnalysisPath(codePath) var classes []string = nil for _, node := range iNodes { - classes = append(classes, node.Package+"."+node.ClassName) + classes = append(classes, node.Package+"."+node.NodeName) } callApp := NewJavaFullApp() @@ -41,7 +41,7 @@ func getCallNodes(codePath string) []core_domain.CodeDataStruct { iNodes := identifierApp.AnalysisPath(codePath) var classes []string = nil for _, node := range iNodes { - classes = append(classes, node.Package+"."+node.ClassName) + classes = append(classes, node.Package+"."+node.NodeName) } callApp := NewJavaFullApp() diff --git a/pkg/application/analysis/java_identifier_app_test.go b/pkg/application/analysis/java_identifier_app_test.go index e1cc510..6ea994c 100644 --- a/pkg/application/analysis/java_identifier_app_test.go +++ b/pkg/application/analysis/java_identifier_app_test.go @@ -13,9 +13,9 @@ func TestJavaIdentifierApp_AnalysisPath(t *testing.T) { identifiers := identApp.AnalysisPath(filepath.FromSlash("../../../_fixtures/call")) g.Expect(len(identifiers)).To(Equal(1)) - g.Expect(identifiers[0].ClassName).To(Equal("BookController")) - g.Expect(identifiers[0].Methods[0].Name).To(Equal("BookController")) - g.Expect(identifiers[0].Methods[1].Name).To(Equal("createBook")) + g.Expect(identifiers[0].NodeName).To(Equal("BookController")) + g.Expect(identifiers[0].Functions[0].Name).To(Equal("BookController")) + g.Expect(identifiers[0].Functions[1].Name).To(Equal("createBook")) g.Expect(identifiers[0].Annotations[0].Name).To(Equal("RestController")) } @@ -27,8 +27,8 @@ func TestPolymorphism_Method(t *testing.T) { identifiers := identApp.AnalysisPath(filepath.FromSlash("../../../_fixtures/suggest/polymorphism")) g.Expect(len(identifiers)).To(Equal(1)) - g.Expect(identifiers[0].ClassName).To(Equal("Overload")) - g.Expect(len(identifiers[0].Methods)).To(Equal(3)) + g.Expect(identifiers[0].NodeName).To(Equal("Overload")) + g.Expect(len(identifiers[0].Functions)).To(Equal(3)) } func TestPolymorphism_Constructor(t *testing.T) { @@ -38,8 +38,8 @@ func TestPolymorphism_Constructor(t *testing.T) { identifiers := identApp.AnalysisPath(filepath.FromSlash("../../../_fixtures/suggest/factory")) g.Expect(len(identifiers)).To(Equal(2)) - g.Expect(identifiers[0].ClassName).To(Equal("Insect")) - g.Expect(identifiers[1].ClassName).To(Equal("Bee")) + g.Expect(identifiers[0].NodeName).To(Equal("Insect")) + g.Expect(identifiers[1].NodeName).To(Equal("Bee")) } func TestAddReturnNull(t *testing.T) { @@ -49,14 +49,14 @@ func TestAddReturnNull(t *testing.T) { identifiers := identApp.AnalysisPath(filepath.FromSlash("../../../_fixtures/evaluate/null")) var returNullCount = 0 - for _, method := range identifiers[0].Methods { + for _, method := range identifiers[0].Functions { if method.IsReturnNull { returNullCount++ } } - g.Expect(identifiers[0].Methods[1].IsReturnNull).To(Equal(true)) - g.Expect(identifiers[0].Methods[2].IsReturnNull).To(Equal(true)) + g.Expect(identifiers[0].Functions[1].IsReturnNull).To(Equal(true)) + g.Expect(identifiers[0].Functions[2].IsReturnNull).To(Equal(true)) g.Expect(returNullCount).To(Equal(2)) } @@ -66,8 +66,8 @@ func TestStaticMethod(t *testing.T) { identApp := NewJavaIdentifierApp() identifiers := identApp.AnalysisPath(filepath.FromSlash("../../../_fixtures/suggest/static")) - g.Expect(identifiers[0].Methods[0].Modifiers[0]).To(Equal("public")) - g.Expect(identifiers[0].Methods[0].Modifiers[1]).To(Equal("static")) + g.Expect(identifiers[0].Functions[0].Modifiers[0]).To(Equal("public")) + g.Expect(identifiers[0].Functions[0].Modifiers[1]).To(Equal("static")) } func TestModifierLength(t *testing.T) { @@ -76,5 +76,5 @@ func TestModifierLength(t *testing.T) { identApp := NewJavaIdentifierApp() identifiers := identApp.AnalysisPath(filepath.FromSlash("../../../_fixtures/bs/ComplexIf.java")) - g.Expect(len(identifiers[0].Methods[0].Modifiers)).To(Equal(1)) + g.Expect(len(identifiers[0].Functions[0].Modifiers)).To(Equal(1)) } diff --git a/pkg/application/evaluate/analyser.go b/pkg/application/evaluate/analyser.go index ff87927..0824c9e 100644 --- a/pkg/application/evaluate/analyser.go +++ b/pkg/application/evaluate/analyser.go @@ -54,9 +54,9 @@ func SummaryMethodIdentifier(identifiers []core_domain.JIdentifier, result *eval for _, ident := range identifiers { result.Summary.ClassCount++ - methodCountArray = append(methodCountArray, float64(len(ident.Methods))) + methodCountArray = append(methodCountArray, float64(len(ident.Functions))) - for _, method := range ident.Methods { + for _, method := range ident.Functions { result.Summary.MethodCount++ if method.IsStatic() { diff --git a/pkg/application/evaluate/evaluator/null_exception.go b/pkg/application/evaluate/evaluator/null_exception.go index 707f398..2f26520 100644 --- a/pkg/application/evaluate/evaluator/null_exception.go +++ b/pkg/application/evaluate/evaluator/null_exception.go @@ -15,7 +15,7 @@ func (n NullPointException) EvaluateList(evaluateModel *EvaluateModel, nodes []c var nullableList []string = nil var nullableMap = make(map[string]string) for _, ident := range identifiers { - for _, method := range ident.Methods { + for _, method := range ident.Functions { methodName := buildMethodPath(ident, method) if method.IsReturnNull { nullableMap[methodName] = methodName @@ -37,5 +37,5 @@ func (n NullPointException) EvaluateList(evaluateModel *EvaluateModel, nodes []c } func buildMethodPath(ident core_domain.JIdentifier, method core_domain.CodeFunction) string { - return ident.Package + "." + ident.ClassName + "." + method.Name + return ident.Package + "." + ident.NodeName + "." + method.Name } diff --git a/pkg/application/refactor/rename/rename_method_test.go b/pkg/application/refactor/rename/rename_method_test.go index eca644b..8411dc7 100644 --- a/pkg/application/refactor/rename/rename_method_test.go +++ b/pkg/application/refactor/rename/rename_method_test.go @@ -21,7 +21,7 @@ func TestRenameMethodApp(t *testing.T) { identifiers := identifierApp.AnalysisPath(codePath) var classes []string = nil for _, node := range identifiers { - classes = append(classes, node.Package+"."+node.ClassName) + classes = append(classes, node.Package+"."+node.NodeName) } callApp := analysis.NewJavaFullApp() diff --git a/pkg/application/tbs/tbs_app_test.go b/pkg/application/tbs/tbs_app_test.go index d88d2d7..9256eb4 100644 --- a/pkg/application/tbs/tbs_app_test.go +++ b/pkg/application/tbs/tbs_app_test.go @@ -147,7 +147,7 @@ func BuildTestAnalysisResultsByPath(codePath string) (map[string]core_domain.JId var classes []string = nil for _, node := range identifiers { - classes = append(classes, node.Package+"."+node.ClassName) + classes = append(classes, node.Package+"."+node.NodeName) } analysisApp := analysis.NewJavaFullApp() diff --git a/pkg/domain/core_domain/code_data_struct.go b/pkg/domain/core_domain/code_data_struct.go index f2c563b..cf2d0c9 100644 --- a/pkg/domain/core_domain/code_data_struct.go +++ b/pkg/domain/core_domain/code_data_struct.go @@ -21,6 +21,16 @@ type CodeDataStruct struct { Extension interface{} } +type JIdentifier struct { + NodeName string + Package string + Type string + Extend string + Implements []string + Functions []CodeFunction + Annotations []CodeAnnotation +} + func NewDataStruct() *CodeDataStruct { return &CodeDataStruct{} } @@ -61,3 +71,36 @@ func BuildCallMethodMap(deps []CodeDataStruct) map[string]CodeFunction { } return callMethodMap } + +func NewJIdentifier() *JIdentifier { + return &JIdentifier{} +} + +func (identifier *JIdentifier) GetClassFullName() string { + return identifier.Package + "." + identifier.NodeName +} + +func BuildIdentifierMap(identifiers []JIdentifier) map[string]JIdentifier { + var identifiersMap = make(map[string]JIdentifier) + + for _, ident := range identifiers { + identifiersMap[ident.Package+"."+ident.NodeName] = ident + } + return identifiersMap +} + +func BuildDIMap(identifiers []JIdentifier, identifierMap map[string]JIdentifier) map[string]string { + var diMap = make(map[string]string) + for _, clz := range identifiers { + if len(clz.Annotations) > 0 { + for _, annotation := range clz.Annotations { + if (annotation.IsComponentOrRepository()) && len(clz.Implements) > 0 { + superClz := identifierMap[clz.Implements[0]] + diMap[superClz.GetClassFullName()] = superClz.GetClassFullName() + } + } + } + } + + return diMap +} diff --git a/pkg/domain/core_domain/jidentifier.go b/pkg/domain/core_domain/jidentifier.go index 440d0e2..1b0934a 100644 --- a/pkg/domain/core_domain/jidentifier.go +++ b/pkg/domain/core_domain/jidentifier.go @@ -1,45 +1,2 @@ package core_domain -type JIdentifier struct { - Package string - ClassName string - ClassType string - ExtendsName string - Extends []string - Implements []string - Methods []CodeFunction - Annotations []CodeAnnotation -} - -func NewJIdentifier() *JIdentifier { - return &JIdentifier{} -} - -func (identifier *JIdentifier) GetClassFullName() string { - return identifier.Package + "." + identifier.ClassName -} - -func BuildIdentifierMap(identifiers []JIdentifier) map[string]JIdentifier { - var identifiersMap = make(map[string]JIdentifier) - - for _, ident := range identifiers { - identifiersMap[ident.Package+"."+ident.ClassName] = ident - } - return identifiersMap -} - -func BuildDIMap(identifiers []JIdentifier, identifierMap map[string]JIdentifier) map[string]string { - var diMap = make(map[string]string) - for _, clz := range identifiers { - if len(clz.Annotations) > 0 { - for _, annotation := range clz.Annotations { - if (annotation.IsComponentOrRepository()) && len(clz.Implements) > 0 { - superClz := identifierMap[clz.Implements[0]] - diMap[superClz.GetClassFullName()] = superClz.GetClassFullName() - } - } - } - } - - return diMap -} diff --git a/pkg/infrastructure/ast/api/java_api_listener.go b/pkg/infrastructure/ast/api/java_api_listener.go index a5eca37..f5b7312 100644 --- a/pkg/infrastructure/ast/api/java_api_listener.go +++ b/pkg/infrastructure/ast/api/java_api_listener.go @@ -220,7 +220,7 @@ func buildApiForInterfaceAnnotation(methodName string) bool { } if _, ok := identMap[superClz]; ok { - for _, method := range identMap[superClz].Methods { + for _, method := range identMap[superClz].Functions { if method.Name == methodName { for _, annotation := range method.Annotations { if annotation.Name == "ServiceMethod" { diff --git a/pkg/infrastructure/ast/identifier/java_identifier_listener.go b/pkg/infrastructure/ast/identifier/java_identifier_listener.go index 693f467..2f1ddb8 100644 --- a/pkg/infrastructure/ast/identifier/java_identifier_listener.go +++ b/pkg/infrastructure/ast/identifier/java_identifier_listener.go @@ -39,13 +39,13 @@ func (s *JavaIdentifierListener) EnterPackageDeclaration(ctx *parser.PackageDecl func (s *JavaIdentifierListener) EnterClassDeclaration(ctx *parser.ClassDeclarationContext) { hasEnterClass = true - currentNode.ClassType = "NodeName" + currentNode.Type = "NodeName" if ctx.IDENTIFIER() != nil { - currentNode.ClassName = ctx.IDENTIFIER().GetText() + currentNode.NodeName = ctx.IDENTIFIER().GetText() } if ctx.EXTENDS() != nil { - currentNode.ExtendsName = ctx.TypeType().GetText() + currentNode.Extend = ctx.TypeType().GetText() } if ctx.IMPLEMENTS() != nil { @@ -65,7 +65,7 @@ func (s *JavaIdentifierListener) EnterClassDeclaration(ctx *parser.ClassDeclarat func (s *JavaIdentifierListener) ExitClassBody(ctx *parser.ClassBodyContext) { hasEnterClass = false - if currentNode.ClassName != "" { + if currentNode.NodeName != "" { nodes = append(nodes, *currentNode) } currentNode = core_domain.NewJIdentifier() @@ -73,7 +73,7 @@ func (s *JavaIdentifierListener) ExitClassBody(ctx *parser.ClassBodyContext) { func (s *JavaIdentifierListener) ExitInterfaceDeclaration(ctx *parser.InterfaceDeclarationContext) { hasEnterClass = false - if currentNode.ClassName != "" { + if currentNode.NodeName != "" { nodes = append(nodes, *currentNode) } currentNode = core_domain.NewJIdentifier() @@ -98,7 +98,7 @@ func (s *JavaIdentifierListener) EnterConstructorDeclaration(ctx *parser.Constru } func (s *JavaIdentifierListener) ExitConstructorDeclaration(ctx *parser.ConstructorDeclarationContext) { - currentNode.Methods = append(currentNode.Methods, currentMethod) + currentNode.Functions = append(currentNode.Functions, currentMethod) } func (s *JavaIdentifierListener) EnterInterfaceBodyDeclaration(ctx *parser.InterfaceBodyDeclarationContext) { @@ -135,7 +135,7 @@ func (s *JavaIdentifierListener) EnterInterfaceMethodDeclaration(ctx *parser.Int } func (s *JavaIdentifierListener) ExitInterfaceMethodDeclaration(ctx *parser.InterfaceMethodDeclarationContext) { - currentNode.Methods = append(currentNode.Methods, currentMethod) + currentNode.Functions = append(currentNode.Functions, currentMethod) currentMethod = core_domain.NewJMethod() } @@ -184,7 +184,7 @@ func (s *JavaIdentifierListener) EnterMethodDeclaration(ctx *parser.MethodDeclar } func (s *JavaIdentifierListener) ExitMethodDeclaration(ctx *parser.MethodDeclarationContext) { - currentNode.Methods = append(currentNode.Methods, currentMethod) + currentNode.Functions = append(currentNode.Functions, currentMethod) currentMethod = core_domain.NewJMethod() } @@ -202,8 +202,8 @@ func (s *JavaIdentifierListener) EnterAnnotation(ctx *parser.AnnotationContext) func (s *JavaIdentifierListener) EnterInterfaceDeclaration(ctx *parser.InterfaceDeclarationContext) { hasEnterClass = true - currentNode.ClassType = "Interface" - currentNode.ClassName = ctx.IDENTIFIER().GetText() + currentNode.Type = "Interface" + currentNode.NodeName = ctx.IDENTIFIER().GetText() } func (s *JavaIdentifierListener) EnterExpression(ctx *parser.ExpressionContext) { -- GitLab