如何避免在各处重复相同的注释参数?
How can I avoid repeating the same annotation arguments everywhere?
我正在使用一个需要参数列表的库中的注解,并且在我的项目中有很多 classes 使用这个注解和那些完全相同的参数。
@AnnotationFromDependency(values = [long, list, of, variables, that, might, change])
class Foo {
fun bar() {
println("dostuff")
}
}
如何避免将 @AnnotationDependency
的相同参数复制到每个使用它的 class?在变量中定义参数列表不起作用,因为它不是编译时常量。
val myValues = arrayOf("long", "list", "of", "values", "that", "might", "change")
@AnnotationFromDependency(values = myValues) // error: An annotation argument must be a compile-time constant
class Foo {
fun bar() {
println("dostuff")
}
}
定义自定义注解并使用第三方注解:
@AnnotationFromDependency(values = ["long", "list", "of", "variables", "that", "might", "change"])
annotation class MyAnnotation()
注意: 元注释是应用于其他注释的注释。
接下来,在需要的地方使用您的注释代替第三方注释:
@MyAnnotation
class Foo()
如果您需要修改值列表,您只需在一个地方进行。
我正在使用一个需要参数列表的库中的注解,并且在我的项目中有很多 classes 使用这个注解和那些完全相同的参数。
@AnnotationFromDependency(values = [long, list, of, variables, that, might, change])
class Foo {
fun bar() {
println("dostuff")
}
}
如何避免将 @AnnotationDependency
的相同参数复制到每个使用它的 class?在变量中定义参数列表不起作用,因为它不是编译时常量。
val myValues = arrayOf("long", "list", "of", "values", "that", "might", "change")
@AnnotationFromDependency(values = myValues) // error: An annotation argument must be a compile-time constant
class Foo {
fun bar() {
println("dostuff")
}
}
定义自定义注解并使用第三方注解:
@AnnotationFromDependency(values = ["long", "list", "of", "variables", "that", "might", "change"])
annotation class MyAnnotation()
注意: 元注释是应用于其他注释的注释。
接下来,在需要的地方使用您的注释代替第三方注释:
@MyAnnotation
class Foo()
如果您需要修改值列表,您只需在一个地方进行。