Companion property without static access
| Diagnostic | COMPANION_PROPERTY_WITHOUT_STATIC_ACCESS |
| Default severity | Error |
| Applies to | JVM compilations only |
| Gradle property | companionPropertyWithoutStaticAccess |
| Exemption | @IntentionallyNonStaticCompanionApi |
What it reports
Companion properties in the public API with at least one Java-visible accessor without static access. The check includes stored, computed, delegated, extension, and override properties.
public class Registry {public companion object {public var currentEndpoint: String = "local"}}
Rationale
Without static access, Java callers must use Registry.Companion.getCurrentEndpoint() and
Registry.Companion.setCurrentEndpoint(value). @JvmStatic adds accessors on Registry.
Don't
public class Registry {public companion object {public var currentEndpoint: String = "local"}}
Do
public class Registry {public companion object {@JvmStaticpublic var currentEndpoint: String = "local"}}
Notes
- The check treats the getter and setter separately. A
varwith only@get:JvmStaticstill reports its setter. Add@set:JvmStatic, hide it with@set:JvmSynthetic, or make it non-public. - A private or internal setter doesn't need static access.
- The check skips
const val,@JvmField, andlateinit varwith a public setter, which expose static fields. Alateinit varwith a non-public setter still reports its getter. For custom accessors, delegates, properties without a backing field, and overrides, use@JvmStatic. - The check skips properties without Java-visible accessors.
- The check skips
@PublishedApi internalproperties.
Exemption
If this is intended, apply @IntentionallyNonStaticCompanionApi to the property or an
enclosing class.
Use the exemption when the declaration is part of the API design or must stay for backwards compatibility.
public class Registry {public companion object {@IntentionallyNonStaticCompanionApi(reason = ExemptionReason.API_DESIGN,)public var currentEndpoint: String = "local"}}
Configuration
build.gradle.kts
apiWatchdog {javaInterop {companionPropertyWithoutStaticAccess = WatchdogSeverity.ERROR}}
javaInterop { enabled = false } disables all Java interop checks.
For a direct compiler call:
-P plugin:org.jetbrains.kotlinx.library.api.watchdog:diagnosticSeverity=COMPANION_PROPERTY_WITHOUT_STATIC_ACCESS:error