Project Lombok 与 Eclipse 模板/代码生成
与 Eclipse 中的代码模板/代码生成相比,Project Lombok 是否提供任何优势?有什么缺点吗(除了包含 .jar 之外)?
Does Project Lombok offer any benefit compared to code templates / code generation in Eclipse? Are there any downsides (apart from including the .jar)?.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Lombok 的优点之一是,一旦您使用 @Data 注释等注释了一个类,您就无需在进行更改时重新生成代码。例如,如果添加一个新字段,@Data 会自动将该字段包含在 equals、hashCode 和 toString 方法中。使用 Eclipse 生成的方法时,您需要手动进行更改。有时,您可能更喜欢手动控制,但在大多数情况下,我预计不会。
One advantage of Lombok is that once you've annotated a class with, say, the @Data annotation, you never need to regenerate the code when you make changes. For example, if you add a new field, @Data would automatically include that field in the equals, hashCode and toString methods. You'd need to manually make that change when using Eclipse generated methods. Some of the time, you may prefer the manual control but for most cases, I expect not.
Lombok 的优点是代码实际上并不存在——即类更具可读性并且不混乱。
The advantage of Lombok is that the code isn't actually there - i.e. classes are much more readable and are not cluttered.
优点:
类似“struct”的内部类缩小到最低限度:
这将创建 getter 和 setter、toString() 以及包含这两个变量的正确 hash() / equals() 方法。
带有
@Value
的变体创建了一个不可变的结构(没有 setter,所有字段都是最终的)。内容 缺点:
value
还不会重命名getValue()
和setValue()
toString
输出不如来自 apache commons 的 ToStringBuilderAdvantages:
'struct'-like inner classes shrink to a bare minimum:
This will create both getters and setters, a toString(), and correct hash() / equals() methods including both variables.
The variant with
@Value
creates an immutable structure (no setters, all fields final).Disadvantages:
value
above will not (yet) renamegetValue()
andsetValue()
toString
output not as nice as, for instance, ToStringBuilder from apache commons很少有人想到:
依赖问题不是虽然被忽视了,但你已经将其从你的问题中排除了。
Very few come to mind:
The dependency issue is not to be overlooked though, but you have excluded it from your question.
Eclipse EMF 提供了一些 Lombok 尚不支持的非常方便的功能:
Eclipse EMF offers some features which are very handy which Lombock does not yet support: