Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java-annotation parameters in Kotlin

Tags:

kotlin

I'm experimenting with Kotlin and I have a following Java-annotation

@Target( { TYPE })
@Retention(RUNTIME)
public @interface View {
    String[] url() default "";
    Class<? extends Component> parent() default Component.class;
}

and in Java-code it is used in following way

@View(url="/", parent=RootView.class)
public class FrontView extends Component {
}

How is that expressed in Kotlin? I have tried

[View(url=Array<String>("/"), parent=Class<RootView>)]
class FrontView : Component() {
}

but it does not compile. I only get type mismatch errors.

Type mismatch.  
Required: jet.Array<jet.String?>?  
Found: jet.Array<T>

and

Type mismatch
Required: java.lang.Class<out net.contextfw.web.application.component.Component?>?
Found: java.lang.Class<T>
like image 767
M.L. Avatar asked Jul 25 '12 08:07

M.L.


People also ask

Do Java annotations work in Kotlin?

annotation. Repeatable meta-annotation. This will make it repeatable both in Kotlin and Java. Java repeatable annotations are also supported from the Kotlin side.

What is @GET annotation in Kotlin?

By prefixing the annotation with @get: the resulting bytecode will have the annotation on the generated getter function: // Decompiled from the resulting Kotlin Bytecode @SomeAnnotation @Nullable public final String getSomeProperty() { return this. someProperty; }

How do you inherit annotations?

Annotations, just like methods or fields, can be inherited between class hierarchies. If an annotation declaration is marked with @Inherited , then a class that extends another class with this annotation can inherit it. The annotation can be overridden in case the child class has the annotation.


1 Answers

I found the solution. The syntax seems to be like this:

[View(url=array("/"), parent=javaClass<RootView>())]
class FrontView() : Component() {
}
like image 197
M.L. Avatar answered Oct 05 '22 12:10

M.L.