使用 Lombok 为带注释的代码生成 Java getter/setter
我使用 Project Lombok 为 Java 类的所有字段自动生成 getter 和 setter 方法。
当某个字段使用例如 @XmlTransient
进行注释时,该注释不会传播到生成的 getter/setter 方法,因此在生成的代码中不会显示任何效果。有没有办法将自动 getter/setter 生成与进一步的注释结合使用?
I use Project Lombok to automatically generate getter and setter methods for all fields of a Java class.
When a field is annotated with e.g. @XmlTransient
, the annotation is not propagated to the generated getter/setter methods, thus in the resulting code it does not show any effect. Is there a way to use automatic getter/setter generation in conjunction with further annotations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为将注释传播到 getter/setter 方法在一般情况下不起作用。例如,您知道如果字段和 bean 属性都用
@XmlElement
注释会发生什么吗?对于 JAXB 注释,我建议在字段上使用@XmlAccessorType(XmlAccessType.FIELD)
。I don't think propagating annotations to getter/setter methods would work in the general case. For example do you know what would happen if both the field and the bean properties were annotated with
@XmlElement
? For JAXB annotations I would suggest using@XmlAccessorType(XmlAccessType.FIELD)
on the field.