|
8 | 8 | import com.intellij.psi.PsiFile; |
9 | 9 | import com.intellij.psi.PsiMethod; |
10 | 10 | import com.intellij.psi.search.GlobalSearchScope; |
11 | | -import com.intellij.psi.search.searches.AnnotatedElementsSearch; |
12 | | -import com.intellij.util.Query; |
13 | 11 | import org.digma.intellij.plugin.log.Log; |
14 | 12 | import org.digma.intellij.plugin.model.discovery.DocumentInfo; |
15 | 13 | import org.digma.intellij.plugin.model.discovery.EndpointInfo; |
16 | 14 | import org.digma.intellij.plugin.model.discovery.MethodInfo; |
17 | 15 | import org.jetbrains.annotations.NotNull; |
18 | 16 |
|
| 17 | +import java.util.Arrays; |
19 | 18 | import java.util.List; |
20 | 19 | import java.util.Objects; |
21 | 20 | import java.util.stream.Collectors; |
@@ -77,39 +76,48 @@ public void endpointDiscovery(@NotNull PsiFile psiFile, @NotNull DocumentInfo do |
77 | 76 | return; |
78 | 77 | } |
79 | 78 |
|
80 | | - httpMethodsAnnotations.forEach(currAnnotation -> { |
81 | | - Query<PsiMethod> psiMethodsOfHttpMethodInFile = AnnotatedElementsSearch.searchPsiMethods(currAnnotation.getPsiClass(), GlobalSearchScope.fileScope(psiFile)); |
| 79 | + List<PsiClass> allClassesInFile = JavaPsiUtils.getClassesWithin(psiFile); |
| 80 | + for (PsiClass currClass : allClassesInFile) { |
| 81 | + final PsiAnnotation controllerPathAnnotation = JavaPsiUtils.findNearestAnnotation(currClass, JAX_RS_PATH_ANNOTATION_STR); |
82 | 82 |
|
83 | | - for (PsiMethod currPsiMethod : psiMethodsOfHttpMethodInFile) { |
84 | | - PsiClass controllerClass = currPsiMethod.getContainingClass(); |
85 | | - if (controllerClass == null) { |
86 | | - continue; // very unlikely |
87 | | - } |
88 | | - PsiAnnotation controllerAnnotation = controllerClass.getAnnotation(JAX_RS_PATH_ANNOTATION_STR); |
89 | | - if (controllerAnnotation == null) { |
90 | | - continue; // skip this method, since its class is not a controller |
91 | | - } |
92 | | - String endpointUriPrefix = JavaLanguageUtils.getPsiAnnotationAttributeValue(controllerAnnotation, "value"); |
93 | | - |
94 | | - String endpointUriSuffix = ""; |
95 | | - PsiAnnotation methodPathAnnotation = currPsiMethod.getAnnotation(JAX_RS_PATH_ANNOTATION_STR); // optional annotation on method |
96 | | - if (methodPathAnnotation != null) { |
97 | | - endpointUriSuffix = JavaLanguageUtils.getPsiAnnotationAttributeValue(methodPathAnnotation, "value"); |
| 83 | + List<PsiMethod> methodsInClass = Arrays.asList(currClass.getMethods()); |
| 84 | + for (PsiMethod currPsiMethod : methodsInClass) { |
| 85 | + final PsiAnnotation methodPathAnnotation = JavaPsiUtils.findNearestAnnotation(currPsiMethod, JAX_RS_PATH_ANNOTATION_STR); |
| 86 | + if (methodPathAnnotation == null && controllerPathAnnotation == null) { |
| 87 | + continue; // skip since could not find annotation of @Path, in either class and or method |
98 | 88 | } |
99 | 89 |
|
100 | | - String endpointFullUri = JavaUtils.combineUri(endpointUriPrefix, endpointUriSuffix); |
| 90 | + for (JavaAnnotation currExpectedAnnotation : httpMethodsAnnotations) { |
| 91 | + PsiAnnotation httpMethodAnnotation = JavaPsiUtils.findNearestAnnotation(currPsiMethod, currExpectedAnnotation.getClassNameFqn()); |
| 92 | + if (httpMethodAnnotation == null) { |
| 93 | + continue; // skipping since could not find annotation of HTTP Method, such as @GET |
| 94 | + } |
| 95 | + String endpointFullUri = combinePaths(controllerPathAnnotation, methodPathAnnotation); |
101 | 96 |
|
102 | | - String httpEndpointCodeObjectId = createHttpEndpointCodeObjectId(currAnnotation, endpointFullUri); |
| 97 | + String httpEndpointCodeObjectId = createHttpEndpointCodeObjectId(currExpectedAnnotation, endpointFullUri); |
103 | 98 |
|
104 | | - EndpointInfo endpointInfo = new EndpointInfo(httpEndpointCodeObjectId, JavaLanguageUtils.createJavaMethodCodeObjectId(currPsiMethod), documentInfo.getFileUri()); |
105 | | - Log.log(LOGGER::debug, "Found endpoint info '{}' for method '{}'", endpointInfo.getId(), endpointInfo.getContainingMethodId()); |
| 99 | + EndpointInfo endpointInfo = new EndpointInfo(httpEndpointCodeObjectId, JavaLanguageUtils.createJavaMethodCodeObjectId(currPsiMethod), documentInfo.getFileUri()); |
| 100 | + Log.log(LOGGER::debug, "Found endpoint info '{}' for method '{}'", endpointInfo.getId(), endpointInfo.getContainingMethodId()); |
106 | 101 |
|
107 | | - MethodInfo methodInfo = documentInfo.getMethods().get(endpointInfo.getContainingMethodId()); |
108 | | - //this method must exist in the document info |
109 | | - Objects.requireNonNull(methodInfo, "method info " + endpointInfo.getContainingMethodId() + " must exist in DocumentInfo for " + documentInfo.getFileUri()); |
110 | | - methodInfo.addEndpoint(endpointInfo); |
| 102 | + MethodInfo methodInfo = documentInfo.getMethods().get(endpointInfo.getContainingMethodId()); |
| 103 | + //this method must exist in the document info |
| 104 | + Objects.requireNonNull(methodInfo, "method info " + endpointInfo.getContainingMethodId() + " must exist in DocumentInfo for " + documentInfo.getFileUri()); |
| 105 | + methodInfo.addEndpoint(endpointInfo); |
| 106 | + } |
111 | 107 | } |
112 | | - }); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + protected static String combinePaths(PsiAnnotation annotOfPrefix, PsiAnnotation annotOfSuffix) { |
| 112 | + String prefixStr = ""; |
| 113 | + if (annotOfPrefix != null) { |
| 114 | + prefixStr = JavaLanguageUtils.getPsiAnnotationAttributeValue(annotOfPrefix, "value"); |
| 115 | + } |
| 116 | + String suffixStr = ""; |
| 117 | + if (annotOfSuffix != null) { |
| 118 | + suffixStr = JavaLanguageUtils.getPsiAnnotationAttributeValue(annotOfSuffix, "value"); |
| 119 | + } |
| 120 | + return JavaUtils.combineUri(prefixStr, suffixStr); |
113 | 121 | } |
114 | 122 |
|
115 | 123 | @NotNull |
|
0 commit comments