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

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

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

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

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 31

var currentRestApi RestApi
var RestApis []RestApi

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

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

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

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

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

P
Phodal Huang 已提交
105 106
		buildRestApi(ctx)
	}
P
Phodal Huang 已提交
107 108 109 110 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)
				fmt.Println(reflect.TypeOf(express.GetChild(0)).String())
			}
		}
	}
P
Phodal Huang 已提交
126
}
P
Phodal Huang 已提交
127

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

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

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

P
Phodal Huang 已提交
153
		localVars[paramValue] = paramType
P
Phodal Huang 已提交
154
	}
P
Phodal Huang 已提交
155 156 157 158 159
	currentRestApi.Body = requestBodyClass
	//currentRestApi.Body
	hasEnterRestController = false
	requestBodyClass = ""
	RestApis = append(RestApis, currentRestApi)
P
Phodal Huang 已提交
160
}
P
Phodal Huang 已提交
161

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

P
Phodal Huang 已提交
167
func (s *JavaApiListener) getApis() []RestApi {
P
Phodal Huang 已提交
168 169
	return RestApis
}