Skip to main content

Companion property without static access

DiagnosticCOMPANION_PROPERTY_WITHOUT_STATIC_ACCESS
Default severityError
Applies toJVM compilations only
Gradle propertycompanionPropertyWithoutStaticAccess
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 {
@JvmStatic
public var currentEndpoint: String = "local"
}
}

Notes​

  • The check treats the getter and setter separately. A var with only @get:JvmStatic still 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, and lateinit var with a public setter, which expose static fields. A lateinit var with 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 internal properties.

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