Kotlin中的Java注释参数

M.L*_*.L. 10 kotlin

我正在尝试使用Kotlin,我有一个以下的Java注释

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

在Java代码中,它以下列方式使用

@View(url="/", parent=RootView.class)
public class FrontView extends Component {
}
Run Code Online (Sandbox Code Playgroud)

如何在Kotlin中表达出来?我试过了

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

但它没有编译.我只得到类型不匹配错误.

Type mismatch.  
Required: jet.Array<jet.String?>?  
Found: jet.Array<T>
Run Code Online (Sandbox Code Playgroud)

Type mismatch
Required: java.lang.Class<out net.contextfw.web.application.component.Component?>?
Found: java.lang.Class<T>
Run Code Online (Sandbox Code Playgroud)

M.L*_*.L. 8

我找到了解决方案.语法似乎是这样的:

[View(url=array("/"), parent=javaClass<RootView>())]
class FrontView() : Component() {
}
Run Code Online (Sandbox Code Playgroud)

  • 语法已经改变,在Kotlin中,现在注释使用@并且与Java语法一致.附加语法用于在有多个选项时定位注释.完整文档现在位于https://kotlinlang.org/docs/reference/annotations.html (6认同)
  • 你甚至可以放弃[] (2认同)