import java.lang.annotation.RetentionPolicy
import java.lang.annotation.Retention
import kotlin.reflect.KFunction1
Retention(RetentionPolicy.RUNTIME)
annotation class special(val why: String)
open class Model(val str: String) {
open fun print() {
println(str)
}
}
special(“Mode 1 annotation”)
open class Model1(str: String) : Model(str)
special(“Mode 2 annotation”)
open class Model2(str: String) : Model(str)
class Factory(val func: KFunction1<String, Model>) {
fun gen(): Model? {
// Can't get the Model's special annotation, how to get it?
val str = func.javaClass.getAnnotation(javaClass<special>())?.why?:"error: $func"
return func.invoke(str)
}
}
fun main(args: Array<String>) {
Factory(::Model1).gen()?.print()
Factory(::Model2).gen()?.print()
}