Skip to main content

Stateful classes without equals, hashCode, and toString

DiagnosticGradle propertyExemption
STATEFUL_CLASS_WITHOUT_EQUALSstatefulClassWithoutEquals@IntentionallyWithoutEquals
STATEFUL_CLASS_WITHOUT_HASH_CODEstatefulClassWithoutHashCode@IntentionallyWithoutHashCode
STATEFUL_CLASS_WITHOUT_TO_STRINGstatefulClassWithoutToString@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​

public class Connection(
public val host: String,
)

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 internal classes.

Check whether inherited implementations include any state that the subclass adds.

Exemptions​

Missing behaviorIndividual exemptionCombined 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:

build.gradle.kts
plugins {
id("dev.drewhamilton.poko") version "0.23.0"
}

Add @Poko to each class that needs generated equals, hashCode, and toString.

Check severity​

build.gradle.kts
apiWatchdog {
statefulClassWithoutEquals = WatchdogSeverity.ERROR
statefulClassWithoutHashCode = WatchdogSeverity.ERROR
statefulClassWithoutToString = 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

See also​