JavaApiListener.go 2.4 KB
Newer Older
P
Phodal Huang 已提交
1 2 3
package api

import (
P
Phodal Huang 已提交
4
	"github.com/phodal/coca/adapter/models"
P
Phodal Huang 已提交
5 6 7 8
	. "github.com/phodal/coca/language/java"
	"strings"
)

P
Phodal Huang 已提交
9 10
var clz []models.JClassNode

P
Phodal Huang 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
type RestApi struct {
	Uri            string
	HttpMethod     string
	MethodName     string
	ResponseStatus string
	Body           []string
	MethodParams   map[string]string
}

var hasEnterClass = false
var isSpringRestController = false
var hasEnterRestController = false
var baseApiUrlName = ""

var currentRestApi RestApi
var RestApis []RestApi

P
Phodal Huang 已提交
28
func NewJavaApiListener() *JavaApiListener {
P
Phodal Huang 已提交
29
	isSpringRestController = false
P
Phodal Huang 已提交
30
	return &JavaApiListener{}
P
Phodal Huang 已提交
31 32
}

P
Phodal Huang 已提交
33
type JavaApiListener struct {
P
Phodal Huang 已提交
34 35 36
	BaseJavaParserListener
}

P
Phodal Huang 已提交
37
func (s *JavaApiListener) EnterClassDeclaration(ctx *ClassDeclarationContext) {
P
Phodal Huang 已提交
38 39 40
	hasEnterClass = true
}

P
Phodal Huang 已提交
41
func (s *JavaApiListener) EnterAnnotation(ctx *AnnotationContext) {
P
Phodal Huang 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
	annotationName := ctx.QualifiedName().GetText()
	if annotationName == "RestController" {
		isSpringRestController = true
	}

	if !isSpringRestController {
		return
	}

	if !hasEnterClass {
		if annotationName == "RequestMapping" {
			if ctx.ElementValuePairs() != nil {
				firstPair := ctx.ElementValuePairs().GetChild(0).(*ElementValuePairContext)
				if firstPair.IDENTIFIER().GetText() == "value" {
					baseApiUrlName = firstPair.ElementValue().GetText()
				}
			} else {
				baseApiUrlName = "/"
			}
		}
	}

	if !(annotationName == "GetMapping" || annotationName == "PutMapping" || annotationName == "PostMapping" || annotationName == "DeleteMapping") {
		return
	}

	hasEnterRestController = true
	uri := ""
	if ctx.ElementValue() != nil {
		uri = baseApiUrlName + ctx.ElementValue().GetText()
	} else {
		uri = baseApiUrlName
	}

	uriRemoveQuote := strings.ReplaceAll(uri, "\"", "")

	currentRestApi = RestApi{uriRemoveQuote, "", "", "", nil, nil}
	if hasEnterClass {
		switch annotationName {
		case "GetMapping":
			currentRestApi.HttpMethod = "GET"
		case "PutMapping":
			currentRestApi.HttpMethod = "PUT"
		case "PostMapping":
			currentRestApi.HttpMethod = "POST"
		case "DeleteMapping":
			currentRestApi.HttpMethod = "DELETE"
		}
	}
}

P
Phodal Huang 已提交
93
func (s *JavaApiListener) EnterMethodDeclaration(ctx *MethodDeclarationContext) {
P
Phodal Huang 已提交
94 95 96 97
	if hasEnterRestController {
		RestApis = append(RestApis, currentRestApi)
		hasEnterRestController = false
	}
P
Phodal Huang 已提交
98
}
P
Phodal Huang 已提交
99

P
Phodal Huang 已提交
100
func (s *JavaApiListener) appendClasses(classes []models.JClassNode) {
P
Phodal Huang 已提交
101
	clz = classes
P
Phodal Huang 已提交
102
}
P
Phodal Huang 已提交
103 104


P
Phodal Huang 已提交
105
func (s *JavaApiListener) getApis() []RestApi {
P
Phodal Huang 已提交
106 107 108
	return RestApis
}