[java] Reduce service requests in external annotation query

GitOrigin-RevId: a0472dfc29ca08e256f64c603d8a25c347c29d4b
This commit is contained in:
Tagir Valeev
2024-09-17 08:33:01 +02:00
committed by intellij-monorepo-bot
parent 0ddb0d84b3
commit 6e2074418d
3 changed files with 43 additions and 15 deletions

View File

@@ -128,29 +128,21 @@ public class AnnotationUtil {
annotationNames -> {
PsiUtilCore.ensureValid(listOwner);
final Project project = listOwner.getProject();
List<PsiAnnotation> annotations = null;
final ExternalAnnotationsManager externalAnnotationsManager = ExternalAnnotationsManager.getInstance(project);
for (String annotationName : annotationNames) {
List<PsiAnnotation> externalAnnotations = externalAnnotationsManager.findExternalAnnotations(listOwner, annotationName);
if (!externalAnnotations.isEmpty()) {
if (annotations == null) {
annotations = new SmartList<>();
}
annotations.addAll(externalAnnotations);
}
}
List<PsiAnnotation> externalAnnotations = externalAnnotationsManager.findExternalAnnotations(listOwner, annotationNames);
final InferredAnnotationsManager inferredAnnotationsManager = InferredAnnotationsManager.getInstance(project);
List<PsiAnnotation> inferredAnnotations = null;
for (String annotationName : annotationNames) {
final PsiAnnotation annotation = inferredAnnotationsManager.findInferredAnnotation(listOwner, annotationName);
if (annotation != null) {
if (annotations == null) {
annotations = new SmartList<>();
if (inferredAnnotations == null) {
inferredAnnotations = new SmartList<>();
}
annotations.add(annotation);
inferredAnnotations.add(annotation);
}
}
return annotations;
return inferredAnnotations == null ? externalAnnotations : ContainerUtil.concat(externalAnnotations, inferredAnnotations);
}
);
return CachedValueProvider.Result.create(value, PsiModificationTracker.MODIFICATION_COUNT);

View File

@@ -4,10 +4,13 @@ package com.intellij.codeInsight;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.messages.Topic;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public abstract class ExternalAnnotationsManager {
@@ -54,7 +57,7 @@ public abstract class ExternalAnnotationsManager {
/**
* Returns external annotations with fully qualified name of {@code annotationFQN}
* associated with {@code listOwner}.
*
* <p>
* Multiple results may be returned for repeatable annotations and annotations
* from several external annotations roots.
*
@@ -64,6 +67,25 @@ public abstract class ExternalAnnotationsManager {
*/
public abstract @NotNull List<PsiAnnotation> findExternalAnnotations(@NotNull PsiModifierListOwner listOwner, @NotNull String annotationFQN);
/**
* Returns external annotations with fully qualified names contained in {@code annotationFQNs}
* associated with {@code listOwner}.
* <p>
* Multiple results may be returned for repeatable annotations and annotations
* from several external annotations roots.
*
* @param listOwner API element to return external annotations of
* @param annotationFQNs collection of fully qualified names of the annotations to search for
* @return external annotations of the {@code listOwner}
*/
public @NotNull List<PsiAnnotation> findExternalAnnotations(@NotNull PsiModifierListOwner listOwner, @NotNull Collection<String> annotationFQNs) {
PsiAnnotation[] annotations = findExternalAnnotations(listOwner);
//There's an implementation in Kotlin tests which violates the new contract of findExternalAnnotations(listOwner) and returns null
//noinspection ConstantValue
return annotations == null ? Collections.emptyList() :
ContainerUtil.filter(annotations, annotation -> annotationFQNs.contains(annotation.getQualifiedName()));
}
// Method used in Kotlin plugin
public abstract boolean isExternalAnnotationWritable(@NotNull PsiModifierListOwner listOwner, @NotNull String annotationFQN);