|
| 1 | +package org.digma.intellij.plugin.idea.psi.java; |
| 2 | + |
| 3 | +import com.intellij.lang.jvm.annotation.JvmAnnotationAttribute; |
| 4 | +import com.intellij.openapi.diagnostic.Logger; |
| 5 | +import com.intellij.openapi.project.Project; |
| 6 | +import com.intellij.psi.JavaPsiFacade; |
| 7 | +import com.intellij.psi.PsiAnnotation; |
| 8 | +import com.intellij.psi.PsiClass; |
| 9 | +import com.intellij.psi.PsiFile; |
| 10 | +import com.intellij.psi.PsiMethod; |
| 11 | +import com.intellij.psi.search.GlobalSearchScope; |
| 12 | +import com.intellij.psi.search.searches.AnnotatedElementsSearch; |
| 13 | +import com.intellij.util.Query; |
| 14 | +import org.digma.intellij.plugin.log.Log; |
| 15 | +import org.digma.intellij.plugin.model.discovery.DocumentInfo; |
| 16 | +import org.digma.intellij.plugin.model.discovery.EndpointInfo; |
| 17 | +import org.digma.intellij.plugin.model.discovery.MethodInfo; |
| 18 | +import org.jetbrains.annotations.NotNull; |
| 19 | +import org.jetbrains.annotations.Nullable; |
| 20 | + |
| 21 | +import java.util.List; |
| 22 | +import java.util.Objects; |
| 23 | +import java.util.stream.Collectors; |
| 24 | + |
| 25 | +public class MicronautFramework { |
| 26 | + |
| 27 | + private static final Logger LOGGER = Logger.getInstance(MicronautFramework.class); |
| 28 | + private static final String CONTROLLER_ANNOTATION_STR = "io.micronaut.http.annotation.Controller"; |
| 29 | + private static final String HTTP_DELETE_ANNOTATION_STR = "io.micronaut.http.annotation.Delete"; |
| 30 | + private static final String HTTP_GET_ANNOTATION_STR = "io.micronaut.http.annotation.Get"; |
| 31 | + private static final String HTTP_HEAD_ANNOTATION_STR = "io.micronaut.http.annotation.Head"; |
| 32 | + private static final String HTTP_OPTIONS_ANNOTATION_STR = "io.micronaut.http.annotation.Options"; |
| 33 | + private static final String HTTP_PATCH_ANNOTATION_STR = "io.micronaut.http.annotation.Patch"; |
| 34 | + private static final String HTTP_POST_ANNOTATION_STR = "io.micronaut.http.annotation.Post"; |
| 35 | + private static final String HTTP_PUT_ANNOTATION_STR = "io.micronaut.http.annotation.Put"; |
| 36 | + private static final String HTTP_TRACE_ANNOTATION_STR = "io.micronaut.http.annotation.Trace"; |
| 37 | + private static final List<String> HTTP_METHODS_ANNOTATION_STR_LIST = List.of( |
| 38 | + HTTP_DELETE_ANNOTATION_STR, HTTP_GET_ANNOTATION_STR, HTTP_HEAD_ANNOTATION_STR, HTTP_OPTIONS_ANNOTATION_STR, |
| 39 | + HTTP_PATCH_ANNOTATION_STR, HTTP_POST_ANNOTATION_STR, HTTP_PUT_ANNOTATION_STR, HTTP_TRACE_ANNOTATION_STR); |
| 40 | + |
| 41 | + private static final List<String> VALUE_OR_URI = List.of("value", "uri"); |
| 42 | + |
| 43 | + private final Project project; |
| 44 | + |
| 45 | + // late init |
| 46 | + private boolean lateInitAlready = false; |
| 47 | + private PsiClass controllerAnnotationClass; |
| 48 | + private List<JavaAnnotation> httpMethodsAnnotations; |
| 49 | + |
| 50 | + public MicronautFramework(Project project) { |
| 51 | + this.project = project; |
| 52 | + } |
| 53 | + |
| 54 | + private void lateInit() { |
| 55 | + if (lateInitAlready) return; |
| 56 | + |
| 57 | + JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); |
| 58 | + controllerAnnotationClass = psiFacade.findClass(CONTROLLER_ANNOTATION_STR, GlobalSearchScope.allScope(project)); |
| 59 | + initHttpMethodAnnotations(psiFacade); |
| 60 | + |
| 61 | + lateInitAlready = true; |
| 62 | + } |
| 63 | + |
| 64 | + private void initHttpMethodAnnotations(JavaPsiFacade psiFacade) { |
| 65 | + httpMethodsAnnotations = HTTP_METHODS_ANNOTATION_STR_LIST.stream() |
| 66 | + .map(currFqn -> { |
| 67 | + PsiClass psiClass = psiFacade.findClass(currFqn, GlobalSearchScope.allScope(project)); |
| 68 | + if (psiClass == null) return null; |
| 69 | + return new JavaAnnotation(currFqn, psiClass); |
| 70 | + }) |
| 71 | + .filter(Objects::nonNull) |
| 72 | + .collect(Collectors.toUnmodifiableList()); |
| 73 | + } |
| 74 | + |
| 75 | + private boolean isMicronautHttpRelevant() { |
| 76 | + return controllerAnnotationClass != null; |
| 77 | + } |
| 78 | + |
| 79 | + public void endpointDiscovery(@NotNull PsiFile psiFile, @NotNull DocumentInfo documentInfo) { |
| 80 | + lateInit(); |
| 81 | + if (!isMicronautHttpRelevant()) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + httpMethodsAnnotations.forEach(currAnnotation -> { |
| 86 | + Query<PsiMethod> psiMethodsInFile = AnnotatedElementsSearch.searchPsiMethods(currAnnotation.getPsiClass(), GlobalSearchScope.fileScope(psiFile)); |
| 87 | + |
| 88 | + for (PsiMethod currPsiMethod : psiMethodsInFile) { |
| 89 | + PsiClass controllerClass = currPsiMethod.getContainingClass(); |
| 90 | + if (controllerClass == null) { |
| 91 | + continue; // very unlikely |
| 92 | + } |
| 93 | + PsiAnnotation controllerAnnotation = controllerClass.getAnnotation(CONTROLLER_ANNOTATION_STR); |
| 94 | + if (controllerAnnotation == null) { |
| 95 | + continue; // skip this method, since its class is not a controller |
| 96 | + } |
| 97 | + String endpointUriPrefix = JavaLanguageUtils.getPsiAnnotationAttributeValue(controllerAnnotation, "value"); |
| 98 | + |
| 99 | + String methodCodeObjectId = JavaLanguageUtils.createJavaMethodCodeObjectId(currPsiMethod); |
| 100 | + String httpEndpointCodeObjectId = createHttpEndpointCodeObjectId(currPsiMethod, currAnnotation, endpointUriPrefix); |
| 101 | + if (httpEndpointCodeObjectId == null) { |
| 102 | + continue; // skip this method, since endpoint value could not be determined |
| 103 | + } |
| 104 | + |
| 105 | + EndpointInfo endpointInfo = new EndpointInfo(httpEndpointCodeObjectId, methodCodeObjectId, documentInfo.getFileUri()); |
| 106 | + Log.log(LOGGER::debug, "Found endpoint info '{}' for method '{}'", endpointInfo.getId(), endpointInfo.getContainingMethodId()); |
| 107 | + |
| 108 | + MethodInfo methodInfo = documentInfo.getMethods().get(endpointInfo.getContainingMethodId()); |
| 109 | + //this method must exist in the document info |
| 110 | + Objects.requireNonNull(methodInfo, "method info " + endpointInfo.getContainingMethodId() + " must exist in DocumentInfo for " + documentInfo.getFileUri()); |
| 111 | + methodInfo.addEndpoint(endpointInfo); |
| 112 | + } |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + @Nullable |
| 117 | + protected static String createHttpEndpointCodeObjectId(PsiMethod psiMethod, JavaAnnotation httpMethodAnnotation, String endpointUriPrefix) { |
| 118 | + PsiAnnotation httpPsiAnnotation = psiMethod.getAnnotation(httpMethodAnnotation.getClassNameFqn()); |
| 119 | + List<JvmAnnotationAttribute> annotationAttributes = httpPsiAnnotation.getAttributes(); |
| 120 | + |
| 121 | + String endpointUriSuffix = "/"; |
| 122 | + for (JvmAnnotationAttribute curr : annotationAttributes) { |
| 123 | + // taking the first attribute, either "value" or "uri" - that's how micronaut behave if both exists |
| 124 | + if (VALUE_OR_URI.contains(curr.getAttributeName())) { |
| 125 | + endpointUriSuffix = JavaLanguageUtils.getPsiAnnotationAttributeValue(httpPsiAnnotation, curr.getAttributeName()); |
| 126 | + if (endpointUriSuffix == null) { |
| 127 | + Log.log(LOGGER::debug, "cannot create http endpoint for method '{}' since could not extract attribute value for name '{}' from annotation '{}'", |
| 128 | + psiMethod.getName(), curr.getAttributeName(), httpMethodAnnotation.getClassNameFqn()); |
| 129 | + return null; // unlikely |
| 130 | + } |
| 131 | + break; // found the first occurrence, and out |
| 132 | + } |
| 133 | + // note: attribute of "uris" is irrelevant |
| 134 | + } |
| 135 | + |
| 136 | + String httpMethodUcase = getHttpMethod(httpMethodAnnotation).toUpperCase(); |
| 137 | + |
| 138 | + // value for example : 'epHTTP:HTTP GET GET - /books/get' |
| 139 | + return "" + |
| 140 | + // digma part |
| 141 | + "epHTTP:" + "HTTP " + httpMethodUcase + " " + |
| 142 | + // Micronaut part |
| 143 | + httpMethodUcase + " - " + JavaUtils.combineUri(endpointUriPrefix, endpointUriSuffix); |
| 144 | + } |
| 145 | + |
| 146 | + @NotNull |
| 147 | + private static String getHttpMethod(JavaAnnotation javaAnnotation) { |
| 148 | + String fqn = javaAnnotation.getClassNameFqn(); |
| 149 | + int lastIndexOfDot = fqn.lastIndexOf('.'); |
| 150 | + if (lastIndexOfDot >= 0) { |
| 151 | + return fqn.substring(lastIndexOfDot + 1); |
| 152 | + } else { |
| 153 | + return fqn; |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments