diff --git a/core/infrastructure/ast/api/java_api_listener.go b/core/infrastructure/ast/api/java_api_listener.go index c3e574da81a7fb53833347deb420dd45703e5d49..954a343519e1c90c6f0bae5a2c7ea028e8b15ce6 100644 --- a/core/infrastructure/ast/api/java_api_listener.go +++ b/core/infrastructure/ast/api/java_api_listener.go @@ -13,7 +13,7 @@ var jClassNodes []models.JClassNode var hasEnterClass = false var isSpringRestController = false var hasEnterRestController = false -var baseApiUrlName string +var baseApiUrl string var localVars = make(map[string]string) var currentRestApi models.RestApi @@ -88,36 +88,20 @@ func (s *JavaApiListener) EnterAnnotation(ctx *parser.AnnotationContext) { } if !hasEnterClass { - // 类声明处的注解 - if annotationName == "RequestMapping" { - if ctx.ElementValuePairs() != nil { - allValuePair := ctx.ElementValuePairs().(*parser.ElementValuePairsContext).AllElementValuePair() - for _, valuePair := range allValuePair { - pair := valuePair.(*parser.ElementValuePairContext) - if pair.IDENTIFIER().GetText() == "value" { - text := pair.ElementValue().GetText() - baseApiUrlName = text[1 : len(text)-1] - } - } - } else if ctx.ElementValue() != nil { - text := ctx.ElementValue().GetText() - baseApiUrlName = text[1 : len(text)-1] - } else { - baseApiUrlName = "/" - } - } + buildBaseApiUrlString(annotationName, ctx) } - if !(annotationName == "RequestMapping" || annotationName == "GetMapping" || annotationName == "PutMapping" || annotationName == "PostMapping" || annotationName == "DeleteMapping") { + notApi := annotationName == "RequestMapping" || annotationName == "GetMapping" || annotationName == "PutMapping" || annotationName == "PostMapping" || annotationName == "DeleteMapping" + if !notApi { return } hasEnterRestController = true uri := "" if ctx.ElementValue() != nil { - uri = baseApiUrlName + ctx.ElementValue().GetText() + uri = baseApiUrl + ctx.ElementValue().GetText() } else { - uri = baseApiUrlName + uri = baseApiUrl } uriRemoveQuote := strings.ReplaceAll(uri, "\"", "") @@ -140,12 +124,33 @@ func (s *JavaApiListener) EnterAnnotation(ctx *parser.AnnotationContext) { } if pair.IDENTIFIER().GetText() == "value" { text := pair.ElementValue().GetText() - currentRestApi.Uri = baseApiUrlName + text[1:len(text)-1] + currentRestApi.Uri = baseApiUrl + text[1:len(text)-1] } } } } +func buildBaseApiUrlString(annotationName string, ctx *parser.AnnotationContext) { + // 类声明处的注解 + if annotationName == "RequestMapping" { + if ctx.ElementValuePairs() != nil { + allValuePair := ctx.ElementValuePairs().(*parser.ElementValuePairsContext).AllElementValuePair() + for _, valuePair := range allValuePair { + pair := valuePair.(*parser.ElementValuePairContext) + if pair.IDENTIFIER().GetText() == "value" { + text := pair.ElementValue().GetText() + baseApiUrl = text[1 : len(text)-1] + } + } + } else if ctx.ElementValue() != nil { + text := ctx.ElementValue().GetText() + baseApiUrl = text[1 : len(text)-1] + } else { + baseApiUrl = "/" + } + } +} + func addApiMethod(annotationName string) { switch annotationName { case