Kotlin中的注释数组

时间:2018-08-08 19:05:12

标签: hibernate kotlin annotations hibernate-annotations

相当于kotlin中的以下注释用法:

@TypeDefs({
        @TypeDef(name = "string-array", typeClass = StringArrayType.class),
        @TypeDef(name = "int-array", typeClass = IntArrayType.class),
        @TypeDef(name = "json", typeClass = JsonStringType.class),
        @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
})

这是@TypeDefs的定义

@Target({TYPE, PACKAGE})
@Retention(RUNTIME)
public @interface TypeDefs {
    /**
     * The grouping of type definitions.
     */
    TypeDef[] value();
}
public class Foo {...}

这是@TypeDef

之一
@Target({TYPE, PACKAGE})
@Retention(RUNTIME)
@Repeatable(TypeDefs.class)
public @interface TypeDef {
    /**
     * The type name.  This is the name that would be used in other locations.
     */
    String name() default "";

    /**
     * The type implementation class.
     */
    Class<?> typeClass();

    /**
     * Name a java type for which this defined type should be the default mapping.
     */
    Class<?> defaultForType() default void.class;

    /**
     * Any configuration parameters for this type definition.
     */
    Parameter[] parameters() default {};
}

我尝试使用arrayOf(),但无法正常工作。

@TypeDefs(arrayOf(
        @TypeDef(name = "string-array", typeClass = StringArrayType::class),
        @TypeDef(name = "int-array", typeClass = IntArrayType::class),
        @TypeDef(name = "json", typeClass = JsonStringType::class),
        @TypeDef(name = "jsonb", typeClass = JsonBinaryType::class)
))
data class Foo (...)

我在IntelliJ Idea上遇到以下错误:An annotation cannot be used as the annotations argument

2 个答案:

答案 0 :(得分:2)

我必须执行以下操作才能使其正常工作:

  • 从内部注释中删除@
  • arrayOf包裹列表
  • 将数组作为命名参数传递。

这是工作代码:

@TypeDefs(value = arrayOf(
        TypeDef(name = "string-array", typeClass = StringArrayType::class),
        TypeDef(name = "int-array", typeClass = IntArrayType::class),
        TypeDef(name = "json", typeClass = JsonStringType::class),
        TypeDef(name = "jsonb", typeClass = JsonBinaryType::class)
))
data class Line(...)

或者,也可以使用方括号代替arrayOf:

@TypeDefs(value = [
    TypeDef(name = "string-array", typeClass = StringArrayType::class),
    TypeDef(name = "int-array", typeClass = IntArrayType::class),
    TypeDef(name = "json", typeClass = JsonStringType::class),
    TypeDef(name = "jsonb", typeClass = JsonBinaryType::class)
])
data class Line(...)

更新:基于@Zoe的答案

如果唯一的参数是注释列表,则无需指定命名参数,并且将其简化为:

@TypeDefs(
    TypeDef(name = "string-array", typeClass = StringArrayType::class),
    TypeDef(name = "int-array", typeClass = IntArrayType::class),
    TypeDef(name = "json", typeClass = JsonStringType::class),
    TypeDef(name = "jsonb", typeClass = JsonBinaryType::class)
)
data class Line(...)

答案 1 :(得分:1)

如果您使用IntelliJ中的Convert file to Kotlin工具,则注释将转换为:

@TypeDefs(TypeDef(name = "string-array", typeClass = StringArrayType::class), 
    TypeDef(name = "int-array", typeClass = IntArrayType::class), 
    TypeDef(name = "json", typeClass = JsonStringType::class), 
    TypeDef(name = "jsonb", typeClass = JsonBinaryType::class))
data class Foo(...)

似乎与使用value = arrayOf(...)一样,如JaviOverflow所述