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

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

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

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

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

var currentRestApi RestApi
var RestApis []RestApi

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

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

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

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

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

P
Phodal Huang 已提交
104 105
		buildRestApi(ctx)
	}
P
Phodal Huang 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

	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 已提交
121
				reflect.TypeOf(express.GetChild(0))
P
Phodal Huang 已提交
122 123 124
			}
		}
	}
P
Phodal Huang 已提交
125
}
P
Phodal Huang 已提交
126

P
Phodal Huang 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
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 已提交
142 143 144
			}
		}

P
Phodal Huang 已提交
145 146
		paramType := paramContext.TypeType().GetText()
		paramValue := paramContext.VariableDeclaratorId().(*VariableDeclaratorIdContext).IDENTIFIER().GetText()
P
Phodal Huang 已提交
147

P
Phodal Huang 已提交
148 149 150
		if hasRequestBody {
			requestBodyClass = paramType
		}
P
Phodal Huang 已提交
151

P
Phodal Huang 已提交
152
		localVars[paramValue] = paramType
P
Phodal Huang 已提交
153
	}
P
Phodal Huang 已提交
154
	currentRestApi.Body = requestBodyClass
P
Phodal Huang 已提交
155

P
Phodal Huang 已提交
156 157 158
	hasEnterRestController = false
	requestBodyClass = ""
	RestApis = append(RestApis, currentRestApi)
P
Phodal Huang 已提交
159
}
P
Phodal Huang 已提交
160

P
Phodal Huang 已提交
161
func (s *JavaApiListener) appendClasses(classes []models.JClassNode) {
P
Phodal Huang 已提交
162
	clz = classes
P
Phodal Huang 已提交
163
}
P
Phodal Huang 已提交
164

P
Phodal Huang 已提交
165
func (s *JavaApiListener) getClassApis() []RestApi {
P
Phodal Huang 已提交
166 167
	return RestApis
}