Stateful classes without equals, hashCode, and toString
| Diagnostic | Gradle property | Exemption |
|---|---|---|
STATEFUL_CLASS_WITHOUT_EQUALS | statefulClassWithoutEquals | @IntentionallyWithoutEquals |
STATEFUL_CLASS_WITHOUT_HASH_CODE | statefulClassWithoutHashCode | @IntentionallyWithoutHashCode |
STATEFUL_CLASS_WITHOUT_TO_STRING | statefulClassWithoutToString | @IntentionallyWithoutToString |
Default severity: error for all three checks.
Combined exemption: @IntentionallyWithoutEqualsHashCodeOrToString
What they report
Regular classes in the public API with at least one non-delegated backing-field property.
Each check reports a missing implementation of its member, independently of the other two.
A supertype's implementation counts unless it comes from kotlin.Any.
public class Endpoint(public val host: String,)
Rationale
Default equality and hashing use object identity, so equal state doesn't make two instances equal.
The default toString doesn't show the state in logs or debuggers.
Poko generates all three members without a data class's copy and componentN functions.
In IntelliJ IDEA, use Alt+Insert or Code | Generate to generate the methods.
Don't
public class Connection(public val host: String,)
Do
Or Do
public class Connection(public val host: String,) {public override fun equals(other: Any?): Boolean =this === other || (other is Connection && host == other.host)public override fun hashCode(): Int = host.hashCode()public override fun toString(): String = "Connection(host=$host)"}
Notes
The checks skip:
- Data and value classes.
- Enums, objects, interfaces, and annotation classes.
- Classes with only delegated properties or properties without backing fields.
@PublishedApi internalclasses.
Check whether inherited implementations include any state that the subclass adds.
Exemptions
| Missing behavior | Individual exemption | Combined exemption |
|---|---|---|
equals | @IntentionallyWithoutEquals | @IntentionallyWithoutEqualsHashCodeOrToString |
hashCode | @IntentionallyWithoutHashCode | @IntentionallyWithoutEqualsHashCodeOrToString |
toString | @IntentionallyWithoutToString | @IntentionallyWithoutEqualsHashCodeOrToString |
Use the exemptions when the behavior is part of the API design or must stay for backwards compatibility. An individual exemption covers one behavior, the combined exemption all three.
@IntentionallyWithoutEqualsHashCodeOrToString(reason = ExemptionReason.API_DESIGN)public class Connection(public val host: String,)
Configuration
Poko
Poko supports Kotlin/JVM and Kotlin Multiplatform. Apply the
version compatible with the Kotlin version used by the project. For Kotlin 2.4.0:
plugins {id("dev.drewhamilton.poko") version "0.23.0"}
Add @Poko to each class that needs generated equals, hashCode, and toString.
Check severity
apiWatchdog {statefulClassWithoutEquals = WatchdogSeverity.ERRORstatefulClassWithoutHashCode = WatchdogSeverity.ERRORstatefulClassWithoutToString = WatchdogSeverity.ERROR}
For a direct compiler call:
-P plugin:org.jetbrains.kotlinx.library.api.watchdog:diagnosticSeverity=STATEFUL_CLASS_WITHOUT_EQUALS:error-P plugin:org.jetbrains.kotlinx.library.api.watchdog:diagnosticSeverity=STATEFUL_CLASS_WITHOUT_HASH_CODE:error-P plugin:org.jetbrains.kotlinx.library.api.watchdog:diagnosticSeverity=STATEFUL_CLASS_WITHOUT_TO_STRING:error