|
| 1 | +package org.digma.intellij.plugin.idea.psi.java; |
| 2 | + |
| 3 | +import com.intellij.openapi.diagnostic.Logger; |
| 4 | +import com.intellij.openapi.project.Project; |
| 5 | +import com.intellij.psi.JavaPsiFacade; |
| 6 | +import com.intellij.psi.PsiAnnotation; |
| 7 | +import com.intellij.psi.PsiClass; |
| 8 | +import com.intellij.psi.PsiFile; |
| 9 | +import com.intellij.psi.PsiMethod; |
| 10 | +import com.intellij.psi.search.GlobalSearchScope; |
| 11 | +import com.intellij.psi.search.searches.AnnotatedElementsSearch; |
| 12 | +import com.intellij.util.Query; |
| 13 | +import org.digma.intellij.plugin.log.Log; |
| 14 | +import org.digma.intellij.plugin.model.discovery.DocumentInfo; |
| 15 | +import org.digma.intellij.plugin.model.discovery.EndpointInfo; |
| 16 | +import org.digma.intellij.plugin.model.discovery.MethodInfo; |
| 17 | +import org.jetbrains.annotations.NotNull; |
| 18 | +import org.jetbrains.annotations.Nullable; |
| 19 | + |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Objects; |
| 23 | +import java.util.stream.Collectors; |
| 24 | + |
| 25 | +public class SpringBootFramework implements IEndpointDiscovery { |
| 26 | + |
| 27 | + private static final Logger LOGGER = Logger.getInstance(SpringBootFramework.class); |
| 28 | + private static final String CONTROLLER_ANNOTATION_STR = "org.springframework.stereotype.Controller"; |
| 29 | + private static final String REST_CONTROLLER_ANNOTATION_STR = "org.springframework.web.bind.annotation.RestController"; |
| 30 | + private static final String HTTP_DELETE_ANNOTATION_STR = "org.springframework.web.bind.annotation.DeleteMapping"; |
| 31 | + private static final String HTTP_GET_ANNOTATION_STR = "org.springframework.web.bind.annotation.GetMapping"; |
| 32 | + private static final String HTTP_PATCH_ANNOTATION_STR = "org.springframework.web.bind.annotation.PatchMapping"; |
| 33 | + private static final String HTTP_POST_ANNOTATION_STR = "org.springframework.web.bind.annotation.PostMapping"; |
| 34 | + private static final String HTTP_PUT_ANNOTATION_STR = "org.springframework.web.bind.annotation.PutMapping"; |
| 35 | + private static final String HTTP_REQUEST_MAPPING_ANNOTATION_STR = "org.springframework.web.bind.annotation.RequestMapping"; |
| 36 | + private static final List<String> HTTP_METHODS_ANNOTATION_STR_LIST = List.of( |
| 37 | + HTTP_DELETE_ANNOTATION_STR, HTTP_GET_ANNOTATION_STR, HTTP_PATCH_ANNOTATION_STR, HTTP_POST_ANNOTATION_STR, HTTP_PUT_ANNOTATION_STR, HTTP_REQUEST_MAPPING_ANNOTATION_STR); |
| 38 | + |
| 39 | + // related to RequestMapping, one of those attributes should be filled |
| 40 | + private static final List<String> ATTRIBUTES_OF_PATH = List.of("value", "path"); |
| 41 | + // |
| 42 | + private static final Map<String, String> MAP_HTTP_XXX_ANNOT_2_HTTP_METHOD; |
| 43 | + |
| 44 | + static { |
| 45 | + MAP_HTTP_XXX_ANNOT_2_HTTP_METHOD = Map.of( |
| 46 | + HTTP_DELETE_ANNOTATION_STR, "DELETE", |
| 47 | + HTTP_GET_ANNOTATION_STR, "GET", |
| 48 | + HTTP_PATCH_ANNOTATION_STR, "PATCH", |
| 49 | + HTTP_POST_ANNOTATION_STR, "POST", |
| 50 | + HTTP_PUT_ANNOTATION_STR, "PUT" |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + private final Project project; |
| 55 | + |
| 56 | + // late init |
| 57 | + private boolean lateInitAlready = false; |
| 58 | + private PsiClass controllerAnnotationClass; |
| 59 | + private List<JavaAnnotation> httpMethodsAnnotations; |
| 60 | + |
| 61 | + public SpringBootFramework(Project project) { |
| 62 | + this.project = project; |
| 63 | + } |
| 64 | + |
| 65 | + private void lateInit() { |
| 66 | + if (lateInitAlready) return; |
| 67 | + |
| 68 | + JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); |
| 69 | + controllerAnnotationClass = psiFacade.findClass(CONTROLLER_ANNOTATION_STR, GlobalSearchScope.allScope(project)); |
| 70 | + initHttpMethodAnnotations(psiFacade); |
| 71 | + |
| 72 | + lateInitAlready = true; |
| 73 | + } |
| 74 | + |
| 75 | + private void initHttpMethodAnnotations(JavaPsiFacade psiFacade) { |
| 76 | + httpMethodsAnnotations = HTTP_METHODS_ANNOTATION_STR_LIST.stream() |
| 77 | + .map(currFqn -> { |
| 78 | + PsiClass psiClass = psiFacade.findClass(currFqn, GlobalSearchScope.allScope(project)); |
| 79 | + if (psiClass == null) return null; |
| 80 | + return new JavaAnnotation(currFqn, psiClass); |
| 81 | + }) |
| 82 | + .filter(Objects::nonNull) |
| 83 | + .collect(Collectors.toUnmodifiableList()); |
| 84 | + } |
| 85 | + |
| 86 | + private boolean isSpringBootWebRelevant() { |
| 87 | + return controllerAnnotationClass != null; |
| 88 | + } |
| 89 | + |
| 90 | + public void endpointDiscovery(@NotNull PsiFile psiFile, @NotNull DocumentInfo documentInfo) { |
| 91 | + lateInit(); |
| 92 | + if (!isSpringBootWebRelevant()) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + httpMethodsAnnotations.forEach(currAnnotation -> { |
| 97 | + Query<PsiMethod> psiMethodsInFile = AnnotatedElementsSearch.searchPsiMethods(currAnnotation.getPsiClass(), GlobalSearchScope.fileScope(psiFile)); |
| 98 | + |
| 99 | + for (PsiMethod currPsiMethod : psiMethodsInFile) { |
| 100 | + final PsiAnnotation mappingPsiAnnotationOnMethod = currPsiMethod.getAnnotation(currAnnotation.getClassNameFqn()); |
| 101 | + if (mappingPsiAnnotationOnMethod == null) { |
| 102 | + continue; // very unlikely |
| 103 | + } |
| 104 | + |
| 105 | + final PsiClass controllerClass = currPsiMethod.getContainingClass(); |
| 106 | + if (controllerClass == null) { |
| 107 | + continue; // very unlikely |
| 108 | + } |
| 109 | + |
| 110 | + if (!JavaPsiUtils.hasOneOfAnnotations(controllerClass, CONTROLLER_ANNOTATION_STR, REST_CONTROLLER_ANNOTATION_STR)) { |
| 111 | + continue; // skip this method, since its class is not a controller (or rest controller) |
| 112 | + } |
| 113 | + |
| 114 | + final PsiAnnotation controllerReqMappingAnnotation = controllerClass.getAnnotation(HTTP_REQUEST_MAPPING_ANNOTATION_STR); |
| 115 | + String endpointUriPrefix = ""; |
| 116 | + if (controllerReqMappingAnnotation != null) { |
| 117 | + endpointUriPrefix = JavaLanguageUtils.getValueOfFirstMatchingAnnotationAttribute(controllerReqMappingAnnotation, ATTRIBUTES_OF_PATH, ""); |
| 118 | + } |
| 119 | + final String endpointUriSuffix = JavaLanguageUtils.getValueOfFirstMatchingAnnotationAttribute(mappingPsiAnnotationOnMethod, ATTRIBUTES_OF_PATH, ""); |
| 120 | + |
| 121 | + String httpMethodName = evalHttpMethod(mappingPsiAnnotationOnMethod, controllerReqMappingAnnotation); |
| 122 | + if (httpMethodName == null) { |
| 123 | + continue; // not likely |
| 124 | + } |
| 125 | + |
| 126 | + String httpEndpointCodeObjectId = createHttpEndpointCodeObjectId(httpMethodName, endpointUriPrefix, endpointUriSuffix); |
| 127 | + |
| 128 | + String methodCodeObjectId = JavaLanguageUtils.createJavaMethodCodeObjectId(currPsiMethod); |
| 129 | + EndpointInfo endpointInfo = new EndpointInfo(httpEndpointCodeObjectId, methodCodeObjectId, documentInfo.getFileUri()); |
| 130 | + Log.log(LOGGER::debug, "Found endpoint info '{}' for method '{}'", endpointInfo.getId(), endpointInfo.getContainingMethodId()); |
| 131 | + |
| 132 | + MethodInfo methodInfo = documentInfo.getMethods().get(endpointInfo.getContainingMethodId()); |
| 133 | + //this method must exist in the document info |
| 134 | + Objects.requireNonNull(methodInfo, "method info " + endpointInfo.getContainingMethodId() + " must exist in DocumentInfo for " + documentInfo.getFileUri()); |
| 135 | + methodInfo.addEndpoint(endpointInfo); |
| 136 | + } |
| 137 | + }); |
| 138 | + } |
| 139 | + |
| 140 | + @Nullable |
| 141 | + private String evalHttpMethod(@NotNull PsiAnnotation mappingPsiAnnotationOnMethod, @Nullable PsiAnnotation controllerReqMappingAnnotation) { |
| 142 | + |
| 143 | + String mappedValue = MAP_HTTP_XXX_ANNOT_2_HTTP_METHOD.get(mappingPsiAnnotationOnMethod.getQualifiedName()); |
| 144 | + if (mappedValue != null) { |
| 145 | + return mappedValue; |
| 146 | + } |
| 147 | + |
| 148 | + if (HTTP_REQUEST_MAPPING_ANNOTATION_STR.equals(mappingPsiAnnotationOnMethod.getQualifiedName())) { |
| 149 | + // trying for attribute "method" in on method annotation |
| 150 | + { |
| 151 | + String value = JavaLanguageUtils.getPsiAnnotationAttributeValue(mappingPsiAnnotationOnMethod, "method"); |
| 152 | + if (value != null) { |
| 153 | + return value.toUpperCase(); |
| 154 | + } |
| 155 | + } |
| 156 | + if (controllerReqMappingAnnotation != null) { |
| 157 | + // fallback to attribute "method" in on controller annotation |
| 158 | + String value = JavaLanguageUtils.getPsiAnnotationAttributeValue(controllerReqMappingAnnotation, "method"); |
| 159 | + if (value != null) { |
| 160 | + return value.toUpperCase(); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + return null; |
| 166 | + } |
| 167 | + |
| 168 | + @NotNull |
| 169 | + protected static String createHttpEndpointCodeObjectId(@NotNull String httpMethod, @Nullable String endpointUriPrefix, @Nullable String endpointUriSuffix) { |
| 170 | + // value for example : 'epHTTP:HTTP GET /books/get' |
| 171 | + return "" + |
| 172 | + // digma part |
| 173 | + "epHTTP:" + "HTTP " + httpMethod.toUpperCase() + " " + |
| 174 | + // Spring Web part |
| 175 | + JavaUtils.combineUri(endpointUriPrefix, endpointUriSuffix); |
| 176 | + } |
| 177 | + |
| 178 | +} |
0 commit comments