mirror of
https://github.com/JetBrains/intellij-sdk-code-samples.git
synced 2025-07-28 01:07:49 +08:00
using_kotlin.md: companion object usage in extensions
This commit is contained in:
parent
899f585b22
commit
aae32db25b
@ -78,9 +78,11 @@ Please note the following important points:
|
||||
|
||||
- Extension implementations should be stateless. Use explicit [](plugin_services.md) for managing (runtime) data.
|
||||
- Avoid any initialization in constructor, see also notes for [Services](plugin_services.md#constructor).
|
||||
- Kotlin: Do not use `object` but `class` for implementation ([more details](using_kotlin.md#caution)).
|
||||
- Kotlin: Do not use `companion object` to avoid excessive classloading/initialization when the extension class is loaded.
|
||||
Use top-level declarations or objects instead.
|
||||
|
||||
When using Kotlin:
|
||||
- Do not use `object` but `class` for implementation. [More details](using_kotlin.md#object-vs-class)
|
||||
- Do not use `companion object` to avoid excessive classloading/initialization when the extension class is loaded.
|
||||
Use top-level declarations or objects instead. [More details](using_kotlin.md#companion-object-extensions)
|
||||
|
||||
</procedure>
|
||||
|
||||
|
@ -161,7 +161,10 @@ You can find the current state of the issue in [KT-57757](https://youtrack.jetbr
|
||||
|
||||
Please see [Third-Party Software and Licenses](https://www.jetbrains.com/legal/third-party-software/).
|
||||
|
||||
## Caution
|
||||
## Plugin Implementation Notes
|
||||
|
||||
### Do not use "object" but "class"
|
||||
{id="object-vs-class"}
|
||||
|
||||
Plugins *may* use [Kotlin classes](https://kotlinlang.org/docs/classes.html) (`class` keyword) to implement declarations in the [plugin configuration file](plugin_configuration_file.md).
|
||||
When registering an extension, the platform uses a dependency injection framework to instantiate these classes at runtime.
|
||||
@ -172,6 +175,15 @@ Problems are highlighted via these inspections (2023.2):
|
||||
- <control>Plugin DevKit | Code | Kotlin object registered as extension</control> for Kotlin code
|
||||
- <control>Plugin DevKit | Plugin descriptor | Extension class is a Kotlin object</control> for <path>plugin.xml</path>
|
||||
|
||||
### Do not use "companion object" in extensions
|
||||
{id="companion-object-extensions"}
|
||||
|
||||
Kotlin `companion object` is always created once you try to load its containing class, and [extension point implementations](plugin_extensions.md) are supposed to be cheap to create.
|
||||
To avoid unnecessary classloading (and thus slowdown in IDE startup), `companion object` in extensions must only contain simple constants or [logger](ide_infrastructure.md#logging).
|
||||
Anything else must be a top-level declaration or stored in an `object`.
|
||||
|
||||
Use inspection <control>Plugin DevKit | Code | Companion object in extensions</control> to highlight such problems (2023.3).
|
||||
|
||||
## Kotlin Code FAQ
|
||||
|
||||
[How to shorten references](https://intellij-support.jetbrains.com/hc/en-us/community/posts/360010025120-Add-new-parameter-into-kotlin-data-class-from-IDEA-plugin?page=1#community_comment_360002950760)
|
||||
|
Loading…
x
Reference in New Issue
Block a user