Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,6 +52,10 @@
* @author future0923
*/
public class SpringUtils {
private static final Set<String> CONTROLLER_ANNOTATIONS = Set.of(
"org.springframework.web.bind.annotation.RestController",
"org.springframework.stereotype.Controller"
);

/**
* 获取Spring环境下的所有请求
Expand Down Expand Up @@ -81,19 +86,88 @@ public static List<HttpUrlItem> getSpringRequestByModule(Project project, Module
private static List<PsiClass> getAllControllerClass(Project project, Module module) {
List<PsiClass> allControllerClass = new ArrayList<>();
GlobalSearchScope moduleScope = getModuleScope(project, module);
Collection<PsiAnnotation> 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<String> annotationNames = JavaAnnotationIndex.getInstance().getAllKeys(project);
Set<String> controllerAnnotationNames = new HashSet<>();

for (String annotationName : annotationNames) {
Collection<PsiAnnotation> 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<PsiAnnotation> 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<String> 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下所有的请求信息
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/
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 {
}
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/
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() {
}
}