vendredi 22 novembre 2019

Kotlin noarg plugin: Constructor only accessible by java reflection

There is something wrong in my setup, but i can't figure out what it is. From Kotlin compiler plugins i have following information.

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.

That said, i would think i could access the noarg constructor by both java and kotlin reflection, but i can only access it by java reflection. Is this the intended behaviour, or am i doing something wrong?

build.gradle

plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.3.50'
    id "org.jetbrains.kotlin.plugin.noarg" version "1.3.50"
    id 'application'
    id 'java'
}

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
    compile group: 'org.jetbrains.kotlin', name: 'kotlin-reflect', version: '1.3.50'
}

noArg {
    annotation('noargdemo.Entity')
}

application {
    mainClassName = 'noargdemo.AppKt'
}
package noargdemo

import kotlin.reflect.full.createInstance

@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
annotation class Entity

@Entity
data class Employee(
        val firstName : String,
        val secondName : String
)

fun main(args: Array<String>) {
    val kotlinConstructors = Employee::class.constructors // size is 1
    val javaConstructors = Employee::class.java.constructors // size is 2
    val instance1 = Employee::class.java.getConstructor().newInstance() // works
    val instance2 = Employee::class.createInstance() // doesnt work
}




Aucun commentaire:

Enregistrer un commentaire