hibernatekotlinannotationshibernate-annotations

Array of anotations in kotlin


What would be the equivalent of the following annotation usage in 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)
})

This is the definition of @TypeDefs

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

and this is the one of @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 {};
}

I tried using arrayOf() but it's not working.

@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 (...)

I'm getting the following error on IntelliJ Idea: An annotation cannot be used as the annotations argument


Solution

  • I had to do the following to make it work:

    Here is the working code:

    @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(...)
    

    alternatively one can also use square brackets instead of 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(...)
    

    UPDATE: Based on @Zoe's answer

    If the only argument is the annotation list, you don't need to specify the named argument and it gets simplified as:

    @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(...)