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

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

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


当前回答

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

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

其他回答

还有长期维护的风险。首先,我建议你阅读一下Lombok的实际工作原理,比如这里有一些来自开发人员的回答。

官方网站还列出了一些缺点,包括Reinier Zwitserloot的这句话:

It's a total hack. Using non-public API. Presumptuous casting (knowing that an annotation processor running in javac will get an instance of JavacAnnotationProcessor, which is the internal implementation of AnnotationProcessor (an interface), which so happens to have a couple of extra methods that are used to get at the live AST). On eclipse, it's arguably worse (and yet more robust) - a java agent is used to inject code into the eclipse grammar and parser class, which is of course entirely non-public API and totally off limits. If you could do what lombok does with standard API, I would have done it that way, but you can't. Still, for what its worth, I developed the eclipse plugin for eclipse v3.5 running on java 1.6, and without making any changes it worked on eclipse v3.4 running on java 1.5 as well, so it's not completely fragile.

总之,虽然Lombok可以节省一些开发时间,但如果有一个不向后兼容的javac更新(例如漏洞缓解),Lombok可能会让您陷入旧版本的Java,而开发人员则会争先恐后地更新他们对那些内部api的使用。这是否是一个严重的风险显然取决于项目。

想要使用lombok的@ToString,但很快在Intellij IDEA中重新构建项目时遇到了随机编译错误。在增量编译成功完成之前,必须多次点击编译。

使用Intellij IDEA 12.1.6和13.0在jdk 1.6.0_39和1.6.0_45下尝试了lombok 1.12.2和0.9.3,没有任何lombok插件。

不得不手动从delomboked源复制生成的方法,并将lombok搁置,直到更好的时机。

更新

只有启用并行编译时才会出现这个问题。

提交一个问题: https://github.com/rzwitserloot/lombok/issues/648

更新

mplushnikov于2016年1月30日评论道: 更新版本的Intellij 不再有这样的问题了。我想这里可以关了。

更新

如果可能的话,我强烈建议从Java+Lombok切换到Kotlin。 因为它已经从头开始解决了Lombok试图解决的所有Java问题。

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

我读了一些关于龙目岛的意见,实际上我在一些项目中使用它。

嗯,第一次接触龙目岛时,我的印象很差。几周后,我开始喜欢上它了。但几个月后,我发现了很多使用它的小问题。所以,我对龙目岛的最终印象并不是那么积极。

我这么想的理由是:

IDE plugin dependency. The IDE support for Lombok is through plugins. Even working good in most part of the time, you are always a hostage from this plugins to be maintained in the future releases of the IDEs and even the language version (Java 10+ will accelerate the development of the language). For example, I tried to update from Intellij IDEA 2017.3 to 2018.1 and I couldn't do that because there was some problem on the actual lombok plugin version and I needed to wait the plugin be updated... This also is a problem if you would like to use a more alternative IDE that don't have any Lombok plugin support. 'Find usages' problem.. Using Lombok you don't see the generated getter, setter, constructor, builder methods and etc. So, if you are planning to find out where these methods are being used in your project by your IDE, you can't do this only looking for the class that owns this hidden methods. So easy that the developers don't care to break the encapsulation. I know that it's not really a problem from Lombok. But I saw a bigger tendency from the developers to not control anymore what methods needs to be visible or not. So, many times they are just copying and pasting @Getter @Setter @Builder @AllArgsConstructor @NoArgsConstructor annotations block without thinking what methods the class really need to be exposed. Builder Obssession ©. I invented this name (get off, Martin Fowler). Jokes apart, a Builder is so easy to create that even when a class have only two parameters the developers prefer to use @Builder instead of constructor or a static constructor method. Sometimes they even try to create a Builder inside the lombok Builder, creating weird situations like MyClass.builder().name("Name").build().create(). Barriers when refactoring. If you are using, for example, a @AllArgsConstructor and need to add one more parameter on the constructor, the IDE can't help you to add this extra parameter in all places (mostly, tests) that are instantiating the class. Mixing Lombok with concrete methods. You can't use Lombok in all scenarios to create a getter/setter/etc. So, you will see these two approaches mixed in your code. You get used to this after some time, but feels like a hack on the language.

就像另一个回答说的,如果您对Java的冗长感到愤怒,并使用Lombok来处理它,那么可以尝试Kotlin。

龙目岛很棒,但是……

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:这两个库现在可以开箱即用。不过,对于其他注释处理器来说,这个问题仍然存在。