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):
class MyTokens {
// New IElementType created for each MyTokens instance
IElementType myToken = new IElementType("MY_TOKEN");
}
Good pattern (Java):
class MyTokens {
// Singleton IElementType shared across all instances
static final IElementType MY_TOKEN = new IElementType("MY_TOKEN");
}
Bad pattern (Kotlin):
class MyTokens {
// New IElementType created for each MyTokens instance
val myToken = IElementType("MY_TOKEN")
}
Good pattern (Kotlin):
class MyTokens {
companion object {
// Singleton IElementType shared across all instances
val MY_TOKEN = IElementType("MY_TOKEN")
}
}
Alternatively in Kotlin, token types can be defined as top-level or object declarations:
// Top-level constant
private val MY_TOKEN = IElementType("MY_TOKEN")
// Or in an object
object MyTokens {
val MY_TOKEN = IElementType("MY_TOKEN")
}