diff --git a/cmd/rcall.go b/cmd/rcall.go index ab3a87c7c3d0ea52f9a78ad931d85117afec9d1c..edc8e4db7fb007ae5ab4d10926e804407d43fd2f 100644 --- a/cmd/rcall.go +++ b/cmd/rcall.go @@ -43,17 +43,22 @@ var reverseCmd = &cobra.Command{ _ = json.Unmarshal(file, &parsedDeps) fmt.Println("start rcall class :", className) - content := analyser.Analysis(className, *&parsedDeps) + content := analyser.Analysis(className, *&parsedDeps, WriteCallMap) if remove != "" { content = strings.ReplaceAll(content, remove, "") } cmd_util.WriteToCocaFile("rcall.dot", content) - cmd_util.ConvertToSvg("call") + cmd_util.ConvertToSvg("rcall") }, } +func WriteCallMap(rcallMap map[string][]string) { + mapJson, _ := json.MarshalIndent(rcallMap, "", "\t") + cmd_util.WriteToCocaFile("rcallmap.json", string(mapJson)) +} + func init() { rootCmd.AddCommand(reverseCmd) diff --git a/core/context/rcall/rcall_graph.go b/core/context/rcall/rcall_graph.go index b839e6efde61266265d3b817be40ef818cf892d9..316d7842405acd5046e1a0a3a5379ab8658a08b0 100644 --- a/core/context/rcall/rcall_graph.go +++ b/core/context/rcall/rcall_graph.go @@ -1,8 +1,6 @@ package rcall import ( - "encoding/json" - "github.com/phodal/coca/cmd/cmd_util" "github.com/phodal/coca/core/context/call" "github.com/phodal/coca/core/domain" ) @@ -14,12 +12,11 @@ func NewRCallGraph() RCallGraph { return *&RCallGraph{} } -func (c RCallGraph) Analysis(funcName string, clzs []domain.JClassNode) string { +func (c RCallGraph) Analysis(funcName string, clzs []domain.JClassNode, writeCallback func(rcallMap map[string][]string)) string { var projectMethodMap = BuildProjectMethodMap(clzs) rcallMap := BuildRCallMethodMap(clzs, projectMethodMap) - mapJson, _ := json.MarshalIndent(rcallMap, "", "\t") - cmd_util.WriteToCocaFile("rcallmap.json", string(mapJson)) + writeCallback(rcallMap) chain := c.buildRCallChain(funcName, rcallMap) @@ -45,9 +42,9 @@ func BuildRCallMethodMap(parserDeps []domain.JClassNode, projectMaps map[string] for _, clz := range parserDeps { for _, method := range clz.Methods { var caller = method.BuildFullMethodName(clz) - for _, call := range method.MethodCalls { - if call.Class != "" { - callee := call.BuildFullMethodName() + for _, jMethodCall := range method.MethodCalls { + if jMethodCall.Class != "" { + callee := jMethodCall.BuildFullMethodName() if projectMaps[callee] < 1 { continue } diff --git a/core/context/rcall/rcall_graph_test.go b/core/context/rcall/rcall_graph_test.go index a06b8eed340c5c27c7fced3acd26926e04122d49..f8949c412132b13e20677b83dd00e26a7a8ea311 100644 --- a/core/context/rcall/rcall_graph_test.go +++ b/core/context/rcall/rcall_graph_test.go @@ -22,7 +22,7 @@ func TestRCallGraph_Analysis(t *testing.T) { _ = json.Unmarshal(file, &parsedDeps) - content := analyser.Analysis("com.phodal.pholedge.book.BookFactory.create", *&parsedDeps) + content := analyser.Analysis("com.phodal.pholedge.book.BookFactory.create", *&parsedDeps, nil) g.Expect(content).To(Equal(`digraph G { rankdir = LR;