All-open compiler plugin
In Kotlin, classes and their members are final by default. This can cause issues with frameworks and libraries that rely on subclassing and runtime proxies, which require classes to be open.
The all-open compiler plugin makes classes and their members open at compile time without requiring the open keyword. You need to create an annotation, add it to the plugin options, and annotate the classes you want to make open:
If a class or any of its superclasses is annotated with a configured annotation, the class and its members become open.
Declare an annotation
The all-open plugin only processes classes marked with an annotation that you register in its configuration. So before enabling the plugin, declare an annotation to mark the classes you want to make open. For example, AllOpenMarker:
Apply the plugin
Gradle
To enable the plugin, add it to the plugins {} block of your build.gradle(.kts) file:
Then specify the annotation you declared earlier (for example, com.example.AllOpenMarker) or a list of annotations that make classes open:
Maven
Add the plugin in your pom.xml file:
Use the all-open plugin
If a class or any of its superclasses has the com.example.AllOpenMarker annotation you created, the class and all its members become open:
The plugin also works with meta-annotations. If you mark another annotation with @AllOpenMarker, all classes and members annotated with it become open:
What's next?
For Spring applications, use the
kotlin-springplugin, a preconfigured wrapper that makes classes with Spring annotationsopen.For JPA entities, use the
kotlin-jpaplugin, which makes entitiesopenand generates zero-argument constructors for them.Explore the source code of the
all-opencompiler plugin.