草庐IT

Java 互操作性 : how to declare a compile-time array constant in Kotlin?

coder 2023-05-09 原文

我有这个 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 找到:kotlin.String

我试过了

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

得到“Error:(20, 23) Kotlin: An annotation parameter must be a compile-time constant”

如何才能满足 Kotlin 编译器的要求?

Java 只接受

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

最佳答案

你在 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 {

关于Java 互操作性 : how to declare a compile-time array constant in Kotlin?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29561395/

有关Java 互操作性 : how to declare a compile-time array constant in Kotlin?的更多相关文章

随机推荐