Java互操作性:如何在Kotlin中声明编译时数组常量?

时间:2015-04-10 12:24:46

标签: java kotlin

我有这个Java annotation declaration并希望在Kotlin中使用它

class CurlCommand {
    Parameter(names = "-groups", description = "Comma-separated list of group names to be run")
    var groups: Array<String>? = null
}

编译器报告 TYPE_MISMATCH    必需:kotlin.Array&lt; kotlin.String&gt;    发现:kotlin.String

我试过

Parameter(names = Array<String>(1, {i-> "-groups"}), description = "Comma-separated list of group names to be run")
var groups: Array<String>? = null

并得到“错误:(20,23)Kotlin:注释参数必须是编译时常量”

我如何满足Kotlin编译器?

Java只接受

@Parameter(names = "-groups", description = "Comma-separated list of group names to be run")
public String groups;

2 个答案:

答案 0 :(得分:5)

你在Kotlin中声明一个常量,如下:

const val LG_PACKAGE = "com.myapp"

但是,kotlin documentation for compile-time constants表示它们只能是String类型或基本类型。因此,如果要使用常量,最接近的是:

const val LG_PACKAGE = "com.myapp"

@EnableJpaRepositories(basePackages = arrayOf(LG_PACKAGE))
@EntityScan(basePackages = arrayOf(LG_PACKAGE))
open class LgApp {

答案 1 :(得分:1)

有时答案非常简单,一旦人们意外发现它

array("-groups")

虽然Kotlin转换器给了我这个代码

@Parameter(names = "-groups", description = "Comma-separated list of group names to be run")
public String groups;

正如我上面提到的那样。