Reputation: 80
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
}
Upvotes: 1
Views: 1423
Reputation: 3695
This is intended behaviour, it's stated even in that description your quoted:
The generated constructor is synthetic so it can’t be directly called from Java or Kotlin, but it can be called using reflection.
Synthetic methods are methods generated by the compiler for internal purposes, they can't be called manually from source code but they are visible for reflections.
You can check if method is synthetic by using Method.isSynthetic: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#isSynthetic--
Upvotes: 2