Companion function without JvmStatic
| Diagnostic | COMPANION_API_WITHOUT_JVM_STATIC |
| Default severity | Error |
| Applies to | JVM compilations only |
| Gradle property | companionApiWithoutJvmStatic |
| Exemption | @IntentionallyNonStaticCompanionApi |
What it reports
Functions in the public API declared directly in a class or interface companion object without
@JvmStatic or @JvmSynthetic.
public class ServiceFactory {public companion object {public fun create(): ServiceFactory = ServiceFactory()}}
Rationale
Without @JvmStatic, Java callers must write Registry.Companion.create() instead of
Registry.create().
Don't
public class Registry {public companion object {public fun create(): Registry = Registry()}}
Do
public class Registry {public companion object {@JvmStaticpublic fun create(): Registry = Registry()}}
Notes
- The check includes overrides.
suspendfunctions are also reported by Kotlin-only API without JvmSynthetic.@JvmSyntheticresolves both diagnostics.- The check skips
@PublishedApi internalfunctions.
Exemption
If this is intended, apply @IntentionallyNonStaticCompanionApi to the function 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 fun create(): Registry = Registry()}}
Configuration
build.gradle.kts
apiWatchdog {javaInterop {companionApiWithoutJvmStatic = 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_API_WITHOUT_JVM_STATIC:error