Kotlin Help

No-arg compiler plugin

The no-arg compiler plugin generates an additional zero-argument constructor for classes with a specific annotation.

The generated constructor is synthetic, so it can't be directly called from Java or Kotlin, but it can be called using reflection.

This allows the Java Persistence API (JPA) to instantiate a class although it doesn't have the zero-parameter constructor from Kotlin or Java point of view (see the description of kotlin-jpa plugin below).

In your Kotlin file

Add new annotations to mark the code that needs a zero-argument constructor:

package com.my annotation class Annotation

Gradle

Add the plugin using Gradle's plugins DSL:

plugins { kotlin("plugin.noarg") version "2.0.21" }
plugins { id "org.jetbrains.kotlin.plugin.noarg" version "2.0.21" }

Then specify the list of no-arg annotations that must lead to generating a no-arg constructor for the annotated classes:

noArg { annotation("com.my.Annotation") }

Enable invokeInitializers option if you want the plugin to run the initialization logic from the synthetic constructor. By default, it is disabled.

noArg { invokeInitializers = true }

Maven

<plugin> <artifactId>kotlin-maven-plugin</artifactId> <groupId>org.jetbrains.kotlin</groupId> <version>${kotlin.version}</version> <configuration> <compilerPlugins> <!-- Or "jpa" for JPA support --> <plugin>no-arg</plugin> </compilerPlugins> <pluginOptions> <option>no-arg:annotation=com.my.Annotation</option> <!-- Call instance initializers in the synthetic constructor --> <!-- <option>no-arg:invokeInitializers=true</option> --> </pluginOptions> </configuration> <dependencies> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-noarg</artifactId> <version>${kotlin.version}</version> </dependency> </dependencies> </plugin>

JPA support

As with the kotlin-spring plugin wrapped on top of all-open, kotlin-jpa is wrapped on top of no-arg. The plugin specifies @Entity, @Embeddable, and @MappedSuperclass no-arg annotations automatically.

Add the plugin using the Gradle plugins DSL:

plugins { kotlin("plugin.jpa") version "2.0.21" }
plugins { id "org.jetbrains.kotlin.plugin.jpa" version "2.0.21" }

In Maven, enable the jpa plugin:

<compilerPlugins> <plugin>jpa</plugin> </compilerPlugins>

Command-line compiler

Add the plugin JAR file to the compiler plugin classpath and specify annotations or presets:

-Xplugin=$KOTLIN_HOME/lib/noarg-compiler-plugin.jar -P plugin:org.jetbrains.kotlin.noarg:annotation=com.my.Annotation -P plugin:org.jetbrains.kotlin.noarg:preset=jpa
Last modified: 30 August 2023