From 995e376cbef816e2f8153c94d1add6cb4f972533 Mon Sep 17 00:00:00 2001 From: Peter Gromov Date: Mon, 8 Jun 2020 13:49:31 +0200 Subject: [PATCH] introduce CachedValuesManager.getProjectPsiDependentCache, convert a couple of usages GitOrigin-RevId: 4943fce8b6bd685f80b90e21389509fd5683974d --- .../java-psi-api/src/com/intellij/psi/LambdaUtil.java | 3 +-- .../intellij/psi/impl/source/PsiTypeElementImpl.java | 3 +-- .../psi/impl/source/resolve/JavaResolveCache.java | 5 +---- .../com/intellij/psi/util/CachedValuesManager.java | 11 +++++++++++ .../intellij/util/CachedValueStabilityChecker.java | 4 ++-- 5 files changed, 16 insertions(+), 10 deletions(-) diff --git a/java/java-psi-api/src/com/intellij/psi/LambdaUtil.java b/java/java-psi-api/src/com/intellij/psi/LambdaUtil.java index 5a0520e24ac8..e6021cb6c052 100644 --- a/java/java-psi-api/src/com/intellij/psi/LambdaUtil.java +++ b/java/java-psi-api/src/com/intellij/psi/LambdaUtil.java @@ -157,8 +157,7 @@ public class LambdaUtil { @Contract("null -> null") public static @Nullable MethodSignature getFunction(final PsiClass psiClass) { if (isPlainInterface(psiClass)) { - return CachedValuesManager.getCachedValue(psiClass, () -> CachedValueProvider.Result - .create(calcFunction(psiClass), PsiModificationTracker.MODIFICATION_COUNT)); + return CachedValuesManager.getProjectPsiDependentCache(psiClass, () -> calcFunction(psiClass)); } return null; } diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/PsiTypeElementImpl.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/PsiTypeElementImpl.java index 5560d1471baa..1387159a8fa6 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/source/PsiTypeElementImpl.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/PsiTypeElementImpl.java @@ -71,8 +71,7 @@ public class PsiTypeElementImpl extends CompositePsiElement implements PsiTypeEl } private TypeInfo getTypeInfo() { - return CachedValuesManager.getCachedValue( - this, () -> CachedValueProvider.Result.create(calculateTypeInfo(), PsiModificationTracker.MODIFICATION_COUNT)); + return CachedValuesManager.getProjectPsiDependentCache(this, this::calculateTypeInfo); } private @NotNull TypeInfo calculateTypeInfo() { diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/JavaResolveCache.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/JavaResolveCache.java index 53a01677256a..6cfc2a256b15 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/JavaResolveCache.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/JavaResolveCache.java @@ -16,9 +16,7 @@ import com.intellij.psi.impl.AnyPsiChangeListener; import com.intellij.psi.impl.PsiManagerImpl; import com.intellij.psi.impl.source.resolve.graphInference.PsiPolyExpressionUtil; import com.intellij.psi.infos.MethodCandidateInfo; -import com.intellij.psi.util.CachedValueProvider; import com.intellij.psi.util.CachedValuesManager; -import com.intellij.psi.util.PsiModificationTracker; import com.intellij.util.ConcurrencyUtil; import com.intellij.util.Function; import com.intellij.util.containers.ContainerUtil; @@ -64,8 +62,7 @@ public class JavaResolveCache { return f.fun(expr); } - return CachedValuesManager.getCachedValue(expr, () -> - CachedValueProvider.Result.create(f.fun(expr), PsiModificationTracker.MODIFICATION_COUNT)); + return CachedValuesManager.getProjectPsiDependentCache(expr, () -> f.fun(expr)); } @Nullable diff --git a/platform/core-api/src/com/intellij/psi/util/CachedValuesManager.java b/platform/core-api/src/com/intellij/psi/util/CachedValuesManager.java index 36f0d2e71a73..d58a74e87fda 100644 --- a/platform/core-api/src/com/intellij/psi/util/CachedValuesManager.java +++ b/platform/core-api/src/com/intellij/psi/util/CachedValuesManager.java @@ -14,6 +14,7 @@ import org.jetbrains.annotations.Nullable; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import java.util.function.Supplier; /** * A service used to create and store {@link CachedValue} objects.

@@ -119,6 +120,16 @@ public abstract class CachedValuesManager { return getCachedValue(psi, getKeyForClass(provider.getClass(), globalKeyForProvider), provider); } + /** + * @return a cached value computed via the given provider, + * associated with the given PSI element, + * recalculated after any PSI change in the project. + */ + public static T getProjectPsiDependentCache(@NotNull PsiElement psi, @NotNull Supplier provider) { + return getCachedValue(psi, getKeyForClass(provider.getClass(), globalKeyForProvider), () -> + CachedValueProvider.Result.create(provider.get(), PsiModificationTracker.MODIFICATION_COUNT)); + } + /** * Create a cached value with the given provider and non-tracked return value, store it in PSI element's user data. If it's already stored, reuse it. * The passed cached value provider may only depend on the passed PSI element and project/application components/services, diff --git a/platform/core-impl/src/com/intellij/util/CachedValueStabilityChecker.java b/platform/core-impl/src/com/intellij/util/CachedValueStabilityChecker.java index 9ff067cdce40..33af44b0ab07 100644 --- a/platform/core-impl/src/com/intellij/util/CachedValueStabilityChecker.java +++ b/platform/core-impl/src/com/intellij/util/CachedValueStabilityChecker.java @@ -5,7 +5,6 @@ import com.intellij.openapi.application.Application; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.application.impl.ApplicationInfoImpl; import com.intellij.openapi.diagnostic.Logger; -import com.intellij.openapi.util.Computable; import com.intellij.openapi.util.Key; import com.intellij.psi.util.CachedValueProvider; import com.intellij.util.containers.ConcurrentFactoryMap; @@ -20,6 +19,7 @@ import java.util.List; import java.util.Objects; import java.util.Set; import java.util.concurrent.ConcurrentMap; +import java.util.function.Supplier; /** * This class checks the contract described in {@link com.intellij.psi.util.CachedValue} documentation, that its @@ -171,7 +171,7 @@ final class CachedValueStabilityChecker { Class superclass = clazz.getSuperclass(); if (superclass == null) return false; - if ((o instanceof Computable || o instanceof Function || o instanceof java.util.function.Function) && + if ((o instanceof Supplier || o instanceof Function || o instanceof java.util.function.Function) && Object.class.equals(clazz.getSuperclass())) { return true; }