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

import (
P
Phodal Huang 已提交
4
	"fmt"
P
Phodal Huang 已提交
5
	"github.com/antlr/antlr4/runtime/Go/antlr"
P
Phodal Huang 已提交
6 7
	"coca/adapter/models"
	. "coca/language/java"
P
Phodal Huang 已提交
8
	"reflect"
P
Phodal Huang 已提交
9 10 11
	"strings"
)

P
Phodal Huang 已提交
12
var jClassNodes []models.JClassNode
P
Phodal Huang 已提交
13

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

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

var currentRestApi RestApi
var RestApis []RestApi
P
Phodal Huang 已提交
31
var pathVars = make(map[string]string)
P
Phodal Huang 已提交
32

P
Phodal Huang 已提交
33
func NewJavaApiListener() *JavaApiListener {
P
Phodal Huang 已提交
34
	isSpringRestController = false
P
Phodal Huang 已提交
35 36
	params := make(map[string]string)
	currentRestApi = *&RestApi{"", "", "", "", "", params}
P
Phodal Huang 已提交
37
	return &JavaApiListener{}
P
Phodal Huang 已提交
38 39
}

P
Phodal Huang 已提交
40
type JavaApiListener struct {
P
Phodal Huang 已提交
41 42 43
	BaseJavaParserListener
}

P
Phodal Huang 已提交
44
func (s *JavaApiListener) EnterClassDeclaration(ctx *ClassDeclarationContext) {
P
Phodal Huang 已提交
45 46 47
	hasEnterClass = true
}

P
Phodal Huang 已提交
48
func (s *JavaApiListener) EnterAnnotation(ctx *AnnotationContext) {
P
Phodal Huang 已提交
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
	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 已提交
85
	currentRestApi = RestApi{uriRemoveQuote, "", "", "", "", nil}
P
Phodal Huang 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99
	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 已提交
100 101
var requestBodyClass string

P
Phodal Huang 已提交
102
func (s *JavaApiListener) EnterMethodDeclaration(ctx *MethodDeclarationContext) {
P
Phodal Huang 已提交
103 104 105 106 107
	if hasEnterRestController && ctx.FormalParameters() != nil {
		if ctx.FormalParameters().GetChild(0) == nil || ctx.FormalParameters().GetText() == "()" || ctx.FormalParameters().GetChild(1) == nil {
			return
		}

P
Phodal Huang 已提交
108
		currentRestApi.MethodName = ctx.IDENTIFIER().GetText()
P
Phodal Huang 已提交
109 110
		buildRestApi(ctx)
	}
P
Phodal Huang 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

	methodBody := ctx.MethodBody()
	blockContext := methodBody.GetChild(0)
	if reflect.TypeOf(blockContext).String() == "*parser.BlockContext" {
		filterMethodCall(blockContext)
	}
}

func filterMethodCall(blockContext antlr.Tree) {
	blcStatement := blockContext.(*BlockContext).AllBlockStatement()
	for _, rangeStatement := range blcStatement {
		if reflect.TypeOf(rangeStatement.GetChild(0)).String() == "*parser.StatementContext" {
			statement := rangeStatement.GetChild(0).(*StatementContext)
			if reflect.TypeOf(statement.GetChild(0)).String() == "*parser.ExpressionContext" {
				express := statement.GetChild(0).(*ExpressionContext)
P
Phodal Huang 已提交
126
				reflect.TypeOf(express.GetChild(0))
P
Phodal Huang 已提交
127 128 129
			}
		}
	}
P
Phodal Huang 已提交
130
}
P
Phodal Huang 已提交
131

P
Phodal Huang 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
func buildRestApi(ctx *MethodDeclarationContext) {
	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
				}
P
Phodal Huang 已提交
147 148 149 150

				if qualifiedName == "PathVariable" {
					fmt.Println()
				}
P
Phodal Huang 已提交
151 152 153
			}
		}

P
Phodal Huang 已提交
154 155
		paramType := paramContext.TypeType().GetText()
		paramValue := paramContext.VariableDeclaratorId().(*VariableDeclaratorIdContext).IDENTIFIER().GetText()
P
Phodal Huang 已提交
156

P
Phodal Huang 已提交
157 158 159
		if hasRequestBody {
			requestBodyClass = paramType
		}
P
Phodal Huang 已提交
160

P
Phodal Huang 已提交
161
		localVars[paramValue] = paramType
P
Phodal Huang 已提交
162
	}
P
Phodal Huang 已提交
163 164 165
	currentRestApi.RequestBodyClass = requestBodyClass

	buildMethodParameters(requestBodyClass)
P
Phodal Huang 已提交
166

P
Phodal Huang 已提交
167 168 169
	hasEnterRestController = false
	requestBodyClass = ""
	RestApis = append(RestApis, currentRestApi)
P
Phodal Huang 已提交
170
}
P
Phodal Huang 已提交
171

P
Phodal Huang 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184
func buildMethodParameters(requestBodyClass string) {
	params := make(map[string]string)
	for _, clz := range jClassNodes {
		if clz.Class == requestBodyClass {
			for _, field := range clz.Fields {
				params[field.Value] = field.Type
			}
		}
	}

	currentRestApi.MethodParams = params
}

P
Phodal Huang 已提交
185
func (s *JavaApiListener) appendClasses(classes []models.JClassNode) {
P
Phodal Huang 已提交
186
	jClassNodes = classes
P
Phodal Huang 已提交
187
}
P
Phodal Huang 已提交
188

P
Phodal Huang 已提交
189
func (s *JavaApiListener) getClassApis() []RestApi {
P
Phodal Huang 已提交
190 191
	return RestApis
}