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.
Add the plugin and specify the list of annotations that must lead to generating a no-arg constructor for the annotated classes.
buildscript {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version"
}
}
apply plugin: "kotlin-noarg"
Or using the Gradle plugins DSL:
plugins {
id "org.jetbrains.kotlin.plugin.noarg" version "%kotlinVersion%"
}
Then specify the list of no-arg annotations:
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
}
<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>
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.
That's how you add the plugin in Gradle:
buildscript {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version"
}
}
apply plugin: "kotlin-jpa"
Or using the Gradle plugins DSL:
plugins {
id "org.jetbrains.kotlin.plugin.jpa" version "%kotlinVersion%"
}
In Maven, enable the jpa
plugin:
<compilerPlugins>
<plugin>jpa</plugin>
</compilerPlugins>
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