Reports incorrect companion objects' usage in extensions.

Kotlin companion object is always created once you try to load its containing class, and extension point implementations are supposed to be cheap to create. Excessive classloading in plugins is a huge problem for IDE startup.

Bad pattern:


  class KotlinDocumentationProvider : AbstractDocumentationProvider(), ExternalDocumentationProvider {

      companion object {
          private val LOG = Logger.getInstance(KotlinDocumentationProvider::class.java)
          private val javaDocumentProvider = JavaDocumentationProvider()
      }
  }

Here KotlinDocumentationProvider is an extension registered in plugin.xml:


  <lang.documentationProvider language="JAVA"
                              implementationClass="org.jetbrains.kotlin.idea.KotlinDocumentationProvider"
                              order="first"/>

In this example JavaDocumentationProvider will be loaded from disk once someone just calls new KotlinDocumentationProvider().

Kotlin companion objects in extension point implementation can only contain a logger and simple constants. Other declarations may cause excessive classloading or early initialization of heavy resources (e.g. TokenSet, Regex, etc.) when the extension class is loaded. Note, that even declarations marked with @JvmStatic still produce an extra class for the companion object, potentially causing expensive computations.

Instead of being stored in companion object, these declarations must be top-level or stored in an object.

FAQ

How to rewrite run ConfigurationType?

Move the declaration to top-level:


  // DO, use top level fun
  internal fun mnRunConfigurationType(): MnRunConfigurationType = runConfigurationType<MnRunConfigurationType>()

  internal class MnRunConfigurationType : ConfigurationType {
    companion object { // DON'T
      fun getInstance(): MnRunConfigurationType = runConfigurationType<MnRunConfigurationType>()
    }
    ...

How to rewrite FileType?

Before:


  internal class SpringBootImportsFileType : LanguageFileType(SPILanguage.INSTANCE, true) {
    companion object {
      val FILE_TYPE = SpringBootImportsFileType()
      ...

After:


  internal object SpringBootImportsFileType : LanguageFileType(SPILanguage.INSTANCE, true) {
  ...

Use INSTANCE fieldName in plugin.xml:


  <fileType name="Spring Boot Imports"
              fieldName="INSTANCE"
              implementationClass="com.intellij.spring.boot.spi.SpringBootImportsFileType"/>

How to rewrite CounterUsagesCollector?

Internal API

Before:


  class AntActionsUsagesCollector : CounterUsagesCollector() {
    override fun getGroup(): EventLogGroup = GROUP

    companion object {
      private val GROUP = EventLogGroup("build.ant.actions", 1)

      @JvmField
      val runSelectedBuildAction = GROUP.registerEvent("RunSelectedBuild")
   }
}

After:


 object AntActionsUsagesCollector : CounterUsagesCollector() {
  override fun getGroup(): EventLogGroup = GROUP

  private val GROUP = EventLogGroup("build.ant.actions", 1)

  @JvmField
  val runSelectedBuildAction = GROUP.registerEvent("RunSelectedBuild")
}

New in 2023.3