# No-arg compiler plugin The `no-arg` compiler plugin generates a zero-argument constructor for classes with a specific annotation. The generated constructor is annotated with `@Deprecated` at the `HIDDEN` level, so you can call it from Java but not from Kotlin. Frameworks can call it through reflection. For example, this allows frameworks such as the Java Persistence API (JPA) to instantiate Kotlin classes that don't declare a zero-argument constructor. Note: For JPA entities with Kotlin, you can use the [kotlin-jpa compiler plugin](kotlin-jpa-plugin.html). It specifies JPA annotations such as `@Entity`, `@Embeddable`, and `@MappedSuperclass` automatically. ## Declare an annotation The `no-arg` 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 that need a zero-argument constructor. For example, `NoArgAnnotation`: ```KOTLIN package com.example annotation class NoArgAnnotation ``` ## Apply the plugin ### Gradle Add the `no-arg` plugin in the `plugins {}` block of your `build.gradle(.kts)` file: Kotlin: ```KOTLIN plugins { kotlin("jvm") version "2.4.21" // The version of Kotlin to use kotlin("plugin.noarg") version "2.4.21" // The Kotlin no-arg plugin } ``` Groovy: ```GROOVY plugins { id "org.jetbrains.kotlin.jvm" version "2.4.21" id "org.jetbrains.kotlin.plugin.noarg" version "2.4.21" } ``` Then specify the annotation you declared earlier (for example, `com.example.NoArgAnnotation`) to generate a zero-argument constructor for annotated classes: ```GROOVY noArg { annotation("com.example.NoArgAnnotation") } ``` Enable the `invokeInitializers` option if you want the plugin to run the class initialization logic from the generated constructor. This option is disabled by default: ```GROOVY noArg { invokeInitializers = true } ``` ### Maven Add the plugin to your `pom.xml` file: ```XML 2.4.21 kotlin-maven-plugin org.jetbrains.kotlin ${kotlin.version} no-arg org.jetbrains.kotlin kotlin-maven-noarg ${kotlin.version} ``` ## Use the `no-arg` plugin When the plugin is applied, classes annotated with a registered annotation get a zero-argument constructor at compile time. You don't need to declare the constructor manually: ```KOTLIN package com.example // The no-arg plugin generates a zero-argument constructor for this class @NoArgAnnotation class MyEntity(val id: Int) ``` ## What's next? * For JPA entities, use the [kotlin-jpa plugin](kotlin-jpa-plugin.html), which configures the `no-arg` and `all-open` plugins with JPA annotations such as `@Entity`, `@Embeddable`, and `@MappedSuperclass`. * Explore the source code of the [no-arg compiler plugin](https://github.com/JetBrains/kotlin/tree/master/plugins/noarg).