-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathApiResolverService.java
More file actions
185 lines (168 loc) · 8.92 KB
/
ApiResolverService.java
File metadata and controls
185 lines (168 loc) · 8.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package cn.gudqs7.plugins.search.resolver;
import cn.gudqs7.plugins.common.enums.HttpMethod;
import cn.gudqs7.plugins.common.pojo.resolver.CommentInfo;
import cn.gudqs7.plugins.common.resolver.comment.AnnotationHolder;
import cn.gudqs7.plugins.common.util.jetbrain.ExceptionUtil;
import cn.gudqs7.plugins.common.util.structure.PsiAnnotationUtil;
import cn.gudqs7.plugins.search.pojo.RequestMappingAnnotationInfo;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtil;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.impl.java.stubs.index.JavaAnnotationIndex;
import com.intellij.psi.search.GlobalSearchScope;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
/**
* @author wq
* @date 2022/5/28
*/
public class ApiResolverService {
private final Project project;
public ApiResolverService(Project project) {
this.project = project;
}
public static ApiResolverService getInstance(Project project) {
return new ApiResolverService(project);
}
@NotNull
public List<ApiNavigationItem> getApiNavigationItemList() {
List<ApiNavigationItem> navigationItemList = new ArrayList<>();
String[] supportAnnotations = new String[]{"Controller", "RestController"};
List<PsiClass> psiClassList = new ArrayList<>();
for (String supportAnnotation : supportAnnotations) {
Collection<PsiAnnotation> psiAnnotations = JavaAnnotationIndex.getInstance().get(supportAnnotation, project, GlobalSearchScope.projectScope(project));
for (PsiAnnotation psiAnnotation : psiAnnotations) {
PsiModifierList psiModifierList = (PsiModifierList) psiAnnotation.getParent();
PsiElement psiElement = psiModifierList.getParent();
if (psiElement == null) {
continue;
}
PsiClass psiClass = (PsiClass) psiElement;
psiClassList.add(psiClass);
}
}
for (PsiClass psiClass : psiClassList) {
try {
navigationItemList.addAll(getServiceItemList(psiClass));
} catch (Exception e) {
String classQname = psiClass.getQualifiedName();
ExceptionUtil.logException(e, String.format("扫描接口时出错, 接口类全限定名为: %s; 错误信息为: %s", classQname, e.getMessage()));
}
}
return navigationItemList;
}
protected List<ApiNavigationItem> getServiceItemList(@NotNull PsiClass psiClass) {
List<ApiNavigationItem> navigationItemList = new ArrayList<>(2);
List<MethodPathInfo> methodPathList = new ArrayList<>(32);
List<String> classParams = new ArrayList<>();
Set<String> classPathSet = new HashSet<>(32);
PsiAnnotation classRequestMappingAnnotation = psiClass.getAnnotation(AnnotationHolder.QNAME_OF_MAPPING);
// 可能类上不加 @RequestMapping, 则代表 path = "/"
if (classRequestMappingAnnotation != null) {
RequestMappingAnnotationInfo classRequestMappingInfo = PsiAnnotationUtil.getAnnotationInfoByPojo(classRequestMappingAnnotation, RequestMappingAnnotationInfo.class);
classPathSet.addAll(classRequestMappingInfo.getValueOrPath());
List<String> params = classRequestMappingInfo.getParams();
if (params != null) {
classParams.addAll(params);
}
} else {
classPathSet.add("/");
}
String classQname = psiClass.getQualifiedName();
PsiMethod[] psiMethods = psiClass.getMethods();
for (PsiMethod psiMethod : psiMethods) {
try {
methodPathList.addAll(getMethodList0(psiMethod, AnnotationHolder.QNAME_OF_MAPPING, null, psiClass));
methodPathList.addAll(getMethodList0(psiMethod, AnnotationHolder.QNAME_OF_GET_MAPPING, HttpMethod.GET, psiClass));
methodPathList.addAll(getMethodList0(psiMethod, AnnotationHolder.QNAME_OF_POST_MAPPING, HttpMethod.POST, psiClass));
methodPathList.addAll(getMethodList0(psiMethod, AnnotationHolder.QNAME_OF_PUT_MAPPING, HttpMethod.PUT, psiClass));
methodPathList.addAll(getMethodList0(psiMethod, AnnotationHolder.QNAME_OF_DELETE_MAPPING, HttpMethod.DELETE, psiClass));
} catch (Exception e) {
String methodName = psiMethod.getName();
String methodQname = classQname + "#" + methodName;
ExceptionUtil.logException(e, String.format("扫描接口时出错, 方法为: %s; 错误信息为: %s", methodQname, e.getMessage()));
}
}
for (String classPath : classPathSet) {
for (MethodPathInfo methodPathInfo : methodPathList) {
PsiMethod psiMethod = methodPathInfo.getPsiMethod();
HttpMethod httpMethod = methodPathInfo.getHttpMethod();
String methodPath = methodPathInfo.getMethodPath();
List<String> methodParams = methodPathInfo.getParams();
if (!classPath.startsWith("/")) {
classPath = "/".concat(classPath);
}
if (!classPath.endsWith("/")) {
classPath = classPath.concat("/");
}
if (methodPath.startsWith("/")) {
methodPath = methodPath.substring(1);
}
// 获取 params 信息, 先从方法的注解取, 取不到则尝试类的注解
String param = "";
if (CollectionUtils.isNotEmpty(methodParams)) {
param = "?" + String.join("&", methodParams);
} else if (CollectionUtils.isNotEmpty(classParams)) {
param = "?" + String.join("&", classParams);
}
String fullPath = classPath + methodPath + param;
navigationItemList.add(new ApiNavigationItem(psiMethod, httpMethod, fullPath, methodPathInfo));
}
}
return navigationItemList;
}
@NotNull
private List<MethodPathInfo> getMethodList0(@NotNull PsiMethod psiMethod, String qnameOfMapping, HttpMethod httpMethod, PsiClass psiClass) {
List<MethodPathInfo> methodPathList = new ArrayList<>(8);
PsiAnnotation methodMappingAnnotation = psiMethod.getAnnotation(qnameOfMapping);
if (methodMappingAnnotation != null) {
RequestMappingAnnotationInfo methodAnnotationInfoByPojo = PsiAnnotationUtil.getAnnotationInfoByPojo(methodMappingAnnotation, RequestMappingAnnotationInfo.class);
if (httpMethod == null) {
List<String> methodList = methodAnnotationInfoByPojo.getMethod();
if (CollectionUtils.isNotEmpty(methodList)) {
httpMethod = HttpMethod.of(methodList.get(0));
} else {
httpMethod = HttpMethod.ALL;
}
}
List<String> pathList = methodAnnotationInfoByPojo.getValueOrPath();
if (CollectionUtils.isNotEmpty(pathList)) {
for (String methodPath : pathList) {
String psiClassName = psiClass.getName();
String psiMethodName = psiMethod.getName();
String moduleName = getModuleName(psiClass);
String location = buildLocation(moduleName, psiClassName, psiMethodName);
AnnotationHolder psiMethodHolder = AnnotationHolder.getPsiMethodHolder(psiMethod);
CommentInfo commentInfo = psiMethodHolder.getCommentInfo();
String description = commentInfo.getValue("");
List<String> params = methodAnnotationInfoByPojo.getParams();
methodPathList.add(new MethodPathInfo(psiMethod, httpMethod, methodPath, location, description, params));
}
}
}
return methodPathList;
}
@Nullable
private List<String> getRequestMappingPath(PsiAnnotation requestMappingAnnotation) {
List<String> pathList = PsiAnnotationUtil.getAnnotationListValue(requestMappingAnnotation, "path", null);
if (CollectionUtils.isEmpty(pathList)) {
pathList = PsiAnnotationUtil.getAnnotationListValue(requestMappingAnnotation, "value", null);
}
return pathList;
}
@Nullable
private String getModuleName(@NotNull PsiClass psiClass) {
Module module = ModuleUtil.findModuleForPsiElement(psiClass);
return module != null ? module.getName() : null;
}
private String buildLocation(@Nullable String moduleName, @NotNull String className, @NotNull String methodName) {
if (StringUtils.isNotBlank(moduleName)) {
return "[" + moduleName + "] " + className + "#" + methodName;
}
return className + "#" + methodName;
}
}