Reputation: 6419
Say I have an annotation in Java like this:
public @interface Foo {
Class<? extends Bar> value();
}
public interface Bar {}
How should I translate Foo
into kotlin? Is it true that I could only switch back to Java in such situation?
Upvotes: 1
Views: 1079
Reputation: 28056
Kotlin annotations cannot have method-definitions. They can, however, have constructor parameters. I think this works like you want it to:
import kotlin.reflect.KClass
interface Bar
class Foo : Bar
@Target(AnnotationTarget.FUNCTION)
annotation class Test(val value: KClass<out Bar>)
@Test(Foo::class)
fun testAnnotation() {
println("Test")
}
Notice the out modifier on the KClass
type parameter. It makes the type parameter covariant, meaning it can be of type Bar
or any type that implements Bar
.
Upvotes: 3