Instance IElementType field
Reports instance fields initialized with IElementType or its subtypes.
IElementType instances represent token types in the IntelliJ Platform's lexer/parser infrastructure and should be defined as static constants to ensure they are singletons. When defined as instance fields, a new IElementType instance is created for each class instance. All such instances are registered in IElementType internal storage and kept there until the app is closed. Currently, the infrastructure allows storing only ~16k of element types, which is not that many.
The inspection suggests converting instance fields to static constants in Java or moving them to a companion object in Kotlin.
Bad pattern (Java):
Good pattern (Java):
Bad pattern (Kotlin):
Good pattern (Kotlin):
Alternatively in Kotlin, token types can be defined as top-level or object declarations:
Locating this inspection
- By ID
Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.
InstanceIElementTypeField- Via Settings dialog
Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.
Inspection ID: InstanceIElementTypeField
Suppressing Inspection
You can suppress this inspection by placing the following comment marker before the code fragment where you no longer want messages from this inspection to appear:
More detailed instructions as well as other ways and options that you have can be found in the product documentation:
Inspection Details | |
|---|---|
By default bundled with: |