From ad538af6e06d93b0dd5616932a0f91f09bd2618e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E4=B8=AD=E5=A5=87?= Date: Tue, 13 Jan 2026 14:48:56 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=B0=9D=E8=AF=95=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89controller=E6=B3=A8=E8=A7=A3?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E8=A2=AB=E6=89=AB=E6=8F=8F=20#198?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tools/idea/search/utils/SpringUtils.java | 90 +++++++++++++++++-- .../boot/mybatis/annotations/OpenApis.java | 28 ++++++ .../CustomerAnnotationController.java | 34 +++++++ 3 files changed, 144 insertions(+), 8 deletions(-) create mode 100644 debug-tools-test/debug-tools-test-spring-boot-mybatis/src/main/java/io/github/future0923/debug/tools/test/spring/boot/mybatis/annotations/OpenApis.java create mode 100644 debug-tools-test/debug-tools-test-spring-boot-mybatis/src/main/java/io/github/future0923/debug/tools/test/spring/boot/mybatis/controller/CustomerAnnotationController.java diff --git a/debug-tools-idea/src/main/java/io/github/future0923/debug/tools/idea/search/utils/SpringUtils.java b/debug-tools-idea/src/main/java/io/github/future0923/debug/tools/idea/search/utils/SpringUtils.java index d80468dc..3efbb665 100644 --- a/debug-tools-idea/src/main/java/io/github/future0923/debug/tools/idea/search/utils/SpringUtils.java +++ b/debug-tools-idea/src/main/java/io/github/future0923/debug/tools/idea/search/utils/SpringUtils.java @@ -23,6 +23,7 @@ import com.intellij.psi.PsiAnnotation; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiJavaCodeReferenceElement; import com.intellij.psi.PsiJvmMember; import com.intellij.psi.PsiMethod; import com.intellij.psi.PsiModifierList; @@ -51,6 +52,10 @@ * @author future0923 */ public class SpringUtils { + private static final Set CONTROLLER_ANNOTATIONS = Set.of( + "org.springframework.web.bind.annotation.RestController", + "org.springframework.stereotype.Controller" + ); /** * 获取Spring环境下的所有请求 @@ -81,19 +86,88 @@ public static List getSpringRequestByModule(Project project, Module private static List getAllControllerClass(Project project, Module module) { List allControllerClass = new ArrayList<>(); GlobalSearchScope moduleScope = getModuleScope(project, module); - Collection pathList = JavaAnnotationIndex.getInstance().get("Controller", project, moduleScope); - pathList.addAll(JavaAnnotationIndex.getInstance().get("RestController", project, moduleScope)); - for (PsiAnnotation psiAnnotation : pathList) { - PsiModifierList psiModifierList = (PsiModifierList) psiAnnotation.getParent(); - PsiElement psiElement = psiModifierList.getParent(); - if (!(psiElement instanceof PsiClass psiClass)) { - continue; + Collection annotationNames = JavaAnnotationIndex.getInstance().getAllKeys(project); + Set controllerAnnotationNames = new HashSet<>(); + + for (String annotationName : annotationNames) { + Collection annotations = JavaAnnotationIndex.getInstance().get(annotationName, project, moduleScope); + for (PsiAnnotation usage : annotations) { + PsiJavaCodeReferenceElement ref = usage.getNameReferenceElement(); + if (ref == null) continue; + + PsiElement resolved = ref.resolve(); + if (!(resolved instanceof PsiClass annoClass)) continue; + + if (!annoClass.isAnnotationType()) continue; + + if (isControllerAnnotation(annoClass)) { + String qualifiedName = annoClass.getQualifiedName(); + if (qualifiedName != null) { + controllerAnnotationNames.add(qualifiedName); + } + } + } + } + + for (String annotationName : controllerAnnotationNames) { + String annotationShortName = getShortName(annotationName); + + Collection usages = JavaAnnotationIndex.getInstance().getAnnotations(annotationShortName, project, moduleScope); + + for (PsiAnnotation usage : usages) { + PsiElement owner = usage.getParent().getParent(); + if (owner instanceof PsiClass psiClass) { + allControllerClass.add(psiClass); + } } - allControllerClass.add(psiClass); } + return allControllerClass; } + private static boolean isControllerAnnotation(PsiClass annoClass) { + PsiModifierList modifierList = annoClass.getModifierList(); + if (modifierList == null) return false; + + for (PsiAnnotation annotation : modifierList.getAnnotations()) { + if (hasControllerMeta(annotation, new HashSet<>())) { + return true; + } + } + return false; + } + + private static boolean hasControllerMeta(PsiAnnotation annotation, Set visited) { + String qName = annotation.getQualifiedName(); + if (qName == null || !visited.add(qName)) { + return false; + } + + if (CONTROLLER_ANNOTATIONS.contains(qName)) { + return true; + } + + PsiJavaCodeReferenceElement ref = annotation.getNameReferenceElement(); + if (ref == null) return false; + + PsiElement resolved = ref.resolve(); + if (!(resolved instanceof PsiClass annoClass)) return false; + + PsiModifierList modifierList = annoClass.getModifierList(); + if (modifierList == null) return false; + + for (PsiAnnotation meta : modifierList.getAnnotations()) { + if (hasControllerMeta(meta, visited)) { + return true; + } + } + return false; + } + + public static String getShortName(String qualifiedName) { + return qualifiedName.substring(qualifiedName.lastIndexOf(".") + 1); + } + /** * 获取Controller下所有的请求信息 * diff --git a/debug-tools-test/debug-tools-test-spring-boot-mybatis/src/main/java/io/github/future0923/debug/tools/test/spring/boot/mybatis/annotations/OpenApis.java b/debug-tools-test/debug-tools-test-spring-boot-mybatis/src/main/java/io/github/future0923/debug/tools/test/spring/boot/mybatis/annotations/OpenApis.java new file mode 100644 index 00000000..04850476 --- /dev/null +++ b/debug-tools-test/debug-tools-test-spring-boot-mybatis/src/main/java/io/github/future0923/debug/tools/test/spring/boot/mybatis/annotations/OpenApis.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2024-2025 the original author or authors. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package io.github.future0923.debug.tools.test.spring.boot.mybatis.annotations; + +import org.springframework.web.bind.annotation.RestController; + +import java.lang.annotation.*; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@RestController +public @interface OpenApis { +} diff --git a/debug-tools-test/debug-tools-test-spring-boot-mybatis/src/main/java/io/github/future0923/debug/tools/test/spring/boot/mybatis/controller/CustomerAnnotationController.java b/debug-tools-test/debug-tools-test-spring-boot-mybatis/src/main/java/io/github/future0923/debug/tools/test/spring/boot/mybatis/controller/CustomerAnnotationController.java new file mode 100644 index 00000000..9130db1b --- /dev/null +++ b/debug-tools-test/debug-tools-test-spring-boot-mybatis/src/main/java/io/github/future0923/debug/tools/test/spring/boot/mybatis/controller/CustomerAnnotationController.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2024-2025 the original author or authors. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package io.github.future0923.debug.tools.test.spring.boot.mybatis.controller; + +import io.github.future0923.debug.tools.test.spring.boot.mybatis.annotations.OpenApis; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; + +@OpenApis +@RequestMapping("customer") +public class CustomerAnnotationController { + + @PostMapping("customerPost") + public void customerPost() { + } + @GetMapping("customerGet") + public void customerGet() { + } +}