java记录的构造函数注解

1bqhqjot  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(313)

有没有一种方法可以得到一个类似constructorproperty的注解 @Target(CONSTRUCTOR) 注解Java16记录的生成构造函数?例如。:

@ConstructorProperties({"id", "filename"})
public record Person(long id, String filename) {}

这会导致以下错误:

java: annotation type not applicable to this kind of declaration
xwmevbvl

xwmevbvl1#

这起作用了:

public record Person(long id, String filename) {
    @ConstructorProperties({"id", "filename"})
    public Person {}
}

我的理解是,没有参数列表的内部构造函数是向使用组件列表创建的默认构造函数添加逻辑的一种方法。很明显,添加一个构造函数注解,它的最终结果就是我想要的:)

相关问题