本文翻译自:Is it safe to use Project Lombok? [closed]

In case you don't know Project Lombok helps with some of the annoyances of Java with stuff like generating getters and setters with annotations and even simple JavaBean like generation with @Data . 如果您不知道Project Lombok可以帮助解决Java的一些烦恼,例如生成带注释的getter和setter,甚至像使用@Data生成简单的JavaBean 。 It could really help me, especially in 50 different event objects where you have up to 7 different fields that need to be constructed and hidden with getters. 它可以真正帮助我,特别是在50个不同的事件对象中,你有多达7个不同的字段需要构建和使用getter隐藏。 I could remove almost a thousand lines of code with this. 我可以删除近千行代码。

However, I'm worried that in the long run, it will be a regretful decision. 但是,我担心从长远来看,这将是一个令人遗憾的决定。 Flamewars will erupt in the ##Java Freenode channel when I mention it, providing code snippets will confuse possible helpers, people will complain about missing JavaDoc , and future commiters might just remove it all anyway. 当我提到它时,Flamewars会在##Java Freenode频道中爆发,提供代码片段会混淆可能的帮助者, 人们会抱怨丢失JavaDoc ,未来的委托人可能会将它全部删除。 I would really enjoy the positive, but I'm worried about the negative. 我真的很喜欢积极的,但我担心的是消极的。

So: Is it safe to use Lombok on any project, small or large? 那么:在任何项目中使用Lombok都是安全的吗? Are the positive effects worth the negatives? 积极影响是否值得消极?


#1楼

参考:https://stackoom.com/question/GA6V/使用Project-Lombok是否安全-关闭


#2楼

I have used Lombok in almost all my projects for one year but unfortunately removed it. 我在几乎所有的项目中都使用了龙目岛一年,但遗憾的是将其删除了。 In the beginning it was a very clean way of development but setting up the development environment for new team members is not very easy and straightforward. 一开始它是一种非常干净的开发方式,但为新团队成员建立开发环境并不容易和直截了当。 When it became a headache I just removed it. 当它变得头疼我刚刚删除它。 But it is a good work and needs some more simplicity to setting up. 但这是一项很好的工作,需要更简单的设置。


#3楼

Just started using Lombok today. 刚刚开始使用龙目岛。 So far I like it, but one drawback I didn't see mentioned was refactoring support. 到目前为止,我喜欢它,但我没有看到提到的一个缺点是重构支持。

If you have a class annotated with @Data , it will generate the getters and setters for you based on the field names. 如果您有一个使用@Data注释的类,它将根据字段名称为您生成getter和setter。 If you use one of those getters in another class, then decide the field is poorly named, it will not find usages of those getters and setters and replace the old name with the new name. 如果你在另一个类中使用其中一个getter,那么确定该字段命名不佳,它将找不到那些getter和setter的用法,并用新名称替换旧名称。

I would imagine this would have to be done via an IDE plug-in and not via Lombok. 我想这必须通过IDE插件而不是通过Lombok来完成。

