diff --git a/core/adapter/call/java_call_app_test.go b/core/adapter/call/java_call_app_test.go index 2620d721dd4f2e8af53972485418d7837c43ba2f..8ea438ee1fd5387f94eae1f822f23e0a78f8e2ae 100644 --- a/core/adapter/call/java_call_app_test.go +++ b/core/adapter/call/java_call_app_test.go @@ -94,16 +94,15 @@ func TestAnnotation(t *testing.T) { callNodes := getCallNodes(codePath) - fmt.Println(callNodes) - //methodMap := make(map[string]models.JMethod) - //for _, c := range callNodes[0].Methods { - // methodMap[c.Name] = c - //} - //g.Expect(methodMap["macOsXPositiveTest"].Name).To(Equal("macOsXPositiveTest")) - // - //for _, call := range methodMap["macOsXPositiveTest"].MethodCalls { - // fmt.Println(call.Class) - //} + methodMap := make(map[string]models.JMethod) + for _, c := range callNodes[0].Methods { + methodMap[c.Name] = c + } + g.Expect(methodMap["macOsXPositiveTest"].Name).To(Equal("macOsXPositiveTest")) + + for _, call := range methodMap["macOsXPositiveTest"].MethodCalls { + fmt.Println(call.Class) + } g.Expect(true).To(Equal(true)) } \ No newline at end of file diff --git a/core/adapter/call/java_call_listener.go b/core/adapter/call/java_call_listener.go index af6f5274d40e1214d8ac67122cb76aaccff42ccb..7977716625246bf62a7f03c0ee1d8ce9815709fa 100644 --- a/core/adapter/call/java_call_listener.go +++ b/core/adapter/call/java_call_listener.go @@ -31,8 +31,13 @@ var classQueue []string var identMap map[string]models.JIdentifier var isOverrideMethod = false -var currentNode models.JClassNode +var classNodeQueue []models.JClassNode +var currentClassForQueue models.JClassNode + +var currentNode *models.JClassNode var classNodes []models.JClassNode +var creatorNodes []models.JClassNode +var currentCreatorNode models.JClassNode var fileName = "" var hasEnterClass = false @@ -90,9 +95,22 @@ func (s *JavaCallListener) exitBody() { currentNode.Fields = fields currentNode.Type = currentType currentNode.Methods = methodsArray + currentNode.Path = fileName - classNodes = append(classNodes, currentNode) } + + if currentType == "Creator" { + currentCreatorNode.Methods = append(currentCreatorNode.Methods, currentNode.Methods...) + var methodsArray []models.JMethod + for _, value := range methodMap { + methodsArray = append(methodsArray, value) + } + + currentNode.Methods = append(currentNode.Methods, methodsArray...) + return + } + + classNodes = append(classNodes, *currentNode) currentNode = models.NewClassNode() initClass() } @@ -308,6 +326,7 @@ func buildMethodParameters(parameters parser.IFormalParametersContext, method *m } method.Parameters = methodParams + updateMethod(method) } return false } @@ -365,38 +384,45 @@ func getMethodMapName(method models.JMethod) string { func (s *JavaCallListener) EnterCreator(ctx *parser.CreatorContext) { variableName := ctx.GetParent().GetParent().GetChild(0).(antlr.ParseTree).GetText() allIdentifiers := ctx.CreatedName().(*parser.CreatedNameContext).AllIDENTIFIER() - //currentType = "Creator" for _, identifier := range allIdentifiers { createdName := identifier.GetText() localVars[variableName] = createdName - if currentMethod.Name == "" { - return + currentType = "Creator" + classNodeQueue = append(classNodeQueue, *currentNode) + buildCreatedCall(createdName, ctx) + + text := ctx.CreatedName().GetText() + creatorNode := &models.JClassNode{ + Package: currentPkg, + Class: text, + Type: "Creator", + Path: "", + Fields: nil, + Methods: nil, + MethodCalls: nil, + Extend: "", + Implements: nil, + Annotations: nil, } - buildCreatedCall(createdName, ctx) + currentCreatorNode = *creatorNode + creatorNodes = append(creatorNodes, *creatorNode) } } -//func (s *JavaCallListener) ExitCreator(ctx *parser.CreatorContext) { -// text := ctx.CreatedName().GetText() -// creatorNode := &models.JClassNode{ -// Package: currentPkg, -// Class: text, -// Type: "Creator", -// Path: "", -// Fields: nil, -// Methods: nil, -// MethodCalls: nil, -// Extend: "", -// Implements: nil, -// Annotations: nil, -// } -// -// classNodes = append(classNodes, *creatorNode) -// //currentNode = *creatorNode -//} +func (s *JavaCallListener) ExitCreator(ctx *parser.CreatorContext) { + currentType = "" + currentCreatorNode = *models.NewClassNode() + + if classNodeQueue == nil || len(classNodeQueue) <= 1 { + return + } + + classNodeQueue = classNodeQueue[0 : len(classNodeQueue)-1] + currentClassForQueue = classNodeQueue[len(classNodeQueue)-1] +} func buildCreatedCall(createdName string, ctx *parser.CreatorContext) { method := methodMap[getMethodMapName(currentMethod)] diff --git a/core/adapter/call/java_call_listener_test.go b/core/adapter/call/java_call_listener_test.go new file mode 100644 index 0000000000000000000000000000000000000000..f9898915ea9e9e4b88efd31704cfac1fac0667e4 --- /dev/null +++ b/core/adapter/call/java_call_listener_test.go @@ -0,0 +1 @@ +package call diff --git a/core/models/jclass_node.go b/core/models/jclass_node.go index 2941abe159c5455ac2279085e08093dd4a0a6a5d..b7f752be7ee2114499adc50a5e5f7bc9671f3f9f 100644 --- a/core/models/jclass_node.go +++ b/core/models/jclass_node.go @@ -18,6 +18,6 @@ type JAppField struct { Value string } -func NewClassNode() JClassNode { - return *&JClassNode{"", "", "", "", nil, nil, nil, "", nil, nil} +func NewClassNode() *JClassNode { + return &JClassNode{"", "", "", "", nil, nil, nil, "", nil, nil} } diff --git a/core/models/jmethod.go b/core/models/jmethod.go index a0b9c9e9e5c0d23d0262e6fee4ad73f17d0ba756..ff9e7a3037e2ef2acaf2a635bcc0a864951aea68 100644 --- a/core/models/jmethod.go +++ b/core/models/jmethod.go @@ -14,6 +14,7 @@ type JMethod struct { IsConstructor bool IsReturnNull bool Modifiers []string + Creators []JClassNode } type Annotation struct {