如果你不知道,Project Lombok帮助解决了Java的一些麻烦,比如用注释生成getter和setter,甚至是简单的JavaBean,比如用@Data生成。它真的可以帮助我,特别是在50个不同的事件对象中,你有多达7个不同的字段需要用getter来构造和隐藏。我可以用这个删除几乎一千行代码。

然而,我担心从长远来看,这将是一个后悔的决定。当我提到它的时候,火焰战争就会在##Java Freenode频道爆发,提供代码片段会让可能的助手感到困惑,人们会抱怨缺少JavaDoc,而未来的提交者可能无论如何都会删除它。我很享受积极的一面,但我担心消极的一面。

那么:在任何项目中使用Lombok安全吗?积极的影响抵得上消极的影响吗?


当前回答

我遇到了Lombok和Jackson CSV的问题,当我将我的对象(java bean)编组到CSV文件时,其中的列重复,然后我删除了Lombok的@Data注释,编组工作正常。

其他回答

我对Lombok的看法是,它只是为编写样板Java代码提供了快捷方式。 当涉及到使用快捷方式来编写Java代码时,我会依赖IDE提供的这些特性——就像在Eclipse中一样,我们可以转到Source > Generate Getters and Setters菜单来生成getter和setter。 我不会依赖Lombok这样的库:

It pollutes your code with an indirection layer of alternative syntax (read @Getter, @Setter, etc. annotations). Rather than learning an alternative syntax for Java, I would switch to any other language that natively provides Lombok like syntax. Lombok requires the use of a Lombok supported IDE to work with your code. This dependency introduces a considerable risk for any non-trivial project. Does the open source Lombok project have enough resources to keep providing support for different versions of a wide range of Java IDE's available? Does the open source Lombok project have enough resources to keep providing support for newer versions of Java that will be coming in future? I also feel nervous that Lombok may introduce compatibility issues with widely used frameworks/libraries (like Spring, Hibernate, Jackson, JUnit, Mockito) that work with your byte code at runtime.

总而言之,我不喜欢用龙目岛来“调味”我的爪哇。

我遇到了Lombok和Jackson CSV的问题,当我将我的对象(java bean)编组到CSV文件时,其中的列重复,然后我删除了Lombok的@Data注释,编组工作正常。

继续使用Lombok吧,如果有必要,您可以在之后“delombok”您的代码http://projectlombok.org/features/delombok.html

当我向我的团队展示项目时,他们的热情很高,所以我认为你不应该害怕团队的反应。

就ROI而言,集成它很简单,并且不需要对其基本形式进行代码更改。(只需向类中添加一个注释) 最后,如果您改变了主意,您可以运行unlombok,或让IDE创建这些setter、getter和ctor(我认为一旦他们看到您的pojo变得多么清晰,就没有人会要求它们了)

龙目岛很棒,但是……

Lombok打破了注释处理的规则,因为它不生成新的源文件。这意味着它不能与其他注释处理器一起使用,如果它们期望getter /setter或其他任何存在的东西。

Annotation processing runs in a series of rounds. In each round, each one gets a turn to run. If any new java files are found after the round is completed, another round begins. In this way, the order of annotation processors doesn't matter if they only generate new files. Since lombok doesn't generate any new files, no new rounds are started so some AP that relies on lombok code don't run as expected. This was a huge source of pain for me while using mapstruct, and delombok-ing isn't a useful option since it destroys your line numbers in logs.

我最终破解了一个构建脚本,可以同时使用lombok和mapstruct。但我想放弃龙目岛,因为它太粗糙了——至少在这个项目中是这样。我一直在用龙目岛做其他东西。 更新到mapstruct+lombok:这两个库现在可以开箱即用。不过,对于其他注释处理器来说,这个问题仍然存在。