When annotation has an array argument with basic type like String or Int it is straightforward how to use that:
public @interface MyAnnotation{
String[] props();
}
@MyAnnotation(props = ["A", "B", "C"])
class Foo {}
Unfortunately that does not work for values that are annotations themselves.
An example is org.springframework.context.annotation.PropertySources
:
public @interface PropertySources {
PropertySource[] value();
}
public @interface PropertySource {
String[] value();
}
In Java syntax usage is
@PropertySources({
@PropertySource({"A", "B", "C"}),
@PropertySource({"D", "E", "F"}),
})
class Foo{}
But in Kotlin the code with similar approach does not compile
@PropertySources([
@PropertySource(["A", "B", "C"]),
@PropertySource(["D", "E", "F"]),
])
class Foo{}
How to express this annotation array nesting construction in Kotlin?
CodePudding user response:
Add value =
and remove the @
from the child annotation declaration:
@PropertySources(value = [
PropertySource("a", "b"),
PropertySource("d", "e"),
])
class Foo
Also note that @PropertySource
is @Repeatable
so you can do:
@PropertySource("a", "b")
@PropertySource("d", "e")
class Foo