FIR IDE: add validation contract to analysis session
This contract ensures that analysis session is used: * in read action * in non-EDT * Project has not changed since the session was created
This commit is contained in:
+1
-2
@@ -10,8 +10,7 @@ import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
|
||||
|
||||
abstract class FrontendAnalysisSession {
|
||||
abstract fun getSmartCastedToTypes(expression: KtExpression): Collection<KotlinTypeMarker>?
|
||||
abstract class FrontendAnalysisSession : Invalidatable {
|
||||
|
||||
abstract fun getImplicitReceiverSmartCasts(expression: KtExpression): Collection<ImplicitReceiverSmartCast>
|
||||
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.frontend.api
|
||||
|
||||
import com.intellij.openapi.application.ApplicationManager
|
||||
import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.analyzer.KotlinModificationTrackerService
|
||||
|
||||
interface Invalidatable {
|
||||
fun isValid(): Boolean
|
||||
|
||||
fun invalidationReason(): String
|
||||
|
||||
fun assertIsValid() {
|
||||
assert(isValid()) { "Access to invalid $this, invalidation reason is ${invalidationReason()}" }
|
||||
}
|
||||
}
|
||||
|
||||
class ReadActionConfinementValidityToken(project: Project) : Invalidatable {
|
||||
private val modificationTracker = KotlinModificationTrackerService.getInstance(project).modificationTracker
|
||||
private val ownerThread = Thread.currentThread()
|
||||
private val onCreatedTimeStamp = modificationTracker.modificationCount
|
||||
|
||||
|
||||
override fun isValid(): Boolean {
|
||||
val application = ApplicationManager.getApplication()
|
||||
if (application.isDispatchThread) return false
|
||||
if (!application.isReadAccessAllowed) return false
|
||||
if (ownerThread != Thread.currentThread()) return false
|
||||
return onCreatedTimeStamp == modificationTracker.modificationCount
|
||||
}
|
||||
|
||||
override fun invalidationReason(): String {
|
||||
val application = ApplicationManager.getApplication()
|
||||
if (application.isDispatchThread) return "Called in EDT thread"
|
||||
if (!application.isReadAccessAllowed) return "Called outside read action"
|
||||
if (ownerThread != Thread.currentThread()) {
|
||||
return "Called outside the thread was created in (was created in $ownerThread, but accessed in ${Thread.currentThread()}"
|
||||
}
|
||||
if (onCreatedTimeStamp != modificationTracker.modificationCount) return "PSI has changed since creation"
|
||||
error("Getting invalidation reason for valid invalidatable")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user