Default parameters without JvmOverloads
| Diagnostic | DEFAULT_PARAMETERS_WITHOUT_JVM_OVERLOADS |
| Default severity | Error |
| Applies to | JVM compilations only |
| Gradle property | defaultParametersWithoutJvmOverloads |
| Exemption | @IntentionallyWithoutJvmOverloads |
What it reports
Functions and constructors in the public API with at least one default parameter value and no
@JvmOverloads.
public fun openSocket(host: String,port: Int = 80,timeout: Int = 30,) { }
Rationale
Without @JvmOverloads, Java callers must pass every argument. The generated overloads let them
use the defaults.
Don't
public fun connect(host: String,port: Int = 80,timeout: Int = 30,) { }
Do
@JvmOverloadspublic fun connect(host: String,port: Int = 80,timeout: Int = 30,) { }
Notes
The check excludes:
- Abstract functions, interface members, and annotation class constructors.
suspendfunctions and members of value classes.- Overrides and
@JvmSyntheticdeclarations. @PublishedApi internaldeclarations.
Exemption
If this is intended, apply @IntentionallyWithoutJvmOverloads to the function or
constructor.
Use the exemption when the declaration is part of the API design or must stay for backwards compatibility.
public class ConnectionConfig@IntentionallyWithoutJvmOverloads(description = "Kotlin-only function. " +"Java callers are expected to use the builder instead.",reason = ExemptionReason.IGNORE_JAVA_INTEROP,)public fun connect(host: String,port: Int = 80,timeout: Int = 30,builder: ConnectionConfig.() -> Unit,) { }
Configuration
build.gradle.kts
apiWatchdog {javaInterop {defaultParametersWithoutJvmOverloads = WatchdogSeverity.ERROR}}
javaInterop { enabled = false } disables all Java interop checks.
For a direct compiler call:
-P plugin:org.jetbrains.kotlinx.library.api.watchdog:diagnosticSeverity=DEFAULT_PARAMETERS_WITHOUT_JVM_OVERLOADS:error