JavaApiListener.go 3.6 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
	. "github.com/phodal/coca/language/java"
P
Phodal Huang 已提交
6
	"reflect"
P
Phodal Huang 已提交
7 8 9
	"strings"
)

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

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

var hasEnterClass = false
var isSpringRestController = false
var hasEnterRestController = false
var baseApiUrlName = ""
P
Phodal Huang 已提交
25
var localVars = make(map[string]string)
P
Phodal Huang 已提交
26 27 28 29

var currentRestApi RestApi
var RestApis []RestApi

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

P
Phodal Huang 已提交
35
type JavaApiListener struct {
P
Phodal Huang 已提交
36 37 38
	BaseJavaParserListener
}

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

P
Phodal Huang 已提交
43
func (s *JavaApiListener) EnterAnnotation(ctx *AnnotationContext) {
P
Phodal Huang 已提交
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
	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, "\"", "")

P
Phodal Huang 已提交
80
	currentRestApi = RestApi{uriRemoveQuote, "", "", "", "", nil}
P
Phodal Huang 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94
	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 已提交
95 96
var requestBodyClass string

P
Phodal Huang 已提交
97
func (s *JavaApiListener) EnterMethodDeclaration(ctx *MethodDeclarationContext) {
P
Phodal Huang 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
	if hasEnterRestController && ctx.FormalParameters() != nil {
		if ctx.FormalParameters().GetChild(0) == nil || ctx.FormalParameters().GetText() == "()" || ctx.FormalParameters().GetChild(1) == nil {
			return
		}

		parameterList := ctx.FormalParameters().GetChild(1).(*FormalParameterListContext)
		formalParameter := parameterList.AllFormalParameter()
		for _, param := range formalParameter {
			paramContext := param.(*FormalParameterContext)

			modifiers := paramContext.AllVariableModifier()
			hasRequestBody := false
			for _, modifier := range modifiers {
				childType := reflect.TypeOf(modifier.GetChild(0))
				if childType.String() == "*parser.AnnotationContext" {
					qualifiedName := modifier.GetChild(0).(*AnnotationContext).QualifiedName().GetText()
					if qualifiedName == "RequestBody" {
						hasRequestBody = true
					}
				}
			}

			paramType := paramContext.TypeType().GetText()
			paramValue := paramContext.VariableDeclaratorId().(*VariableDeclaratorIdContext).IDENTIFIER().GetText()

			if hasRequestBody {
				requestBodyClass = paramType
			}

			localVars[paramValue] = paramType
		}

		currentRestApi.Body = requestBodyClass

		//currentRestApi.Body
P
Phodal Huang 已提交
133
		hasEnterRestController = false
P
Phodal Huang 已提交
134 135 136
		requestBodyClass = ""

		RestApis = append(RestApis, currentRestApi)
P
Phodal Huang 已提交
137
	}
P
Phodal Huang 已提交
138
}
P
Phodal Huang 已提交
139

P
Phodal Huang 已提交
140
func (s *JavaApiListener) appendClasses(classes []models.JClassNode) {
P
Phodal Huang 已提交
141
	clz = classes
P
Phodal Huang 已提交
142
}
P
Phodal Huang 已提交
143

P
Phodal Huang 已提交
144
func (s *JavaApiListener) getApis() []RestApi {
P
Phodal Huang 已提交
145 146
	return RestApis
}