Companion object in extensions
Reports incorrect companion objects' usage in IDE extensions.
Kotlin companion object is always created once you try to load its containing class, and IDE extensions are supposed to be cheap to create. Excessive classloading in plugins is a huge problem for IDE startup.
Bad pattern:
Here KotlinDocumentationProvider
is an extension registered in plugin.xml
:
In this example JavaDocumentationProvider
will be loaded from disk once someone just calls new KotlinDocumentationProvider()
.
Kotlin companion objects in extension implementations 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.
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:
How to rewrite FileType?
Before:
After:
Use INSTANCE
fieldName in plugin.xml
:
New in 2023.2
Inspection Details | |
---|---|
Available in: | IntelliJ IDEA 2023.3, Qodana for JVM 2023.3 |
Plugin: | Plugin DevKit, 233.SNAPSHOT |