From 343502125b2a1cbacf5390d5d9f23814369b1eda Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Tue, 24 Sep 2019 14:26:23 +0300 Subject: [PATCH] Rename ResolveInWriteActionManager -> ResolveInDispatchThreadManager --- .../resolve/ModuleResolutionFacadeImpl.kt | 9 ++-- ...r.kt => ResolveInDispatchThreadManager.kt} | 47 ++++++++++--------- .../handlers/KotlinClassifierInsertHandler.kt | 4 +- .../kotlin/idea/core/ShortenReferences.kt | 4 +- .../idea/injection/KotlinLanguageInjector.kt | 8 ++-- idea/resources/META-INF/plugin-common.xml | 2 +- .../KotlinCopyPasteReferenceProcessor.kt | 4 +- .../fixers/KotlinLastLambdaParameterFixer.kt | 4 +- .../idea/quickfix/KotlinQuickFixAction.kt | 4 +- ...bstractLiteralTextToKotlinCopyPasteTest.kt | 6 +-- .../idea/quickfix/AbstractQuickFixTest.kt | 9 ++-- 11 files changed, 51 insertions(+), 50 deletions(-) rename idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/{ResolveInWriteActionManager.kt => ResolveInDispatchThreadManager.kt} (57%) diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/ModuleResolutionFacadeImpl.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/ModuleResolutionFacadeImpl.kt index 885d23dbe3f..e195b4f362b 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/ModuleResolutionFacadeImpl.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/ModuleResolutionFacadeImpl.kt @@ -16,8 +16,6 @@ package org.jetbrains.kotlin.idea.caches.resolve -import com.intellij.openapi.application.ApplicationManager -import com.intellij.openapi.diagnostic.Logger import com.intellij.openapi.project.Project import com.intellij.psi.PsiElement import org.jetbrains.kotlin.analyzer.AnalysisResult @@ -26,7 +24,6 @@ import org.jetbrains.kotlin.container.getService import org.jetbrains.kotlin.container.tryGetService import org.jetbrains.kotlin.descriptors.DeclarationDescriptor import org.jetbrains.kotlin.descriptors.ModuleDescriptor -import org.jetbrains.kotlin.idea.KotlinPluginUtil import org.jetbrains.kotlin.idea.caches.project.IdeaModuleInfo import org.jetbrains.kotlin.idea.project.ResolveElementCache import org.jetbrains.kotlin.idea.resolve.ResolutionFacade @@ -57,7 +54,7 @@ internal class ModuleResolutionFacadeImpl( } override fun analyze(elements: Collection, bodyResolveMode: BodyResolveMode): BindingContext { - ResolveInWriteActionManager.assertNoResolveUnderWriteAction() + ResolveInDispatchThreadManager.assertNoResolveInDispatchThread() if (elements.isEmpty()) return BindingContext.EMPTY val resolveElementCache = getFrontendService(elements.first(), ResolveElementCache::class.java) @@ -65,7 +62,7 @@ internal class ModuleResolutionFacadeImpl( } override fun analyzeWithAllCompilerChecks(elements: Collection): AnalysisResult { - ResolveInWriteActionManager.assertNoResolveUnderWriteAction() + ResolveInDispatchThreadManager.assertNoResolveInDispatchThread() return projectFacade.getAnalysisResultsForElements(elements) } @@ -76,7 +73,7 @@ internal class ModuleResolutionFacadeImpl( bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, declaration] ?: getFrontendService(moduleInfo, AbsentDescriptorHandler::class.java).diagnoseDescriptorNotFound(declaration) } else { - ResolveInWriteActionManager.assertNoResolveUnderWriteAction() + ResolveInDispatchThreadManager.assertNoResolveInDispatchThread() val resolveSession = projectFacade.resolverForElement(declaration).componentProvider.get() resolveSession.resolveToDescriptor(declaration) diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/ResolveInWriteActionManager.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/ResolveInDispatchThreadManager.kt similarity index 57% rename from idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/ResolveInWriteActionManager.kt rename to idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/ResolveInDispatchThreadManager.kt index a4d9584632e..a693be82c2c 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/ResolveInWriteActionManager.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/ResolveInDispatchThreadManager.kt @@ -11,13 +11,13 @@ import com.intellij.openapi.util.registry.Registry import org.jetbrains.annotations.TestOnly /** - * Temporary allow resolve in write action. + * Temporary allow resolve in dispatch thread. * - * All resolve should be banned from write action. This method is needed for the transition period to document + * All resolve should be banned from the UI thread. This method is needed for the transition period to document * places that are not fixed yet. */ -fun allowResolveInWriteAction(runnable: () -> T): T { - return ResolveInWriteActionManager.runWithResolveAllowedInWriteAction(runnable) +fun allowResolveInDispatchThread(runnable: () -> T): T { + return ResolveInDispatchThreadManager.runWithResolveAllowedInDispatchThread(runnable) } /** @@ -28,31 +28,36 @@ fun allowResolveInWriteAction(runnable: () -> T): T { * * (A better check is needed instead that would assert no resolve result are obtained outside of caches.) */ -fun allowResolveExpectedToBeCached(runnable: () -> T): T { - return ResolveInWriteActionManager.runWithResolveAllowedInWriteAction(runnable) +fun allowCachedResolveInDispatchThread(runnable: () -> T): T { + return ResolveInDispatchThreadManager.runWithResolveAllowedInDispatchThread(runnable) } /** - * Force resolve check in tests. + * Force resolve check in tests as it disabled by default. */ @TestOnly -fun forceResolveInWriteActionCheckInTests(errorHandler: (() -> Unit)? = null, runnable: () -> T): T { - return ResolveInWriteActionManager.runWithForceResolveAllowedCheckInTests(errorHandler, runnable) +fun forceCheckForResolveInDispatchThreadInTests(errorHandler: (() -> Unit)? = null, runnable: () -> T): T { + return ResolveInDispatchThreadManager.runWithForceCheckForResolveInDispatchThreadInTests(errorHandler, runnable) } -private const val RESOLVE_IN_WRITE_ACTION_ERROR_MESSAGE = "Resolve is not allowed under the write action!" +private const val RESOLVE_IN_DISPATCH_THREAD_ERROR_MESSAGE = "Resolve is not allowed in dispatch thread!" -class ResolveInWriteActionException(message: String? = null) : IllegalThreadStateException(message ?: RESOLVE_IN_WRITE_ACTION_ERROR_MESSAGE) +class ResolveInDispatchThreadException(message: String? = null) : + IllegalThreadStateException(message ?: RESOLVE_IN_DISPATCH_THREAD_ERROR_MESSAGE) -internal object ResolveInWriteActionManager { - private val LOG = Logger.getInstance(ResolveInWriteActionManager::class.java) +internal object ResolveInDispatchThreadManager { + private val LOG = Logger.getInstance(ResolveInDispatchThreadManager::class.java) - // Should be accessed only from dispatch thread + // Guarded by isDispatchThread check private var isResolveAllowed: Boolean = false + + // Guarded by isDispatchThread check private var isForceCheckInTests: Boolean = false + + // Guarded by isDispatchThread check private var errorHandler: (() -> Unit)? = null - fun assertNoResolveUnderWriteAction() { + internal fun assertNoResolveInDispatchThread() { val application = ApplicationManager.getApplication() ?: return if (!application.isDispatchThread) { return @@ -69,16 +74,16 @@ internal object ResolveInWriteActionManager { return } - throw ResolveInWriteActionException() + throw ResolveInDispatchThreadException() } else { @Suppress("InvalidBundleOrProperty") - if (!Registry.`is`("kotlin.write.resolve.check", false)) return + if (!Registry.`is`("kotlin.dispatch.thread.resolve.check", false)) return - LOG.error(RESOLVE_IN_WRITE_ACTION_ERROR_MESSAGE) + LOG.error(RESOLVE_IN_DISPATCH_THREAD_ERROR_MESSAGE) } } - internal inline fun runWithResolveAllowedInWriteAction(runnable: () -> T): T { + internal inline fun runWithResolveAllowedInDispatchThread(runnable: () -> T): T { val wasSet = if (ApplicationManager.getApplication()?.isDispatchThread == true && !isResolveAllowed) { isResolveAllowed = true @@ -95,8 +100,8 @@ internal object ResolveInWriteActionManager { } } - internal fun runWithForceResolveAllowedCheckInTests(errorHandler: (() -> Unit)?, runnable: () -> T): T { - ApplicationManager.getApplication()?.assertIsDispatchThread() + internal fun runWithForceCheckForResolveInDispatchThreadInTests(errorHandler: (() -> Unit)?, runnable: () -> T): T { + ApplicationManager.getApplication()?.assertIsDispatchThread() ?: error("Application is not available") val wasSet = if (!isForceCheckInTests) { isForceCheckInTests = true diff --git a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/handlers/KotlinClassifierInsertHandler.kt b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/handlers/KotlinClassifierInsertHandler.kt index dd23592c56f..fd8ecaef2ce 100644 --- a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/handlers/KotlinClassifierInsertHandler.kt +++ b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/handlers/KotlinClassifierInsertHandler.kt @@ -11,7 +11,7 @@ import com.intellij.psi.PsiClass import com.intellij.psi.PsiDocumentManager import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.ClassifierDescriptor -import org.jetbrains.kotlin.idea.caches.resolve.allowResolveInWriteAction +import org.jetbrains.kotlin.idea.caches.resolve.allowResolveInDispatchThread import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.completion.isAfterDot import org.jetbrains.kotlin.idea.completion.isArtificialImportAliasedDescriptor @@ -53,7 +53,7 @@ object KotlinClassifierInsertHandler : BaseDeclarationInsertHandler() { val token = file.findElementAt(startOffset)!! val nameRef = token.parent as? KtNameReferenceExpression if (nameRef != null) { - val bindingContext = allowResolveInWriteAction { nameRef.analyze(BodyResolveMode.PARTIAL) } + val bindingContext = allowResolveInDispatchThread { nameRef.analyze(BodyResolveMode.PARTIAL) } val target = bindingContext[BindingContext.SHORT_REFERENCE_TO_COMPANION_OBJECT, nameRef] ?: bindingContext[BindingContext.REFERENCE_TARGET, nameRef] as? ClassDescriptor if (target != null && IdeDescriptorRenderers.SOURCE_CODE.renderClassifierName(target) == qualifiedName) return diff --git a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/ShortenReferences.kt b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/ShortenReferences.kt index 184a684c24d..07c78711161 100644 --- a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/ShortenReferences.kt +++ b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/ShortenReferences.kt @@ -13,7 +13,7 @@ import com.intellij.psi.impl.source.PostprocessReformattingAspect import com.intellij.psi.util.PsiTreeUtil import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.idea.analysis.analyzeAsReplacement -import org.jetbrains.kotlin.idea.caches.resolve.allowResolveInWriteAction +import org.jetbrains.kotlin.idea.caches.resolve.allowResolveInDispatchThread import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade import org.jetbrains.kotlin.idea.imports.canBeReferencedViaImport import org.jetbrains.kotlin.idea.imports.getImportableTargets @@ -200,7 +200,7 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT // step 2: analyze collected elements with resolve and decide which can be shortened now and which need descriptors to be imported before shortening val allElementsToAnalyze = visitors.flatMap { visitor -> visitor.getElementsToAnalyze().map { it.element } }.toSet() - val bindingContext = allowResolveInWriteAction { + val bindingContext = allowResolveInDispatchThread { file.getResolutionFacade().analyze(allElementsToAnalyze, BodyResolveMode.PARTIAL_WITH_CFA) } diff --git a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/injection/KotlinLanguageInjector.kt b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/injection/KotlinLanguageInjector.kt index 012853b5887..9fa4155f4d0 100644 --- a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/injection/KotlinLanguageInjector.kt +++ b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/injection/KotlinLanguageInjector.kt @@ -33,7 +33,7 @@ import org.intellij.plugins.intelliLang.inject.java.JavaLanguageInjectionSupport import org.intellij.plugins.intelliLang.util.AnnotationUtilEx import org.jetbrains.kotlin.descriptors.FunctionDescriptor import org.jetbrains.kotlin.descriptors.annotations.Annotated -import org.jetbrains.kotlin.idea.caches.resolve.allowResolveInWriteAction +import org.jetbrains.kotlin.idea.caches.resolve.allowResolveInDispatchThread import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.core.util.runInReadActionWithWriteActionPriority import org.jetbrains.kotlin.idea.patterns.KotlinFunctionPattern @@ -269,7 +269,7 @@ class KotlinLanguageInjector( for (reference in callee.references) { ProgressManager.checkCanceled() - val resolvedTo = allowResolveInWriteAction { reference.resolve() } + val resolvedTo = allowResolveInDispatchThread { reference.resolve() } if (resolvedTo is PsiMethod) { val injectionForJavaMethod = injectionForJavaMethod(argument, resolvedTo) if (injectionForJavaMethod != null) { @@ -298,7 +298,7 @@ class KotlinLanguageInjector( if (!fastCheckInjectionsExists(annotationEntry)) return null val calleeExpression = annotationEntry.calleeExpression ?: return null val callee = getNameReference(calleeExpression)?.mainReference?.let { reference -> - allowResolveInWriteAction { reference.resolve() } + allowResolveInDispatchThread { reference.resolve() } } when (callee) { is PsiClass -> { @@ -346,7 +346,7 @@ class KotlinLanguageInjector( // Found psi element after resolve can be obtained from compiled declaration but annotations parameters are lost there. // Search for original descriptor from reference. val ktReference = reference as? KtReference ?: return null - val functionDescriptor = allowResolveInWriteAction { + val functionDescriptor = allowResolveInDispatchThread { val bindingContext = ktReference.element.analyze(BodyResolveMode.PARTIAL_WITH_DIAGNOSTICS) ktReference.resolveToDescriptors(bindingContext).singleOrNull() as? FunctionDescriptor } ?: return null diff --git a/idea/resources/META-INF/plugin-common.xml b/idea/resources/META-INF/plugin-common.xml index 114605a36f9..d2dd9536656 100644 --- a/idea/resources/META-INF/plugin-common.xml +++ b/idea/resources/META-INF/plugin-common.xml @@ -383,7 +383,7 @@ diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinCopyPasteReferenceProcessor.kt b/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinCopyPasteReferenceProcessor.kt index 59f75ba23f9..5eabb42baa4 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinCopyPasteReferenceProcessor.kt +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinCopyPasteReferenceProcessor.kt @@ -33,7 +33,7 @@ import org.jetbrains.annotations.TestOnly import org.jetbrains.kotlin.descriptors.CallableDescriptor import org.jetbrains.kotlin.descriptors.DeclarationDescriptor import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor -import org.jetbrains.kotlin.idea.caches.resolve.allowResolveInWriteAction +import org.jetbrains.kotlin.idea.caches.resolve.allowResolveInDispatchThread import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade import org.jetbrains.kotlin.idea.caches.resolve.resolveImportReference import org.jetbrains.kotlin.idea.codeInsight.shorten.performDelayedRefactoringRequests @@ -130,7 +130,7 @@ class KotlinCopyPasteReferenceProcessor : CopyPastePostProcessor() } val bindingContext = - allowResolveInWriteAction { + allowResolveInDispatchThread { file.getResolutionFacade().analyze(allElementsToResolve, BodyResolveMode.PARTIAL) } diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinLastLambdaParameterFixer.kt b/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinLastLambdaParameterFixer.kt index aa2975e25c1..62ab7a8bcb0 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinLastLambdaParameterFixer.kt +++ b/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinLastLambdaParameterFixer.kt @@ -20,7 +20,7 @@ import com.intellij.lang.SmartEnterProcessorWithFixers import com.intellij.openapi.editor.Editor import com.intellij.psi.PsiElement import org.jetbrains.kotlin.builtins.isFunctionType -import org.jetbrains.kotlin.idea.caches.resolve.allowResolveInWriteAction +import org.jetbrains.kotlin.idea.caches.resolve.allowResolveInDispatchThread import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall import org.jetbrains.kotlin.idea.editor.KotlinSmartEnterHandler import org.jetbrains.kotlin.psi.KtCallExpression @@ -30,7 +30,7 @@ class KotlinLastLambdaParameterFixer : SmartEnterProcessorWithFixers.Fixer(element: T) : QuickFixActionBase(element) { @@ -18,7 +18,7 @@ abstract class KotlinQuickFixAction(element: T) : QuickFixAc override fun isAvailableImpl(project: Project, editor: Editor?, file: PsiFile): Boolean { val ktFile = file as? KtFile ?: return false - return allowResolveExpectedToBeCached { + return allowCachedResolveInDispatchThread { // Quick fixes availability is checked in UI thread but only after background highlighting pass is finished // so we are expecting that every relevant results are already cached. isAvailable(project, editor, ktFile) diff --git a/idea/tests/org/jetbrains/kotlin/idea/conversion/copy/AbstractLiteralTextToKotlinCopyPasteTest.kt b/idea/tests/org/jetbrains/kotlin/idea/conversion/copy/AbstractLiteralTextToKotlinCopyPasteTest.kt index e3163e6bd12..9ad335f61ce 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/conversion/copy/AbstractLiteralTextToKotlinCopyPasteTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/conversion/copy/AbstractLiteralTextToKotlinCopyPasteTest.kt @@ -7,7 +7,7 @@ package org.jetbrains.kotlin.idea.conversion.copy import com.intellij.openapi.actionSystem.IdeActions import org.jetbrains.kotlin.idea.AbstractCopyPasteTest -import org.jetbrains.kotlin.idea.caches.resolve.forceResolveInWriteActionCheckInTests +import org.jetbrains.kotlin.idea.caches.resolve.forceCheckForResolveInDispatchThreadInTests import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor import org.jetbrains.kotlin.test.KotlinTestUtils import java.io.File @@ -26,13 +26,13 @@ abstract class AbstractLiteralTextToKotlinCopyPasteTest : AbstractCopyPasteTest( if (!myFixture.editor.selectionModel.hasSelection()) myFixture.editor.selectionModel.setSelection(0, fileText.length) - forceResolveInWriteActionCheckInTests { + forceCheckForResolveInDispatchThreadInTests { myFixture.performEditorAction(IdeActions.ACTION_COPY) } configureTargetFile(targetFileName) - forceResolveInWriteActionCheckInTests { + forceCheckForResolveInDispatchThreadInTests { myFixture.performEditorAction(IdeActions.ACTION_PASTE) } diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/AbstractQuickFixTest.kt b/idea/tests/org/jetbrains/kotlin/idea/quickfix/AbstractQuickFixTest.kt index 5c76b7fa6b9..7026a3bc2d7 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/AbstractQuickFixTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/AbstractQuickFixTest.kt @@ -16,13 +16,12 @@ import com.intellij.openapi.util.io.FileUtil import com.intellij.openapi.util.text.StringUtil import com.intellij.openapi.vfs.CharsetToolkit import com.intellij.rt.execution.junit.FileComparisonFailure -import com.intellij.testFramework.LightPlatformTestCase import com.intellij.testFramework.LightProjectDescriptor import com.intellij.testFramework.UsefulTestCase import com.intellij.util.ui.UIUtil import junit.framework.TestCase -import org.jetbrains.kotlin.idea.caches.resolve.ResolveInWriteActionException -import org.jetbrains.kotlin.idea.caches.resolve.forceResolveInWriteActionCheckInTests +import org.jetbrains.kotlin.idea.caches.resolve.ResolveInDispatchThreadException +import org.jetbrains.kotlin.idea.caches.resolve.forceCheckForResolveInDispatchThreadInTests import org.jetbrains.kotlin.idea.facet.KotlinFacet import org.jetbrains.kotlin.idea.test.* import org.jetbrains.kotlin.psi.KtFile @@ -179,12 +178,12 @@ abstract class AbstractQuickFixTest : KotlinLightCodeInsightFixtureTestCase(), Q val intentionClassName = unwrappedIntention.javaClass.name if (!quickFixesAllowedToResolveInWriteAction.isWriteActionAllowed(intentionClassName)) { - throw ResolveInWriteActionException("Resolve is not allowed under the write action for `$intentionClassName`!") + throw ResolveInDispatchThreadException("Resolve is not allowed under the write action for `$intentionClassName`!") } } val stubComparisonFailure: ComparisonFailure? = try { - forceResolveInWriteActionCheckInTests(writeActionResolveHandler) { + forceCheckForResolveInDispatchThreadInTests(writeActionResolveHandler) { myFixture.launchAction(intention) } null