gpt4 book ai didi

hibernate - kotlin 中的注释数组

转载 作者:行者123 更新时间:2023-12-04 08:56:35 29 4
gpt4 key购买 nike

在 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

最佳答案

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

  • 从内部注解中去掉@
  • 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(...)

关于hibernate - kotlin 中的注释数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51753961/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com