feat: make inner class to inner class node

上级 e3594aeb
package com.phodal.coca.abug;
public class Outer { public class Outer {
final int z=10; final int z=10;
class Inner extends HasStatic { class Inner extends HasStatic {
static final int x = 3; static final int x = 3;
static int y = 4; static int y = 4;
public static void pr() {
}
} }
public static void main(String[] args) { public static void main(String[] args) {
......
...@@ -77,6 +77,7 @@ func TestInterface(t *testing.T) { ...@@ -77,6 +77,7 @@ func TestInterface(t *testing.T) {
callNodes := getCallNodes(codePath) callNodes := getCallNodes(codePath)
methodMap := make(map[string]domain.JMethod) methodMap := make(map[string]domain.JMethod)
for _, c := range callNodes[0].Methods { for _, c := range callNodes[0].Methods {
methodMap[c.Name] = c methodMap[c.Name] = c
} }
...@@ -238,5 +239,7 @@ func Test_InnerClass(t *testing.T) { ...@@ -238,5 +239,7 @@ func Test_InnerClass(t *testing.T) {
callNodes := getCallNodes(codePath) callNodes := getCallNodes(codePath)
g.Expect(len(callNodes)).To(Equal(1))
g.Expect(callNodes[0].Class).To(Equal("Outer")) g.Expect(callNodes[0].Class).To(Equal("Outer"))
g.Expect(callNodes[0].InnerClass[0].Class).To(Equal("Inner"))
} }
...@@ -13,6 +13,7 @@ type JClassNode struct { ...@@ -13,6 +13,7 @@ type JClassNode struct {
Extend string Extend string
Implements []string Implements []string
Annotations []Annotation Annotations []Annotation
InnerClass []JClassNode
} }
type JAppField struct { type JAppField struct {
...@@ -21,7 +22,7 @@ type JAppField struct { ...@@ -21,7 +22,7 @@ type JAppField struct {
} }
func NewClassNode() *JClassNode { func NewClassNode() *JClassNode {
return &JClassNode{"", "", "", "", nil, nil, nil, "", nil, nil} return &JClassNode{"", "", "", "", nil, nil, nil, "", nil, nil, nil}
} }
func (j *JClassNode) IsUtilClass() bool { func (j *JClassNode) IsUtilClass() bool {
......
...@@ -88,7 +88,6 @@ func (s *JavaFullListener) ExitInterfaceBody(ctx *parser.InterfaceBodyContext) { ...@@ -88,7 +88,6 @@ func (s *JavaFullListener) ExitInterfaceBody(ctx *parser.InterfaceBodyContext) {
func (s *JavaFullListener) exitBody() { func (s *JavaFullListener) exitBody() {
if currentNode.Class != "" { if currentNode.Class != "" {
currentNode.Fields = fields currentNode.Fields = fields
currentNode.Type = currentType
currentNode.Path = fileName currentNode.Path = fileName
currentNode.SetMethodFromMap(methodMap) currentNode.SetMethodFromMap(methodMap)
} }
...@@ -98,11 +97,29 @@ func (s *JavaFullListener) exitBody() { ...@@ -98,11 +97,29 @@ func (s *JavaFullListener) exitBody() {
return return
} }
if currentNode.Class != "" { if currentNode.Class == "" {
currentNode = domain.NewClassNode()
initClass()
return
}
if currentNode.Type == "InnerClass" && len(classNodeQueue) >= 1 {
classNodeQueue[0].InnerClass = append(currentNode.InnerClass, *currentNode)
} else {
classNodes = append(classNodes, *currentNode) classNodes = append(classNodes, *currentNode)
} }
currentNode = domain.NewClassNode() if len(classNodeQueue) >= 1 {
if len(classNodeQueue) == 1 {
currentNode = &classNodeQueue[0]
} else {
classNodeQueue = classNodeQueue[0 : len(classNodeQueue)-1]
currentNode = &classNodeQueue[len(classNodeQueue)-1]
}
} else {
currentNode = domain.NewClassNode()
}
initClass() initClass()
} }
...@@ -119,12 +136,14 @@ func (s *JavaFullListener) EnterImportDeclaration(ctx *parser.ImportDeclarationC ...@@ -119,12 +136,14 @@ func (s *JavaFullListener) EnterImportDeclaration(ctx *parser.ImportDeclarationC
func (s *JavaFullListener) EnterClassDeclaration(ctx *parser.ClassDeclarationContext) { func (s *JavaFullListener) EnterClassDeclaration(ctx *parser.ClassDeclarationContext) {
// TODO: support inner class // TODO: support inner class
if currentNode.Class != "" { if currentNode.Class != "" {
return classNodeQueue = append(classNodeQueue, *currentNode)
currentType = "InnerClass"
} else {
currentType = "Class"
} }
hasEnterClass = true hasEnterClass = true
currentClzExtend = "" currentClzExtend = ""
currentType = "Class"
if ctx.IDENTIFIER() != nil { if ctx.IDENTIFIER() != nil {
currentClz = ctx.IDENTIFIER().GetText() currentClz = ctx.IDENTIFIER().GetText()
currentNode.Class = currentClz currentNode.Class = currentClz
...@@ -143,6 +162,7 @@ func (s *JavaFullListener) EnterClassDeclaration(ctx *parser.ClassDeclarationCon ...@@ -143,6 +162,7 @@ func (s *JavaFullListener) EnterClassDeclaration(ctx *parser.ClassDeclarationCon
} }
} }
currentNode.Type = currentType
// TODO: 支持依赖注入 // TODO: 支持依赖注入
} }
...@@ -157,6 +177,8 @@ func (s *JavaFullListener) EnterInterfaceDeclaration(ctx *parser.InterfaceDeclar ...@@ -157,6 +177,8 @@ func (s *JavaFullListener) EnterInterfaceDeclaration(ctx *parser.InterfaceDeclar
buildExtend(typ.GetText()) buildExtend(typ.GetText())
} }
} }
currentNode.Type = currentType
} }
func (s *JavaFullListener) EnterInterfaceBodyDeclaration(ctx *parser.InterfaceBodyDeclarationContext) { func (s *JavaFullListener) EnterInterfaceBodyDeclaration(ctx *parser.InterfaceBodyDeclarationContext) {
...@@ -399,7 +421,6 @@ func (s *JavaFullListener) EnterCreator(ctx *parser.CreatorContext) { ...@@ -399,7 +421,6 @@ func (s *JavaFullListener) EnterCreator(ctx *parser.CreatorContext) {
createdName := identifier.GetText() createdName := identifier.GetText()
localVars[variableName] = createdName localVars[variableName] = createdName
classNodeQueue = append(classNodeQueue, *currentNode)
buildCreatedCall(createdName, ctx) buildCreatedCall(createdName, ctx)
if currentMethod.Name == "" { if currentMethod.Name == "" {
...@@ -414,6 +435,8 @@ func (s *JavaFullListener) EnterCreator(ctx *parser.CreatorContext) { ...@@ -414,6 +435,8 @@ func (s *JavaFullListener) EnterCreator(ctx *parser.CreatorContext) {
return return
} }
//classNodeQueue = append(classNodeQueue, *currentNode)
currentType = "CreatorClass" currentType = "CreatorClass"
text := ctx.CreatedName().GetText() text := ctx.CreatedName().GetText()
creatorNode := &domain.JClassNode{ creatorNode := &domain.JClassNode{
...@@ -441,7 +464,9 @@ func (s *JavaFullListener) ExitCreator(ctx *parser.CreatorContext) { ...@@ -441,7 +464,9 @@ func (s *JavaFullListener) ExitCreator(ctx *parser.CreatorContext) {
methodMap[getMethodMapName(currentMethod)] = method methodMap[getMethodMapName(currentMethod)] = method
} }
currentType = "" if currentType == "CreatorClass" {
currentType = ""
}
currentCreatorNode = *domain.NewClassNode() currentCreatorNode = *domain.NewClassNode()
if classNodeQueue == nil || len(classNodeQueue) < 1 { if classNodeQueue == nil || len(classNodeQueue) < 1 {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册