未验证 提交 f6258432 编写于 作者: P Phodal Huang

feat: add annotation support for interface api call

上级 f9feda8c
......@@ -34,7 +34,7 @@ var analysisCmd = &cobra.Command{
var classes []string = nil
for _, node := range iNodes {
classes = append(classes, node.Package+"."+node.Name)
classes = append(classes, node.Package+"."+node.ClassName)
}
callApp := new(JavaCallApp)
......
......@@ -36,14 +36,14 @@ var apiCmd = &cobra.Command{
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
path := *&apiCmdConfig.Path
dependence := *&apiCmdConfig.DependencePath
depPath := *&apiCmdConfig.DependencePath
apiPrefix := *&apiCmdConfig.AggregateApi
var restApis []RestApi
if path != "" {
if *&apiCmdConfig.ForceUpdate {
app := new(JavaApiApp)
restApis = app.AnalysisPath(path, dependence)
restApis = app.AnalysisPath(path, depPath)
cModel, _ := json.MarshalIndent(restApis, "", "\t")
WriteToCocaFile("apis.json", string(cModel))
} else {
......@@ -55,9 +55,9 @@ var apiCmd = &cobra.Command{
}
var parsedDeps []models.JClassNode
file := ReadFile(dependence)
file := ReadFile(depPath)
if file == nil {
log.Fatal("lost file:" + dependence)
log.Fatal("lost file:" + depPath)
}
_ = json.Unmarshal(file, &parsedDeps)
......
package api
import (
"coca/core/adapter/identifier"
"coca/core/models"
"coca/core/support"
"encoding/json"
......@@ -25,6 +26,13 @@ func (j *JavaApiApp) AnalysisPath(codeDir string, depPath string) []RestApi {
_ = json.Unmarshal(file, &parsedDeps)
identifiers := LoadIdentify(depPath)
var identifiersMap = make(map[string]models.JIdentifier)
for _, ident := range identifiers {
identifiersMap[ident.Package + "." + ident.ClassName] = ident
}
files := support.GetJavaFiles(codeDir)
for index := range files {
file := files[index]
......@@ -35,7 +43,7 @@ func (j *JavaApiApp) AnalysisPath(codeDir string, depPath string) []RestApi {
parser := support.ProcessFile(file)
context := parser.CompilationUnit()
listener := NewJavaApiListener()
listener := NewJavaApiListener(identifiersMap)
listener.appendClasses(parsedDeps)
antlr.NewParseTreeWalker().Walk(listener, context)
......@@ -46,3 +54,21 @@ func (j *JavaApiApp) AnalysisPath(codeDir string, depPath string) []RestApi {
return *&allApis
}
func LoadIdentify(importPath string) []models.JIdentifier {
var identifiers []models.JIdentifier
apiContent := support.ReadCocaFile("identify.json")
if apiContent == nil {
identifierApp := new(identifier.JavaIdentifierApp)
ident := identifierApp.AnalysisPath(importPath)
identModel, _ := json.MarshalIndent(ident, "", "\t")
support.WriteToCocaFile("identify.json", string(identModel))
return *&ident
}
_ = json.Unmarshal(apiContent, &identifiers)
return *&identifiers
}
\ No newline at end of file
......@@ -32,10 +32,21 @@ var RestApis []RestApi
var currentClz string
var currentPkg string
func NewJavaApiListener() *JavaApiListener {
var identMap map[string]models2.JIdentifier
var imports []string
var currentExtends = ""
var currentImplements = ""
func NewJavaApiListener(ident map[string]models2.JIdentifier) *JavaApiListener {
isSpringRestController = false
currentClz = ""
currentPkg = ""
currentExtends = ""
currentImplements = ""
imports = nil
identMap = ident
params := make(map[string]string)
currentRestApi = *&RestApi{"", "", "", "", "", params, "", ""}
......@@ -46,6 +57,11 @@ type JavaApiListener struct {
parser.BaseJavaParserListener
}
func (s *JavaApiListener) EnterImportDeclaration(ctx *parser.ImportDeclarationContext) {
importText := ctx.QualifiedName().GetText()
imports = append(imports, importText)
}
func (s *JavaApiListener) EnterPackageDeclaration(ctx *parser.PackageDeclarationContext) {
currentPkg = ctx.QualifiedName().GetText()
}
......@@ -55,6 +71,14 @@ func (s *JavaApiListener) EnterClassDeclaration(ctx *parser.ClassDeclarationCont
if ctx.IDENTIFIER() != nil {
currentClz = ctx.IDENTIFIER().GetText()
}
if ctx.EXTENDS() != nil {
currentExtends = ctx.TypeType().GetText()
}
if ctx.IMPLEMENTS() != nil {
currentImplements = ctx.TypeList().GetText()
}
}
func (s *JavaApiListener) ExitClassDeclaration(ctx *parser.ClassDeclarationContext) {
......@@ -83,7 +107,7 @@ func (s *JavaApiListener) EnterAnnotation(ctx *parser.AnnotationContext) {
baseApiUrlName = text[1 : len(text)-1]
}
}
} else if ctx.ElementValue() != nil{
} else if ctx.ElementValue() != nil {
text := ctx.ElementValue().GetText()
baseApiUrlName = text[1 : len(text)-1]
} else {
......@@ -124,7 +148,7 @@ func (s *JavaApiListener) EnterAnnotation(ctx *parser.AnnotationContext) {
}
if pair.IDENTIFIER().GetText() == "value" {
text := pair.ElementValue().GetText()
currentRestApi.Uri = baseApiUrlName + text[1 : len(text)-1]
currentRestApi.Uri = baseApiUrlName + text[1:len(text)-1]
}
}
}
......@@ -161,7 +185,44 @@ func addApiMethod(annotationName string) {
var requestBodyClass string
func contains(slice []string, item string) bool {
set := make(map[string]struct{}, len(slice))
for _, s := range slice {
set[s] = struct{}{}
}
_, ok := set[item]
return ok
}
func (s *JavaApiListener) EnterMethodDeclaration(ctx *parser.MethodDeclarationContext) {
methodName := ctx.IDENTIFIER().GetText()
if currentImplements != "" {
var superClz = ""
for index := range imports {
imp := imports[index]
if strings.HasSuffix(imp, "."+currentImplements) {
superClz = imp
}
}
if _, ok := identMap[superClz]; ok {
for _, method := range identMap[superClz].Methods {
if method.Name == methodName {
if contains(method.Annotations, "ServiceMethod") {
currentRestApi.PackageName = currentPkg
currentRestApi.ClassName = currentClz
currentRestApi.MethodName = methodName
RestApis = append(RestApis, currentRestApi)
return
}
}
}
}
}
if hasEnterRestController && ctx.FormalParameters() != nil {
if ctx.FormalParameters().GetChild(0) == nil || ctx.FormalParameters().GetChild(1) == nil {
return
......@@ -169,7 +230,7 @@ func (s *JavaApiListener) EnterMethodDeclaration(ctx *parser.MethodDeclarationCo
currentRestApi.PackageName = currentPkg
currentRestApi.ClassName = currentClz
currentRestApi.MethodName = ctx.IDENTIFIER().GetText()
currentRestApi.MethodName = methodName
if ctx.FormalParameters().GetText() == "()" {
currentRestApi.RequestBodyClass = requestBodyClass
hasEnterRestController = false
......
package call
import (
"coca/core/adapter/identifier"
"coca/core/models"
"coca/core/support"
"fmt"
......@@ -14,7 +13,7 @@ var nodeInfos []models.JClassNode
type JavaCallApp struct {
}
func (j *JavaCallApp) AnalysisPath(codeDir string, classes []string, identNodes []identifier.JIdentifier) []models.JClassNode {
func (j *JavaCallApp) AnalysisPath(codeDir string, classes []string, identNodes []models.JIdentifier) []models.JClassNode {
nodeInfos = nil
files := support.GetJavaFiles(codeDir)
for index := range files {
......
package call
import (
"coca/core/adapter/identifier"
"coca/core/languages/java"
"coca/core/models"
"github.com/antlr/antlr4/runtime/Go/antlr"
......@@ -28,9 +27,9 @@ var methodMap = make(map[string]models.JMethod)
var methodQueue []models.JMethod
var classQueue []string
var identNodes []identifier.JIdentifier
var identNodes []models.JIdentifier
func NewJavaCallListener(nodes []identifier.JIdentifier) *JavaCallListener {
func NewJavaCallListener(nodes []models.JIdentifier) *JavaCallListener {
currentClz = ""
currentPkg = ""
currentMethod = models.NewJMethod()
......
package identifier
import (
"coca/core/models"
"coca/core/support"
"github.com/antlr/antlr4/runtime/Go/antlr"
)
var nodeInfos []JIdentifier = nil
var nodeInfos []models.JIdentifier = nil
type JavaIdentifierApp struct {
}
func (j *JavaIdentifierApp) AnalysisPath(codeDir string) []JIdentifier {
func (j *JavaIdentifierApp) AnalysisPath(codeDir string) []models.JIdentifier {
nodeInfos = nil
files := support.GetJavaFiles(codeDir)
for index := range files {
......@@ -19,13 +20,13 @@ func (j *JavaIdentifierApp) AnalysisPath(codeDir string) []JIdentifier {
parser := support.ProcessFile(file)
context := parser.CompilationUnit()
clzInfo := NewJIdentifier()
clzInfo := models.NewJIdentifier()
listener := new(JavaIdentifierListener)
listener.InitNode(clzInfo)
antlr.NewParseTreeWalker().Walk(listener, context)
if clzInfo.Name != "" {
if clzInfo.ClassName != "" {
clzInfo.Methods = clzInfo.GetMethods()
nodeInfos = append(nodeInfos, *clzInfo)
}
......
......@@ -3,9 +3,11 @@ package identifier
import (
"coca/core/languages/java"
"coca/core/models"
"fmt"
"reflect"
)
var node *JIdentifier
var node *models.JIdentifier
var currentMethod models.JMethod
var hasEnterClass = false
......@@ -24,7 +26,7 @@ func (s *JavaIdentifierListener) EnterClassDeclaration(ctx *parser.ClassDeclarat
node.Type = "Class"
if ctx.IDENTIFIER() != nil {
node.Name = ctx.IDENTIFIER().GetText()
node.ClassName = ctx.IDENTIFIER().GetText()
}
if ctx.EXTENDS() != nil {
......@@ -32,11 +34,22 @@ func (s *JavaIdentifierListener) EnterClassDeclaration(ctx *parser.ClassDeclarat
}
}
func (s *JavaIdentifierListener) ExitClassDeclaration(ctx *parser.ClassDeclarationContext) {
hasEnterClass = false
}
func (s *JavaIdentifierListener) EnterInterfaceBodyDeclaration(ctx *parser.InterfaceBodyDeclarationContext) {
fmt.Println(ctx.GetText())
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)
currentMethod.Annotations = append(currentMethod.Annotations, annotationContext.QualifiedName().GetText())
}
}
}
func (s *JavaIdentifierListener) EnterInterfaceMethodDeclaration(ctx *parser.InterfaceMethodDeclarationContext) {
hasEnterMethod = true
......@@ -61,7 +74,7 @@ func (s *JavaIdentifierListener) EnterInterfaceMethodDeclaration(ctx *parser.Int
}
}
func (s *JavaIdentifierListener) ExitInterfaceDeclaration(ctx *parser.InterfaceDeclarationContext) {
func (s *JavaIdentifierListener) ExitInterfaceMethodDeclaration(ctx *parser.InterfaceMethodDeclarationContext) {
hasEnterMethod = false
node.AddMethod(currentMethod)
......@@ -110,18 +123,17 @@ func (s *JavaIdentifierListener) EnterAnnotation(ctx *parser.AnnotationContext)
if annotationName == "Override" {
isOverrideMethod = true
}
if hasEnterClass {
if !hasEnterMethod {
currentMethod.Annotations = append(currentMethod.Annotations, annotationName)
}
currentMethod.Annotations = append(currentMethod.Annotations, annotationName)
}
}
func (s *JavaIdentifierListener) EnterInterfaceDeclaration(ctx *parser.InterfaceDeclarationContext) {
node.Type = "Interface"
node.Name = ctx.IDENTIFIER().GetText()
node.ClassName = ctx.IDENTIFIER().GetText()
}
func (s *JavaIdentifierListener) InitNode(identifier *JIdentifier) {
func (s *JavaIdentifierListener) InitNode(identifier *models.JIdentifier) {
node = identifier
}
package identifier
package models
import "coca/core/models"
var methods []models.JMethod
var methods []JMethod
type JIdentifier struct {
Package string
Name string
ClassName string
Type string
ExtendsName string
Extends []JIdentifier
Methods []models.JMethod
Methods []JMethod
}
func NewJIdentifier() *JIdentifier {
......@@ -19,10 +17,10 @@ func NewJIdentifier() *JIdentifier {
return identifier
}
func (identifier *JIdentifier) AddMethod(method models.JMethod) {
func (identifier *JIdentifier) AddMethod(method JMethod) {
methods = append(methods, method)
}
func (identifier *JIdentifier) GetMethods() []models.JMethod {
func (identifier *JIdentifier) GetMethods() []JMethod {
return methods
}
......@@ -13,6 +13,8 @@ type JMethod struct {
Annotations []string
}
// TODO support annnotation
func NewJMethod() JMethod {
return *&JMethod{
Name: "",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册