java 龙目岛项目有哪些风险?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4589184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:05:50  来源:igfitidea点击:

What are the risks with Project Lombok?

javajakarta-eeboilerplatelombok

提问by jldugger

I'm coming up with performance goals for the new year, and I thought I'd be fun to put a goal to reduce the size of the code base, especially boilerplate. One action I've come up with to address this is to use Project Lombokto make beans as short as they should be. But I have a habit of overlooking downsides to new software and approaches, so I'm relying on the Stack Overflow community: Can anyone tell me why Lombok is a bad idea?

我正在制定新的一年的性能目标,我认为我设定一个减少代码库大小的目标会很有趣,尤其是样板文件。我为解决这个问题而提出的一项措施是使用Project Lombok使 bean 尽可能短。但是我习惯于忽视新软件和方法的缺点,所以我依赖于 Stack Overflow 社区:谁能告诉我为什么 Lombok 是个坏主意?

采纳答案by Zeki

A major downside is IDE support. Since Lombok is not actually a language change, and since your IDE only understands java, you will need an IDE that supports Lombok to get things working right. As of now, that's only Eclipsethat includes Eclipse and IntelliJ. If you use eclipse that might be ok, but remember that you are making a decision for future developers as well.

一个主要的缺点是 IDE 支持。由于 Lombok 实际上不是语言更改,而且您的 IDE 只理解 java,因此您需要一个支持 Lombok 的 IDE 才能使事情正常进行。到目前为止,只有 Eclipse包含 Eclipse 和 IntelliJ。如果您使用 eclipse 可能没问题,但请记住,您也在为未来的开发人员做出决定。

I'd suggest you consider moving some of your code into a less ceremonial language such as groovy. We've had success moving some of our business logic and models into groovy and it works really smoothly.

我建议你考虑将你的一些代码移动到一种不太正式的语言中,比如 groovy。我们已经成功地将我们的一些业务逻辑和模型转移到 groovy 中,并且它的工作非常顺利。

回答by Jcs

A limitation of Lombok is the fact that it is closely tied to the java compiler. Since the annotation processor API only allows creation of new files during the compilation (and not the modification of the existing files) lombok uses that API as a entry point to modify the java compiler. Unfortunately these modifications of the compiler make heavy usage of non-public APIs. Using lombok may be a good idea but you must be aware that upgrading your compiler may break your code. The probability is low but I always feel uncomfortable using non-public APIs.

Lombok 的一个限制是它与 java 编译器密切相关。由于注解处理器 API 只允许在编译期间创建新文件(而不是修改现有文件),lombok 使用该 API 作为入口点来修改 java 编译器。不幸的是,编译器的这些修改导致大量使用非公共 API。使用 lombok 可能是个好主意,但您必须意识到升级编译器可能会破坏您的代码。概率很低,但我总是觉得使用非公共 API 不舒服。

回答by Krzysztof Tomaszewski

In my opinion source code in "Java+Lombok" is not Java source code anymore. I see it as something similar Borland company made many years ago in their Borland C++ Builder IDE for VCL - they introduced "properties" in C++ code effectively introducing some kind of a new programming language that wasn't C++ anymore (not C++ in sense of standard of C++ language). Sources using "Java+Lombok" are not valid sources in sense of Java language specification. Moreover I think annotations were not designed to influence language semantic.

在我看来,“Java+Lombok”中的源代码不再是 Java 源代码。我认为它是类似于 Borland 公司多年前在他们的 Borland C++ Builder IDE for VCL 中制作的东西——他们在 C++ 代码中引入了“属性”,有效地引入了某种不再是 C++ 的新编程语言(不是 C++ 意义上的C++ 语言标准)。使用“Java+Lombok”的源在 Java 语言规范意义上不是有效的源。此外,我认为注释不是为了影响语言语义而设计的。

回答by Will Hartung

One potential disadvantage to something like Lombok is that with the setters/getters "missing", source tools may not "recognize" aspects of the resulting object that give it "bean" qualities, since those qualities only manifest in the compiled class.

像 Lombok 这样的东西的一个潜在缺点是,由于 setter/getter“缺失”,源工具可能无法“识别”结果对象的哪些方面赋予它“bean”特性,因为这些特性只在编译类中表现出来。

Another disadvantage is that it's Yet Another piece of "black magic" within the tool chain. Fortunately, it seems to be a rather benign piece (I have not used it), and the fact that it happens at compile time rather than runtime is actually a blessing (IMHO). But, you're not going to be able to reuse or share your code without the project since it's adding artifacts to your code base. So, while the compiled class file may be a "POJO", I'd argue that your source code is NOT a POJO.

另一个缺点是它是工具链中的又一“黑魔法”。幸运的是,它似乎是一个相当良性的部分(我没有使用过它),而且它发生在编译时而不是运行时的事实实际上是一种祝福(恕我直言)。但是,如果没有项目,您将无法重用或共享您的代码,因为它会向您的代码库添加工件。因此,虽然编译后的类文件可能是“POJO”,但我认为您的源代码不是 POJO。

Neither of these are crippling downsides, rather just aspects to be aware of looking forward.

这些都不是严重的缺点,而只是需要注意的方面。

回答by dehasi

It's a third party library, and there are developers who don't know it well.

是第三方库,有开发者不太了解。

IDE should support annotations processing (there are plugins for IDEA and Eclipse).

IDE 应该支持注解处理(IDEA 和 Eclipse 有插件)。

As was mentioned above, your code will be without getters/setters. It leads to sonar/checkstyle violations.

如上所述,您的代码将没有 getter/setter。它会导致声纳/检查样式违规。

回答by Anil Bharadia

As pointed out by user @Jcsin another answer, i would like to add more.

正如用户@Jcs在另一个答案中指出的那样,我想添加更多内容。

In our project we, are using mapstruct which is used to generate mapper classes, before the code is compiled, using mvn generate-sources command, this is done at process phase using maven processor plugin.

在我们的项目中,我们使用 mapstruct 来生成映射器类,在编译代码之前,使用 mvn generate-sources 命令,这是在进程阶段使用 maven 处理器插件完成的。

project lombok adds the bytecode for the getter/setter in class file at compile phase.

项目 lombok 在编译阶段为类文件中的 getter/setter 添加字节码。

since process phase is executed before the compile, it finds that there is no getter/setter available in class.

因为process阶段是在compile之前执行的,所以发现class中没有getter/setter。

There are some workarounds available to execute compile phase more than one. See this git hub ticketfor more details.

有一些解决方法可用于多次执行编译阶段。有关更多详细信息,请参阅此git hub 票证

Note : I am using STS ide by Spring and it is supported by lombok :)

注意:我使用的是 Spring 的 STS ide,lombok 支持它 :)

回答by Ashish Khandelwal

In my opinion, The most obvious risk with Project Lombok is that when you decide to use lombok, you also decide that everyone else who deals with your code uses lombok. This is a true statement for all libraries, but Lombok is special in that it is a build-time dependency and your IDE needs plugins to figure out what is going on. That means anyone who has reason to touch your code ex. someone trying to debug weird behavior, etc.) needs to know how to set it up and how it works. That can be a little frustrating.

在我看来,Project Lombok 最明显的风险是,当您决定使用 lombok 时,您也决定其他处理您代码的人都使用 lombok。这是所有库的真实陈述,但 Lombok 的特殊之处在于它是构建时依赖项,您的 IDE 需要插件来弄清楚发生了什么。这意味着任何有理由接触您的代码前的人。有人试图调试奇怪的行为等)需要知道如何设置它以及它是如何工作的。这可能有点令人沮丧。