Skip to content

Commit

Permalink
feat: add call to api
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 29, 2019
1 parent 0cf72f9 commit 9dec07f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 24 deletions.
26 changes: 26 additions & 0 deletions core/adapter/call/java_call_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package call

import (
"github.com/antlr/antlr4/runtime/Go/antlr"
"github.com/phodal/coca/core/adapter/common_listener"
"github.com/phodal/coca/core/models"
"github.com/phodal/coca/languages/java"
"reflect"
Expand Down Expand Up @@ -33,6 +34,7 @@ var isOverrideMethod = false
var currentNode models.JClassNode
var classNodes []models.JClassNode
var fileName = ""
var hasEnterClass = false

func NewJavaCallListener(nodes map[string]models.JIdentifier, file string) *JavaCallListener {
identMap = nodes
Expand Down Expand Up @@ -69,10 +71,12 @@ func (s *JavaCallListener) getNodeInfo() []models.JClassNode {
}

func (s *JavaCallListener) ExitClassBody(ctx *parser.ClassBodyContext) {
hasEnterClass = false
s.exitBody()
}

func (s *JavaCallListener) ExitInterfaceBody(ctx *parser.InterfaceBodyContext) {
hasEnterClass = false
s.exitBody()
}

Expand Down Expand Up @@ -104,6 +108,7 @@ func (s *JavaCallListener) EnterImportDeclaration(ctx *parser.ImportDeclarationC
}

func (s *JavaCallListener) EnterClassDeclaration(ctx *parser.ClassDeclarationContext) {
hasEnterClass = true
currentClzExtend = ""
currentType = "Class"
if ctx.IDENTIFIER() != nil {
Expand All @@ -128,6 +133,7 @@ func (s *JavaCallListener) EnterClassDeclaration(ctx *parser.ClassDeclarationCon
}

func (s *JavaCallListener) EnterInterfaceDeclaration(ctx *parser.InterfaceDeclarationContext) {
hasEnterClass = true
currentType = "Interface"
currentNode.Class = ctx.IDENTIFIER().GetText()

Expand All @@ -139,6 +145,18 @@ func (s *JavaCallListener) EnterInterfaceDeclaration(ctx *parser.InterfaceDeclar
}
}


func (s *JavaCallListener) EnterInterfaceBodyDeclaration(ctx *parser.InterfaceBodyDeclarationContext) {
hasEnterClass = true
for _, modifier := range ctx.AllModifier() {
modifier := modifier.(*parser.ModifierContext).GetChild(0)
if reflect.TypeOf(modifier.GetChild(0)).String() == "*parser.AnnotationContext" {
annotationContext := modifier.GetChild(0).(*parser.AnnotationContext)
common_listener.BuildAnnotation(annotationContext)
}
}
}

func (s *JavaCallListener) EnterInterfaceMethodDeclaration(ctx *parser.InterfaceMethodDeclarationContext) {
startLine := ctx.GetStart().GetLine()
startLinePosition := ctx.IDENTIFIER().GetSymbol().GetColumn()
Expand Down Expand Up @@ -206,6 +224,14 @@ func (s *JavaCallListener) EnterAnnotation(ctx *parser.AnnotationContext) {
} else {
isOverrideMethod = false
}

if hasEnterClass {
annotation := common_listener.BuildAnnotation(ctx)
currentMethod.Annotations = append(currentMethod.Annotations, annotation)
} else {
annotation := common_listener.BuildAnnotation(ctx)
currentNode.Annotations = append(currentNode.Annotations, annotation)
}
}

func (s *JavaCallListener) EnterConstructorDeclaration(ctx *parser.ConstructorDeclarationContext) {
Expand Down
26 changes: 26 additions & 0 deletions core/adapter/common_listener/common_listener.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package common_listener

import (
"github.com/phodal/coca/core/models"
"github.com/phodal/coca/languages/java"
)

func BuildAnnotation(ctx *parser.AnnotationContext) models.Annotation {
annotationName := ctx.QualifiedName().GetText()
annotation := models.NewAnnotation()
annotation.QualifiedName = annotationName
if ctx.ElementValuePairs() != nil {
pairs := ctx.ElementValuePairs().(*parser.ElementValuePairsContext).AllElementValuePair()
for _, pair := range pairs {
pairCtx := pair.(*parser.ElementValuePairContext)
pairCtx.ElementValue()
annotation.ValuePairs = append(annotation.ValuePairs, *&models.AnnotationKeyValue{
Key: pairCtx.IDENTIFIER().GetText(),
Value: pairCtx.ElementValue().GetText(),
})
}
}

return annotation
}

27 changes: 4 additions & 23 deletions core/adapter/identifier/java_identifier_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package identifier

import (
"github.com/antlr/antlr4/runtime/Go/antlr"
"github.com/phodal/coca/core/adapter/common_listener"
"github.com/phodal/coca/core/models"
"github.com/phodal/coca/languages/java"
"reflect"
Expand Down Expand Up @@ -107,13 +108,12 @@ func (s *JavaIdentifierListener) EnterInterfaceBodyDeclaration(ctx *parser.Inter
modifier := modifier.(*parser.ModifierContext).GetChild(0)
if reflect.TypeOf(modifier.GetChild(0)).String() == "*parser.AnnotationContext" {
annotationContext := modifier.GetChild(0).(*parser.AnnotationContext)
buildAnnotation(annotationContext)
common_listener.BuildAnnotation(annotationContext)
}
}
}

func (s *JavaIdentifierListener) EnterInterfaceMethodDeclaration(ctx *parser.InterfaceMethodDeclarationContext) {

startLine := ctx.GetStart().GetLine()
startLinePosition := ctx.GetStart().GetColumn()
stopLine := ctx.GetStop().GetLine()
Expand Down Expand Up @@ -190,33 +190,14 @@ func (s *JavaIdentifierListener) EnterAnnotation(ctx *parser.AnnotationContext)
}

if hasEnterClass {
annotation := buildAnnotation(ctx)
annotation := common_listener.BuildAnnotation(ctx)
currentMethod.Annotations = append(currentMethod.Annotations, annotation)
} else {
annotation := buildAnnotation(ctx)
annotation := common_listener.BuildAnnotation(ctx)
currentNode.Annotations = append(currentNode.Annotations, annotation)
}
}

func buildAnnotation(ctx *parser.AnnotationContext) models.Annotation {
annotationName := ctx.QualifiedName().GetText()
annotation := models.NewAnnotation()
annotation.QualifiedName = annotationName
if ctx.ElementValuePairs() != nil {
pairs := ctx.ElementValuePairs().(*parser.ElementValuePairsContext).AllElementValuePair()
for _, pair := range pairs {
pairCtx := pair.(*parser.ElementValuePairContext)
pairCtx.ElementValue()
annotation.ValuePairs = append(annotation.ValuePairs, *&models.AnnotationKeyValue{
Key: pairCtx.IDENTIFIER().GetText(),
Value: pairCtx.ElementValue().GetText(),
})
}
}

return annotation
}

func (s *JavaIdentifierListener) EnterInterfaceDeclaration(ctx *parser.InterfaceDeclarationContext) {
hasEnterClass = true
currentNode.ClassType = "Interface"
Expand Down
3 changes: 2 additions & 1 deletion core/models/jclass_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type JClassNode struct {
MethodCalls []JMethodCall
Extend string
Implements []string
Annotations []Annotation
}

type JAppField struct {
Expand All @@ -18,5 +19,5 @@ type JAppField struct {
}

func NewClassNode() JClassNode {
return *&JClassNode{"", "", "", "", nil, nil, nil, "", nil}
return *&JClassNode{"", "", "", "", nil, nil, nil, "", nil, nil}
}

0 comments on commit 9dec07f

Please sign in to comment.