Project Lombok 与 Eclipse 模板/代码生成

发布于 2024-08-28 14:27:48 字数 133 浏览 4 评论 0原文

与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

驱逐舰岛风号 2024-09-04 14:27:48

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.

浮萍、无处依 2024-09-04 14:27:48

Lombok 的优点是代码实际上并不存在——即类更具可读性并且不混乱。

The advantage of Lombok is that the code isn't actually there - i.e. classes are much more readable and are not cluttered.

一江春梦 2024-09-04 14:27:48

优点:

  • 非常易于使用,
  • 类更加干净(“无样板代码”),尤其是
    类似“struct”的内部类缩小到最低限度:
@Data 
private class AttrValue {
  private String attribute;
  private MyType value;
}

这将创建 getter 和 setter、toString() 以及包含这两个变量的正确 hash() / equals() 方法。
带有 @Value 的变体创建了一个不可变的结构(没有 setter,所有字段都是最终的)。

  • 更改字段(getter、setter、toString、hash、equals)时无需生成/删除代码。不会
  • 干扰手动编码方法:只需在需要时将您自己的特定 setter 添加到类中即可。 Lombok 会跳过此步骤并生成其他所有

内容 缺点:

  • 尚无名称重构:重命名上面的 value 还不会重命名 getValue()setValue()
  • 可能会稍微减慢 ecplise
  • toString 输出不如来自 apache commons 的 ToStringBuilder

Advantages:

  • Very easy to use
  • Classes are much cleaner ('no boilerplate code'),especially
    'struct'-like inner classes shrink to a bare minimum:
@Data 
private class AttrValue {
  private String attribute;
  private MyType value;
}

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

  • No need to generate/remove code when you change fields (getters, setters, toString, hash, equals)
  • No interference with hand-coded methods: just add you own specific setter to the class where needed. Lombok skips this and generates everything else

Disadvantages:

  • No name refactoring, yet: renaming value above will not (yet) rename getValue() and setValue()
  • May slows down ecplise slightly
  • toString output not as nice as, for instance, ToStringBuilder from apache commons
掐死时间 2024-09-04 14:27:48

很少有人想到:

  • 它是基于注释的,因此对于仍在 Java5 之前的遗留项目没有好处( delombok 可以提供帮助)。实际上,它需要使用javac v1.6编译器。
  • 它仍然存在关于多个构造函数的限制

依赖问题不是虽然被忽视了,但你已经将其从你的问题中排除了。

Very few come to mind:

  • it is based on annotation, so no good for legacy project still in pre-Java5 (delombok can help). Actually, it requires using the javac v1.6 compiler.
  • it still have limitations regarding multiple constructors

The dependency issue is not to be overlooked though, but you have excluded it from your question.

沉鱼一梦 2024-09-04 14:27:48

Eclipse EMF 提供了一些 Lombok 尚不支持的非常方便的功能:

  • 强大的通知机制,用于了解实例中的更改
  • 无需 java 反射的通用 API。访问和修改实例,无需对类型进行强
  • 引用 命令和基于 API 的编辑
  • 模型之间的交叉引用:创建和加载模型树,EMF 通过为交叉引用创建代理来处理加载。这可以节省内存并提高巨大域树的性能
  • 还有更多...

Eclipse EMF offers some features which are very handy which Lombock does not yet support:

  • Powerful notification mechanims to get informed about changes in your instances
  • Generic API without java reflection. Access and modify instances without a strong reference to the type
  • Command und API based editing
  • Cross references between models: Create and load model trees and EMF handles the loading by creating a proxy for the cross reference. This saves memory and boost performance in huge domain trees
  • And much more...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文