Skip to main content

Companion function without JvmStatic

DiagnosticCOMPANION_API_WITHOUT_JVM_STATIC
Default severityError
Applies toJVM compilations only
Gradle propertycompanionApiWithoutJvmStatic
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 {
@JvmStatic
public fun create(): Registry = Registry()
}
}

Notes​

  • The check includes overrides.
  • suspend functions are also reported by Kotlin-only API without JvmSynthetic. @JvmSynthetic resolves both diagnostics.
  • The check skips @PublishedApi internal functions.

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

See also​