Skip to main content

Subclass opt-in without markers

DiagnosticSUBCLASS_OPT_IN_WITHOUT_MARKERS
Default severityError
Gradle propertysubclassOptInWithoutMarkers
Exemptionnone, replace with @IntentionallyOpen

What it reports​

A markerless @SubclassOptInRequired on a class or interface in the public API that external classes can subclass.

@SubclassOptInRequired
public abstract class ExtensionPoint

Rationale​

Without a marker class, @SubclassOptInRequired doesn't restrict subclassing. External subclasses can depend on the API without an opt-in to future changes.

Don't​

@SubclassOptInRequired
public abstract class Connector

Do​

@RequiresOptIn
public annotation class UnstableApi
@SubclassOptInRequired(UnstableApi::class)
public abstract class Connector

Notes​

  • Final classes, classes whose constructors are all internal or private, and sealed types aren't reported.
  • The annotation can list more than one marker class.
  • @PublishedApi internal types aren't reported.
  • To keep unrestricted subclassing for compatibility, replace @SubclassOptInRequired with @IntentionallyOpen. The exemptExistingApi task makes this replacement.

Configuration​

build.gradle.kts
apiWatchdog {
subclassOptInWithoutMarkers = WatchdogSeverity.ERROR
}

For a direct compiler call:

-P plugin:org.jetbrains.kotlinx.library.api.watchdog:diagnosticSeverity=SUBCLASS_OPT_IN_WITHOUT_MARKERS:error

See also​