Get rid of getInstanceIfNotDisposed
Relates to #EA-216005
This commit is contained in:
+1
-2
@@ -98,8 +98,7 @@ class GradleMigrateTest : GradleImportingTestCase() {
|
||||
}
|
||||
|
||||
val importResult = FutureResult<MigrationTestState?>()
|
||||
val migrationProjectComponent = KotlinMigrationProjectService.getInstanceIfNotDisposed(myProject)
|
||||
?: error("Disposed project")
|
||||
val migrationProjectComponent = KotlinMigrationProjectService.getInstance(myProject)
|
||||
|
||||
migrationProjectComponent.setImportFinishListener { migrationState ->
|
||||
importResult.set(migrationState)
|
||||
|
||||
+4
-2
@@ -8,11 +8,13 @@ package org.jetbrains.kotlin.idea.actions.internal
|
||||
import com.intellij.openapi.actionSystem.AnAction
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent
|
||||
import org.jetbrains.kotlin.idea.configuration.ui.KotlinConfigurationCheckerService
|
||||
import org.jetbrains.kotlin.idea.util.ProgressIndicatorUtils.runUnderDisposeAwareIndicator
|
||||
|
||||
class ReactivePostOpenProjectActionsAction : AnAction() {
|
||||
override fun actionPerformed(e: AnActionEvent) {
|
||||
val project = e.project ?: return
|
||||
val configurationChecker = KotlinConfigurationCheckerService.getInstanceIfNotDisposed(project) ?: return
|
||||
configurationChecker.performProjectPostOpenActions()
|
||||
runUnderDisposeAwareIndicator(project) {
|
||||
KotlinConfigurationCheckerService.getInstance(project).performProjectPostOpenActions()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+8
-3
@@ -21,18 +21,23 @@ import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotifica
|
||||
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskType
|
||||
import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.idea.configuration.ui.KotlinConfigurationCheckerService
|
||||
import org.jetbrains.kotlin.idea.util.ProgressIndicatorUtils.runUnderDisposeAwareIndicator
|
||||
|
||||
class KotlinExternalSystemSyncListener : ExternalSystemTaskNotificationListenerAdapter() {
|
||||
override fun onStart(id: ExternalSystemTaskId, workingDir: String) {
|
||||
val project = id.findResolvedProject() ?: return
|
||||
KotlinMigrationProjectService.getInstanceIfNotDisposed(project)?.onImportAboutToStart()
|
||||
KotlinConfigurationCheckerService.getInstanceIfNotDisposed(project)?.syncStarted()
|
||||
runUnderDisposeAwareIndicator(project) {
|
||||
KotlinMigrationProjectService.getInstance(project).onImportAboutToStart()
|
||||
KotlinConfigurationCheckerService.getInstance(project).syncStarted()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onEnd(id: ExternalSystemTaskId) {
|
||||
// At this point changes might be still not applied to project structure yet.
|
||||
val project = id.findResolvedProject() ?: return
|
||||
KotlinConfigurationCheckerService.getInstanceIfNotDisposed(project)?.syncDone()
|
||||
runUnderDisposeAwareIndicator(project) {
|
||||
KotlinConfigurationCheckerService.getInstance(project).syncDone()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -60,7 +60,7 @@ class KotlinSetupEnvironmentNotificationProvider(private val myProject: Project)
|
||||
return createSetupSdkPanel(myProject, psiFile)
|
||||
}
|
||||
|
||||
val configurationChecker = KotlinConfigurationCheckerService.getInstanceIfNotDisposed(module.project) ?: return null
|
||||
val configurationChecker = KotlinConfigurationCheckerService.getInstance(module.project)
|
||||
|
||||
if (!configurationChecker.isSyncing &&
|
||||
isNotConfiguredNotificationRequired(module.toModuleGroup()) &&
|
||||
|
||||
+5
-6
@@ -81,14 +81,13 @@ class KotlinConfigurationCheckerComponent(val project: Project) : ProjectCompone
|
||||
companion object {
|
||||
const val CONFIGURE_NOTIFICATION_GROUP_ID = "Configure Kotlin in Project"
|
||||
|
||||
fun getInstance(project: Project): KotlinConfigurationCheckerComponent =
|
||||
project.getComponent(KotlinConfigurationCheckerComponent::class.java)
|
||||
?: error("Can't find ${KotlinConfigurationCheckerComponent::class} component")
|
||||
|
||||
fun getInstanceIfNotDisposed(project: Project): KotlinConfigurationCheckerComponent? {
|
||||
return runReadAction {
|
||||
if (!project.isDisposed) {
|
||||
project.getComponent(KotlinConfigurationCheckerComponent::class.java)
|
||||
?: error("Can't find ${KotlinConfigurationCheckerComponent::class} component")
|
||||
} else {
|
||||
null
|
||||
}
|
||||
if (!project.isDisposed) getInstance(project) else null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+5
-11
@@ -32,6 +32,8 @@ import org.jetbrains.kotlin.idea.configuration.getModulesWithKotlinFiles
|
||||
import org.jetbrains.kotlin.idea.configuration.notifyOutdatedBundledCompilerIfNecessary
|
||||
import org.jetbrains.kotlin.idea.configuration.ui.notifications.notifyKotlinStyleUpdateIfNeeded
|
||||
import org.jetbrains.kotlin.idea.project.getAndCacheLanguageLevelByDependencies
|
||||
import org.jetbrains.kotlin.idea.util.ProgressIndicatorUtils
|
||||
import org.jetbrains.kotlin.idea.util.application.getServiceSafe
|
||||
import org.jetbrains.kotlin.idea.util.application.isUnitTestMode
|
||||
import java.util.concurrent.Callable
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
@@ -51,7 +53,7 @@ class KotlinConfigurationCheckerStartupActivity : StartupActivity {
|
||||
|
||||
notifyKotlinStyleUpdateIfNeeded(project)
|
||||
|
||||
KotlinConfigurationCheckerService.getInstanceIfNotDisposed(project)?.performProjectPostOpenActions()
|
||||
KotlinConfigurationCheckerService.getInstance(project).performProjectPostOpenActions()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,15 +103,7 @@ class KotlinConfigurationCheckerService(val project: Project) {
|
||||
companion object {
|
||||
const val CONFIGURE_NOTIFICATION_GROUP_ID = "Configure Kotlin in Project"
|
||||
|
||||
fun getInstanceIfNotDisposed(project: Project): KotlinConfigurationCheckerService? {
|
||||
return runReadAction {
|
||||
if (!project.isDisposed) {
|
||||
project.getService(KotlinConfigurationCheckerService::class.java)
|
||||
?: error("Can't find ${KotlinConfigurationCheckerService::class} service")
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
fun getInstance(project: Project): KotlinConfigurationCheckerService = project.getServiceSafe()
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,14 +12,17 @@ import org.jetbrains.idea.maven.project.MavenProject
|
||||
import org.jetbrains.idea.maven.project.MavenProjectsManager
|
||||
import org.jetbrains.kotlin.idea.configuration.KotlinMigrationProjectService
|
||||
import org.jetbrains.kotlin.idea.configuration.notifyOutdatedBundledCompilerIfNecessary
|
||||
import org.jetbrains.kotlin.idea.util.ProgressIndicatorUtils.runUnderDisposeAwareIndicator
|
||||
|
||||
class MavenImportListener(val project: Project) : MavenProjectsManager.Listener {
|
||||
init {
|
||||
project.messageBus.connect().subscribe(
|
||||
project.messageBus.connect(project).subscribe(
|
||||
MavenImportListener.TOPIC,
|
||||
MavenImportListener { _: Collection<MavenProject>, _: List<Module> ->
|
||||
notifyOutdatedBundledCompilerIfNecessary(project)
|
||||
KotlinMigrationProjectService.getInstanceIfNotDisposed(project)?.onImportFinished()
|
||||
runUnderDisposeAwareIndicator(project) {
|
||||
notifyOutdatedBundledCompilerIfNecessary(project)
|
||||
KotlinMigrationProjectService.getInstance(project).onImportFinished()
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
@@ -27,6 +30,8 @@ class MavenImportListener(val project: Project) : MavenProjectsManager.Listener
|
||||
}
|
||||
|
||||
override fun projectsScheduled() {
|
||||
KotlinMigrationProjectService.getInstanceIfNotDisposed(project)?.onImportAboutToStart()
|
||||
runUnderDisposeAwareIndicator(project) {
|
||||
KotlinMigrationProjectService.getInstance(project).onImportAboutToStart()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -108,8 +108,7 @@ class MavenMigrateTest : MavenImportingTestCase() {
|
||||
}
|
||||
|
||||
val importResult = FutureResult<MigrationTestState?>()
|
||||
val migrationProjectComponent = KotlinMigrationProjectService.getInstanceIfNotDisposed(myProject)
|
||||
?: error("Disposed project")
|
||||
val migrationProjectComponent = KotlinMigrationProjectService.getInstance(myProject)
|
||||
|
||||
migrationProjectComponent.setImportFinishListener { migrationState ->
|
||||
importResult.set(migrationState)
|
||||
|
||||
@@ -8,13 +8,16 @@ package org.jetbrains.kotlin.idea.configuration
|
||||
import com.intellij.openapi.externalSystem.service.project.manage.ProjectDataImportListener
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.startup.StartupActivity
|
||||
import org.jetbrains.kotlin.idea.util.ProgressIndicatorUtils.runUnderDisposeAwareIndicator
|
||||
|
||||
class KotlinMigrationProjectComponent : StartupActivity {
|
||||
|
||||
override fun runActivity(project: Project) {
|
||||
val connection = project.messageBus.connect(project)
|
||||
connection.subscribe(ProjectDataImportListener.TOPIC, ProjectDataImportListener {
|
||||
KotlinMigrationProjectService.getInstanceIfNotDisposed(project)?.onImportFinished()
|
||||
runUnderDisposeAwareIndicator(project) {
|
||||
KotlinMigrationProjectService.getInstance(project).onImportFinished()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
+5
-6
@@ -116,14 +116,13 @@ class KotlinMigrationProjectComponent(val project: Project) {
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun getInstance(project: Project): KotlinMigrationProjectComponent =
|
||||
project.getComponent(KotlinMigrationProjectComponent::class.java)
|
||||
?: error("Can't find ${KotlinMigrationProjectComponent::class.qualifiedName} component")
|
||||
|
||||
fun getInstanceIfNotDisposed(project: Project): KotlinMigrationProjectComponent? {
|
||||
return runReadAction {
|
||||
if (!project.isDisposed) {
|
||||
project.getComponent(KotlinMigrationProjectComponent::class.java)
|
||||
?: error("Can't find ${KotlinMigrationProjectComponent::class.qualifiedName} component")
|
||||
} else {
|
||||
null
|
||||
}
|
||||
if (!project.isDisposed) getInstance(project) else null
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
|
||||
package org.jetbrains.kotlin.idea.configuration
|
||||
|
||||
import com.intellij.openapi.application.ApplicationManager
|
||||
import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil
|
||||
import com.intellij.openapi.module.Module
|
||||
import com.intellij.openapi.module.ModuleManager
|
||||
@@ -28,8 +27,7 @@ import org.jetbrains.kotlin.idea.framework.MAVEN_SYSTEM_ID
|
||||
import org.jetbrains.kotlin.idea.migration.CodeMigrationToggleAction
|
||||
import org.jetbrains.kotlin.idea.migration.applicableMigrationTools
|
||||
import org.jetbrains.kotlin.idea.project.languageVersionSettings
|
||||
import org.jetbrains.kotlin.idea.util.application.getServiceSafe
|
||||
import org.jetbrains.kotlin.idea.util.application.runReadAction
|
||||
import org.jetbrains.kotlin.idea.util.application.*
|
||||
import org.jetbrains.kotlin.idea.util.runReadActionInSmartMode
|
||||
import org.jetbrains.kotlin.idea.versions.LibInfo
|
||||
import java.io.File
|
||||
@@ -75,7 +73,7 @@ class KotlinMigrationProjectService(val project: Project) {
|
||||
return
|
||||
}
|
||||
|
||||
ApplicationManager.getApplication().executeOnPooledThread {
|
||||
executeOnPooledThread {
|
||||
var migrationInfo: MigrationInfo? = null
|
||||
var hasApplicableTools = false
|
||||
|
||||
@@ -97,11 +95,11 @@ class KotlinMigrationProjectService(val project: Project) {
|
||||
hasApplicableTools = true
|
||||
}
|
||||
|
||||
if (ApplicationManager.getApplication().isUnitTestMode) {
|
||||
if (isUnitTestMode()) {
|
||||
return@executeOnPooledThread
|
||||
}
|
||||
|
||||
ApplicationManager.getApplication().invokeLater {
|
||||
invokeLater {
|
||||
showMigrationNotification(project, migrationInfo)
|
||||
}
|
||||
} finally {
|
||||
@@ -111,11 +109,7 @@ class KotlinMigrationProjectService(val project: Project) {
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun getInstanceIfNotDisposed(project: Project): KotlinMigrationProjectService? {
|
||||
return runReadAction {
|
||||
if (!project.isDisposed) project.getServiceSafe() else null
|
||||
}
|
||||
}
|
||||
fun getInstance(project: Project): KotlinMigrationProjectService = project.getServiceSafe()
|
||||
|
||||
private fun prepareMigrationInfo(old: MigrationState?, new: MigrationState?): MigrationInfo? {
|
||||
if (old == null || new == null) {
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.util
|
||||
|
||||
import com.intellij.openapi.Disposable
|
||||
import com.intellij.openapi.progress.ProcessCanceledException
|
||||
import com.intellij.openapi.progress.ProgressIndicator
|
||||
import com.intellij.openapi.progress.ProgressManager
|
||||
import com.intellij.openapi.progress.util.BackgroundTaskUtil
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.util.Computable
|
||||
import com.intellij.util.ExceptionUtil
|
||||
import org.jetbrains.annotations.Nls
|
||||
import java.util.concurrent.CancellationException
|
||||
@@ -27,6 +30,11 @@ object ProgressIndicatorUtils {
|
||||
computable: () -> T
|
||||
): T = com.intellij.openapi.actionSystem.ex.ActionUtil.underModalProgress(project, progressTitle, computable)
|
||||
|
||||
fun <T> runUnderDisposeAwareIndicator(
|
||||
parent: Disposable,
|
||||
computable: () -> T
|
||||
): T = BackgroundTaskUtil.runUnderDisposeAwareIndicator(parent, Computable { computable() })
|
||||
|
||||
@JvmStatic
|
||||
fun <T> awaitWithCheckCanceled(future: Future<T>): T {
|
||||
val indicator = ProgressManager.getInstance().progressIndicator
|
||||
|
||||
@@ -5,9 +5,11 @@
|
||||
|
||||
package org.jetbrains.kotlin.idea.util
|
||||
|
||||
import com.intellij.openapi.Disposable
|
||||
import com.intellij.openapi.diagnostic.Logger
|
||||
import com.intellij.openapi.progress.ProcessCanceledException
|
||||
import com.intellij.openapi.progress.ProgressManager
|
||||
import com.intellij.openapi.progress.util.BackgroundTaskUtil
|
||||
import com.intellij.openapi.project.DumbService
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.util.Computable
|
||||
@@ -36,6 +38,11 @@ object ProgressIndicatorUtils {
|
||||
return ProgressManager.getInstance().runProcessWithProgressSynchronously(process, progressTitle, true, project)
|
||||
}
|
||||
|
||||
fun <T> runUnderDisposeAwareIndicator(
|
||||
parent: Disposable,
|
||||
computable: () -> T
|
||||
): T = BackgroundTaskUtil.runUnderDisposeAwareIndicator(parent, Computable { computable() })
|
||||
|
||||
@JvmStatic
|
||||
fun <T> awaitWithCheckCanceled(future: Future<T>): T {
|
||||
while (true) {
|
||||
@@ -50,5 +57,4 @@ object ProgressIndicatorUtils {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,9 +5,11 @@
|
||||
|
||||
package org.jetbrains.kotlin.idea.util
|
||||
|
||||
import com.intellij.openapi.Disposable
|
||||
import com.intellij.openapi.diagnostic.Logger
|
||||
import com.intellij.openapi.progress.ProcessCanceledException
|
||||
import com.intellij.openapi.progress.ProgressManager
|
||||
import com.intellij.openapi.progress.util.BackgroundTaskUtil
|
||||
import com.intellij.openapi.project.DumbService
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.util.Computable
|
||||
@@ -38,6 +40,11 @@ object ProgressIndicatorUtils {
|
||||
return ProgressManager.getInstance().runProcessWithProgressSynchronously(process, progressTitle, true, project)
|
||||
}
|
||||
|
||||
fun <T> runUnderDisposeAwareIndicator(
|
||||
parent: Disposable,
|
||||
computable: () -> T
|
||||
): T = BackgroundTaskUtil.runUnderDisposeAwareIndicator(parent, Computable { computable() })
|
||||
|
||||
@JvmStatic
|
||||
fun <T> awaitWithCheckCanceled(future: Future<T>): T {
|
||||
while (true) {
|
||||
|
||||
Reference in New Issue
Block a user