fix "IDEA-221944 Deadlock on opening second project" and support preloading for proje...
[idea/community.git] / platform / projectModel-impl / src / com / intellij / openapi / components / service.kt
1 // Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2 package com.intellij.openapi.components
3
4 import com.intellij.openapi.application.ApplicationManager
5 import com.intellij.openapi.components.impl.stores.IComponentStore
6 import com.intellij.openapi.project.Project
7 import com.intellij.project.ProjectStoreOwner
8
9 inline fun <reified T : Any> service(): T = ApplicationManager.getApplication().getService(T::class.java, true)
10
11 inline fun <reified T : Any> serviceOrNull(): T? = ApplicationManager.getApplication().getService(T::class.java, true)
12
13 inline fun <reified T : Any> serviceIfCreated(): T? = ApplicationManager.getApplication().getServiceIfCreated(T::class.java)
14
15 inline fun <reified T : Any> Project.service(): T = getService(T::class.java, true)
16
17 val ComponentManager.stateStore: IComponentStore
18   get() {
19     return when (this) {
20       is ProjectStoreOwner -> this.getComponentStore()
21       else -> {
22         // module or application service
23         getService(IComponentStore::class.java)
24       }
25     }
26   }
27
28 @Suppress("EXTENSION_SHADOWED_BY_MEMBER")
29 @Deprecated(message = "do not use", replaceWith = ReplaceWith("getComponentInstancesOfType(baseClass)"))
30 fun <T> ComponentManager.getComponents(baseClass: Class<T>): List<T> {
31   @Suppress("DEPRECATION")
32   return getComponentInstancesOfType(baseClass, false)
33 }