UPDATE (Jan 22 '13) 更新(2013年1月22日)
After using Lombok for 3 months, I still recommend it for most projects. 使用Lombok 3个月后,我仍然推荐它用于大多数项目。 I did, however, find another drawback that is similar to the one listed above. 但是,我确实找到了与上面列出的相似的另一个缺点。

If you have a class, say MyCompoundObject.java that has 2 members, both annotated with @Delegate , say myWidgets and myGadgets , when you call myCompoundObject.getThingies() from another class, it's impossible to know if it's delegating to the Widget or Gadget because you can no longer jump to source within the IDE. 如果你有一个类,比如MyCompoundObject.java有2个成员,都注释了@Delegate ,比如myWidgetsmyGadgets ,当你从另一个类调用myCompoundObject.getThingies() ,就不可能知道它是否委托给WidgetGadget因为您无法再在IDE中跳转到源代码。

Using the Eclipse "Generate Delegate Methods..." provides you with the same functionality, is just as quick and provides source jumping. 使用Eclipse“Generate Delegate Methods ...”为您提供相同的功能,同样快速并提供源跳转。 The downside is it clutters your source with boilerplate code that take the focus off the important stuff. 缺点是它使用样板代码使您的源变得混乱,重点放在重要的东西上。

UPDATE 2 (Feb 26 '13) 更新2(2013年2月26日)
After 5 months, we're still using Lombok, but I have some other annoyances. 5个月后,我们仍在使用龙目岛,但我还有其他一些烦恼。 The lack of a declared getter & setter can get annoying at times when you are trying to familiarize yourself with new code. 当您尝试熟悉新代码时,缺少声明的getter和setter可能会变得很烦人。

For example, if I see a method called getDynamicCols() but I don't know what it's about, I have some extra hurdles to jump to determine the purpose of this method. 例如,如果我看到一个名为getDynamicCols()的方法,但我不知道它是什么,我有一些额外的障碍来跳转来确定这个方法的目的。 Some of the hurdles are Lombok, some are the lack of a Lombok smart plugin. 一些障碍是龙目岛,一些是缺乏Lombok智能插件。 Hurdles include: 障碍包括:

  • Lack of JavaDocs. 缺乏JavaDocs。 If I javadoc the field, I would hope the getter and setter would inherit that javadoc through the Lombok compilation step. 如果我对该字段进行javadoc,我希望getter和setter将通过Lombok编译步骤继承该javadoc。
  • Jump to method definition jumps me to the class, but not the property that generated the getter. 跳转到方法定义会跳转到类,但不会跳转到生成getter的属性。 This is a plugin issue. 这是一个插件问题。
  • Obviously you are not able to set a breakpoint in a getter/setter unless you generate or code the method. 显然,除非生成或编码方法,否则无法在getter / setter中设置断点。
  • NOTE: This Reference Search is not an issue as I first thought it was. 注意:此参考搜索不是我最初认为的问题。 You do need to be using a perspective that enables the Outline view though. 您确实需要使用启用Outline视图的透视图。 Not a problem for most developers. 大多数开发人员都不是问题。 My problem was I am using Mylyn which was filtering my Outline view, so I didn't see the methods. 我的问题是我正在使用Mylyn过滤我的Outline视图,所以我没有看到方法。 Lack of References search. 缺乏参考搜索。 If I want to see who's calling getDynamicCols(args...) , I have to generate or code the setter to be able to search for references. 如果我想看看谁在调用getDynamicCols(args...) ,我必须生成或编码setter才能搜索引用。

UPDATE 3 (Mar 7 '13) 更新3(2013年3月7日)
Learning to use the various ways of doing things in Eclipse I guess. 我想在Eclipse中学习使用各种方法做事。 You can actually set a conditional breakpoint (BP) on a Lombok generated method. 实际上,您可以在Lombok生成的方法上设置条件断点(BP)。 Using the Outline view, you can right-click the method to Toggle Method Breakpoint . 使用“ Outline视图,可以右键单击“ Toggle Method Breakpoint Then when you hit the BP, you can use the debugging Variables view to see what the generated method named the parameters (usually the same as the field name) and finally, use the Breakpoints view to right-click the BP and select Breakpoint Properties... to add a condition. 然后,当您点击BP时,您可以使用调试Variables视图来查看生成的方法命名参数(通常与字段名称相同),最后使用“ Breakpoints视图右键单击BP并选择“ Breakpoint Properties...添加条件。 Nice. 尼斯。

UPDATE 4 (Aug 16 '13) 更新4(2013年8月16日)
Netbeans doesn't like it when you update your Lombok dependencies in your Maven pom. 当您在Maven pom中更新Lombok依赖项时,Netbeans不喜欢它。 The project still compiles, but files get flagged for having compilation errors because it can't see the methods Lombok is creating. 该项目仍在编译,但文件因为编译错误而被标记,因为它无法看到Lombok正在创建的方法。 Clearing the Netbeans cache resolves the issue. 清除Netbeans缓存可以解决问题。 Not sure if there is a "Clean Project" option like there is in Eclipse. 不确定是否存在Eclipse中的“Clean Project”选项。 Minor issue, but wanted to make it known. 小问题,但想让它知道。

UPDATE 5 (Jan 17 '14) 更新5(2014年1月17日)
Lombok doesn't always play nice with Groovy, or at least the groovy-eclipse-compiler . Lombok并不总是和Groovy,或者至少是groovy-eclipse-compiler一起玩得很好。 You might have to downgrade your version of the compiler. 您可能必须降级编译器的版本。 Maven Groovy and Java + Lombok Maven Groovy和Java + Lombok

UPDATE 6 (Jun 26 '14) 更新6(2014年6月26日)
A word of warning. 一句警告。 Lombok is slightly addictive and if you work on a project where you can't use it for some reason, it will annoy the piss out of you. 龙目岛有点上瘾,如果你在一个你不能因某些原因使用它的项目上工作,它会让你烦恼不已。 You may be better off just never using it at all. 你最好永远不要使用它。

UPDATE 7 (Jul 23 '14) 更新7(2014年7月23日)
This is a bit of an interesting update because it directly addresses the safety of adopting Lombok that the OP asked about. 这是一个有趣的更新,因为它直接解决了OP所采用的Lombok的安全问题。

As of v1.14, the @Delegate annotation has been demoted to an Experimental status. 从v1.14开始,@ @Delegate注释已被降级为实验状态。 The details are documented on their site ( Lombok Delegate Docs ). 详细信息记录在他们的网站上( Lombok Delegate Docs )。

The thing is, if you were using this feature, your backout options are limited. 问题是,如果您使用此功能,您的退出选项是有限的。 I see the options as: 我看到的选项如下:

  • Manually remove @Delegate annotations and generate/handcode the delegate code. 手动删除@Delegate注释并生成/手动编写代理代码。 This is a little harder if you were using attributes within the annotation. 如果您在注释中使用属性,这会有点困难。
  • Delombok the files that have the @Delegate annotation and maybe add back in the annotations that you do want. Delombok具有@Delegate注释的文件,可能会添加到您想要的注释中。
  • Never update Lombok or maintain a fork (or live with using experiential features). 永远不要更新Lombok或维护分叉(或使用体验功能)。
  • Delombok your entire project and stop using Lombok. Delombok整个项目并停止使用Lombok。

As far as I can tell, Delombok doesn't have an option to remove a subset of annotations ; 据我所知, Delombok没有删除注释子集的选项 ; it's all or nothing at least for the context of a single file. 它至少对于单个文件的上下文是全有或全无。 I opened a ticket to request this feature with Delombok flags, but I wouldn't expect that in the near future. 我开了一张票,用Delombok旗帜请求这个功能 ,但我不希望在不久的将来。

UPDATE 8 (Oct 20 '14) 更新8(2014年10月20日)
If it's an option for you, Groovy offers most of the same benefits of Lombok, plus a boat load of other features, including @Delegate . 如果它是你的选择,Groovy提供了龙目岛的大部分相同的好处,以及其他功能的船载,包括@Delegate 。 If you think you'll have a hard time selling the idea to the powers that be, take a look at the @CompileStatic or @TypeChecked annotation to see if that can help your cause. 如果你认为你很难将这个想法卖给那些权力,那就看看@CompileStatic@TypeChecked注释,看看它是否有助于你的事业。 In fact, the primary focus of the Groovy 2.0 release was static safety . 实际上, Groovy 2.0版本的主要关注点是静态安全性 。

UPDATE 9 (Sep 1 '15) 更新9(2015年9月1日)
Lombok is still being actively maintained and enhanced , which bodes well to the safety level of adoption. 龙目岛仍在积极维护和增强 ,这预示着采用的安全水平。 The @Builder annotations is one of my favorite new features. @Builder注释是我最喜欢的新功能之一。

UPDATE 10 (Nov 17 '15) 更新10(2015年11月17日)
This may not seem directly related to the OP's question, but worth sharing. 这似乎与OP的问题没有直接关系,但值得分享。 If you're looking for tools to help you reduce the amount of boilerplate code you write, you can also check out Google Auto - in particular AutoValue . 如果您正在寻找可以帮助您减少编写的样板代码量的工具,您还可以查看Google Auto - 特别是AutoValue 。 If you look at their slide deck , the list Lombok as a possible solution to the problem they are trying to solve. 如果你看一下他们的幻灯片 ,列表龙目岛作为他们试图解决的问题的可能解决方案。 The cons they list for Lombok are: 他们为龙目岛列出的缺点是:

  • The inserted code is invisible (you can't "see" the the methods it generates) [ed note - actually you can, but it just requires a decompiler] 插入的代码是不可见的(你不能“看到”它生成的方法)[编号 - 实际上你可以,但它只需要一个反编译器]
  • The compiler hacks are non-standard and fragile 编译器黑客是非标准和脆弱的
  • "In our view, your code is no longer really Java" “在我们看来,你的代码不再是真正的Java”

I'm not sure how much I agree with their evaluation. 我不确定我对他们的评价多少赞同。 And given the cons of AutoValue that are documented in the slides, I'll be sticking with Lombok (if Groovy is not an option). 考虑到幻灯片中记录的AutoValue的缺点,我将坚持使用Lombok(如果Groovy不是一个选项)。

UPDATE 11 (Feb 8 '16) 更新11(2016年2月8日)
I found out Spring Roo has some similar annotations . 我发现Spring Roo有一些类似的注释 。 I was a little surprised to find out Roo is still a thing and finding documentation for the annotations is a bit rough. 我有点惊讶地发现Roo仍然是一个东西,找到注释的文档有点粗糙。 Removal also doesn't look as easy as de-lombok. 去除也不像de-lombok那么容易。 Lombok seems like the safer choice. 龙目岛似乎是更安全的选择。

UPDATE 12 (Feb 17 '16) 更新12(16年2月17日)
While trying to come up with justifications for why it's safe to bring in Lombok for the project I'm currently working on, I found a piece of gold that was added with v1.14 - The Configuration System ! 在尝试为我目前正在进行的项目引入Lombok的原因提出理由时,我发现了一块随v1.14添加的黄金 - 配置系统 ! This is means you can configure a project to dis-allow certain features that your team deems unsafe or undesirable. 这意味着您可以配置项目以禁止您的团队认为不安全或不合需要的某些功能。 Better yet, it can also create directory specific config with different settings. 更好的是,它还可以使用不同的设置创建特定于目录的配置。 This is AWESOME. 这太棒了。

UPDATE 13 (Oct 4 '16) 更新13(16年10月4日)
If this kind of thing matters to you, Oliver Gierke felt it was safe to add Lombok to Spring Data Rest . 如果这种事情对您很重要, Oliver Gierke认为将Lombok添加到Spring Data Rest是安全的。

UPDATE 14 (Sep 26 '17) 更新14(17年9月26日)
As pointed out by @gavenkoa in the comments on the OPs question, JDK9 compiler support isn't yet available (Issue #985). 正如@gavenkoa在关于OP问题的评论中指出的那样, JDK9编译器支持尚不可用 (问题#985)。 It also sounds like it's not going to be an easy fix for the Lombok team to get around. 听起来这对龙目团队来说并不是一个简单的解决办法。

UPDATE 15 (Mar 26 '18) 更新15(18年3月26日)
The Lombok changelog indicates as of v1.16.20 " Compiling lombok on JDK1.9 is now possible " even though #985 is still open. Lombok更改日志表示从v1.16.20开始“尽管#985仍然打开,但现在可以在JDK1.9上编译 lombok”。

Changes to accommodate JDK9, however, necessitated some breaking changes; 但是,为适应JDK9而进行的更改需要进行一些重大改变; all isolated to changes in config defaults. 所有与配置默认值的更改隔离。 It's a little concerning that they introduced breaking changes, but the version only bumped the "Incremental" version number (going from v1.16.18 to v1.16.20). 关于他们引入了重大变化,这有点令人担忧,但该版本仅提升了“增量”版本号(从v1.16.18升至v1.16.20)。 Since this post was about the safety, if you had a yarn/npm like build system that automatically upgraded to the latest incremental version, you might be in for a rude awakening. 由于这篇文章是关于安全性的,如果你有一个像自己升级到最新增量版本的yarn/npm类的构建系统,你可能会陷入一种粗鲁的觉醒。

UPDATE 16 (Jan 9 '19) 更新16(1919年1月9日)

It seems the JDK9 issues have been resolved and Lombok works with JDK10, and even JDK11 as far as I can tell. 似乎JDK9问题已经解决 ,Lombok与JDK10一起工作,甚至JDK11也是如此。

One thing I noticed though that was concerning from a safety aspect is the fact that the change log going from v1.18.2 to v1.18.4 lists two items as BREAKING CHANGE !? 有一件事我注意到,从安全方面来说这是因为从v1.18.2到v1.18.4的更改日志列出了两个项目为BREAKING CHANGE I'm not sure how a breaking change happens in a semver "patch" update. 我不确定在semver“补丁”更新中如何发生突破性变化。 Could be an issue if you use a tool that auto-updates patch versions. 如果您使用自动更新修补程序版本的工具,则可能会出现问题。


#4楼

I know I'm late, but I can't resist the temptation: anybody liking Lombok should also have a look at Scala. 我知道我已经迟到了,但我无法抗拒诱惑:任何喜欢龙目岛的人都应该看看Scala。 Many good ideas that you find in Lombok are part of the Scala language. 您在Lombok中找到的许多好主意都是Scala语言的一部分。

On your question: it's definitely easier to get your developers trying Lombok than Scala. 关于你的问题:让你的开发人员尝试Lombok比Scala更容易。 Give it a try and if they like it, try Scala. 尝试一下,如果他们喜欢它,请尝试Scala。

Just as a disclaimer: I like Java, too! 就像免责声明一样:我也喜欢Java!


#5楼

Wanted to use lombok's @ToString but soon faced random compile errors on project rebuild in Intellij IDEA. 想要使用@ToString@ToString但很快就会遇到Intellij IDEA中项目重建的随机编译错误。 Had to hit compile several times before incremental compilation could complete with success. 在增量编译成功完成之前,不得不多次编译。

Tried both lombok 1.12.2 and 0.9.3 with Intellij IDEA 12.1.6 and 13.0 without any lombok plugin under jdk 1.6.0_39 and 1.6.0_45. 在jdk 1.6.0_39和1.6.0_45下使用Intellij IDEA 12.1.6和13.0尝试了lombok 1.12.2和0.9.3,没有任何lombok插件。

Had to manually copy generated methods from delomboked source and put lombok on hold until better times. 不得不从delomboked源手动复制生成的方法,并将lombok保留到更好的时间。

Update 更新

The problem happens only with parallel compile enabled. 仅在启用并行编译时才会出现此问题。

Filed an issue: https://github.com/rzwitserloot/lombok/issues/648 提起问题: https : //github.com/rzwitserloot/lombok/issues/648

Update 更新

mplushnikov commented on 30 Jan 2016: mplushnikov于2016年1月30日评论:

Newer version of Intellij doesn't have such issues anymore. 较新版本的Intellij不再存在此类问题。 I think it can be closed here. 我想它可以在这里关闭。

Update 更新

I would highly recommend to switch from Java+Lombok to Kotlin if possible. 我强烈建议尽可能从Java + Lombok切换到Kotlin 。 As it has resolved from the ground up all Java issues that Lombok tries to work around. 因为它已经彻底解决了Lombok试图解决的所有Java问题。


#6楼

I'm not recommend it. 我不推荐它。 I used to use it, but then when I work with NetBeans 7.4 it was messing my codes. 我以前习惯使用它,但是当我使用NetBeans 7.4时,它会弄乱我的代码。 I've to remove lombok in all of files in my projects. 我要删除项目中所有文件中的lombok。 There is delombok, but how can I be sure it would not screw my codes. 有delombok,但我怎么能确定它不会搞砸我的代码。 I have to spends days just to remove lombok and back to ordinary Java styles. 我必须花几天时间才能删除lombok并回到普通的Java样式。 I just too spicy... 我太辣了......

使用Project Lombok是否安全? [关闭]相关推荐

  1. lombok 生成代码_使用Project Lombok减少Java应用程序中的样板代码

    lombok 生成代码 对Java编程语言最常提出的批评之一是它需要大量的样板代码 . 对于简单的类尤其如此,该类只需要存储一些值就可以. 您需要这些值的getter和setter方法,也许您还需要一 ...

  2. 使用Project Lombok减少Java应用程序中的样板代码

    对Java编程语言最常提出的批评之一是它需要大量的样板代码 . 对于简单的类尤其如此,该类只需要存储一些值就可以. 您需要这些值的getter和setter,也许您还需要一个构造函数,覆盖equals ...

  3. x-杂项-maven-repository-lombok-intro:使用PROJECT LOMBOK减少BOILERPLATE代码

    ylbtech-杂项-maven-repository-lombok-intro:使用PROJECT LOMBOK减少BOILERPLATE代码 1.返回顶部 1. REDUCING BOILERPL ...

  4. lombok 简化代码_如何编写更少,更好的代码,或Project Lombok

    lombok 简化代码 我长期以来一直打算写有关Lombok的文章 ,以至于每当一个自重的Java开发人员都已经听说过它时,我可能会写它. 尽管如此,值得一提的是,如果只是提醒自己,不要犹豫,尝试性能 ...

  5. 如何编写更少,更好的代码,或Project Lombok

    我长期以来一直打算撰写有关Lombok项目的文章 ,以至于每当一个自重的Java开发人员都已经听说过它时,我可能会写它. 尽管如此,值得一提的是,如果只是提醒自己,不要犹豫,尝试性能增强工具并查看它们 ...

  6. Lombok 使用详解,简化 Java 编程

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试资料 作者 | 日拱一兵 来源 | 公众号(日拱一兵) 在 Java 应 ...

  7. Java代码简化之lombok

    Java代码简化之lombok 本文目录: lombok简介 IDEA整合Lombok及使用 Lombok常用注解解释 1. lombok简介 Project Lombok makes java a ...

  8. lombok使用详细教程

    lombok使用总结 前提 这篇文章主要介绍lombok的使用,至于lombok的源码和原理暂不探究,可以看上一篇文章插件化注解处理API去了解lombok的基本原理.参考资料: lombok官网 l ...

  9. Lombok最全使用详解

    这里写自定义目录标题 作者 为什么使用Lombok Lombok官网的介绍 运行原理 Lombok的安装 1.添加依赖 2.添加插件(推荐) 常用注解 举例说明 [1]@Getter @Setter ...

最新文章

  1. 教你设计一个超牛逼的本地缓存!
  2. redis 主从切换
  3. openstack 手动安装版 功能测试
  4. 提升财运必看!运势不好的原因,竟然和这些数字有关?(已有近10万人提升了财运)...
  5. 电大计算机应用基础考试复习,电大 《计算机应用基础》形考作业2
  6. matlab线旋转成面,用matlab怎么绘制一条曲线绕z轴旋转生成的曲面?
  7. 3.中小型企业通用自动化运维架构 -- Ansible playbook
  8. Tracup丨什么是甘特图?关于甘特图的百科全书
  9. SAP 标准TCODE ME2L ME2N VL31等设置清单范围默认值(ME2L默认ALV显示)
  10. Android 动画分类
  11. 【无标题】C# 修改操作时禁用过滤器
  12. nginx的几个超时时间
  13. 7-4 工作分配问题分数 20作者 陈晓梅单位 广东外语外贸大学
  14. 再生龙移植ubuntu硬盘大小限制解决方案
  15. 英特尔CPU详细参数表(新老CPU详细对照表)
  16. “展厅三维全景”技术,将产品和企业文化以vr展示出来
  17. linux僵尸进程产生的原因以及如何避免产生僵尸进程
  18. Altium Designer 批量修改器件名
  19. 新装Win7 x64用Windows Update安装IE9一直失败的解决方法
  20. linux充值系统下载,几款Linux操作系统下载

热门文章

  1. Android 内存优化
  2. OkHttp 使用基础
  3. 算法----------找到 K 个最接近的元素
  4. 第十、十一周项目一-点-圆-圆柱类族的设计(2)
  5. Android性能优化之运算篇(二)
  6. swift_025(Swift 的自动引用计数(ARC)
  7. 解决Warning: Ignoring InnerClasses attribute for an anonymous inner class
  8. 在linux下给grep命令添加颜色
  9. Git 常用命令清单
  10. 关于css的float