dimanche 14 mai 2023

Kotlin no-arg compiler plugin doesn't generate constructors

Because I need some way to initialize all variables in the noArg constructor, I've tried to apply no-arg Kotlin Compiler Plugin.

Build.gradle:

plugins {
    id "org.jetbrains.kotlin.plugin.noarg" version "1.8.0"
}

noArg {
    annotation("com.neutrino.game.utility.NoArg")
    invokeInitializers = true
}

I've also tried

buildscript {
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlinVersion"
    }
}

allprojects {
    apply plugin: "kotlin-noarg"
    noArg {
        annotation("com.neutrino.game.utility.NoArg")
        invokeInitializers = true
    }
}

NoArg.kt

package com.neutrino.game.utility

@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
@com.neutrino.game.utility.NoArg
annotation class NoArg

and finally

@NoArg
class Test(test0: String) {

    val test1 = 1
    val test2 = "test2"
}

However, this does not seem to work, regardless of @Target and @Retention.

Kotlin bytecode viewer does not generate the no-args constructor.

Test::class.java.constructors.forEach { 
    println("Parameters ${it.parameterCount}, Is synthetic? ${it.isSynthetic}") 
}

does not show any synthetic constructors generated either.

How to apply this compiler plugin and how to test if it works?

It is important for me that the no args constructor initializes all variables.





Aucun commentaire:

Enregistrer un commentaire