Java IntelliJ IDEA 看不到 Lombok 生成的代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29980133/
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-08-11 08:55:31  来源:igfitidea点击:

IntelliJ IDEA cannot see Lombok generated code

javaintellij-ideajunit4lombokintellij-lombok-plugin

提问by copolii

I have a Gradle-based project that uses lombok. I have imported this project into IntelliJ IDEA 14.1 (using the Import External Modelimport method). I can run the JUnit4 unit tests without problem in Gradle, but IntelliJ seems to have a problem seeing the Lombok generated Getters. This is preventing me from running the tests in IDEA.

我有一个使用 lombok 的基于 Gradle 的项目。我已将此项目导入 IntelliJ IDEA 14.1(使用导入外部模型导入方法)。我可以在 Gradle 中毫无问题地运行 JUnit4 单元测试,但 IntelliJ 似乎在看到 Lombok 生成的 Getter 时遇到问题。这阻止了我在 IDEA 中运行测试。

To make sure it wasn't a set up issue, I created a Very simple projectand confirmed that the same issue occurs in the simple test project.

为了确保这不是设置问题,我创建了一个非常简单的项目,并确认在简单的测试项目中也出现了同样的问题。

My versions: Gradle: gradle-2.3-all via Gradle wrapper
Intellij IDEA: IU-141.713
Lombok Plugin: 0.9.2

我的版本:Gradle:gradle-2.3-all 通过 Gradle 包装器
Intellij IDEA:IU-141.713
Lombok 插件:0.9.2

What am I missing here?

我在这里缺少什么?

UpdateBy cannot seeI don't mean physically invisible. I mean this:

更新通过看不到我不是指物理上不可见。我的意思是:

/home/meeee/workspace/junit-lombok-idea/lib/src/main/java/com/example/jumbokea/Main.java
Error:(10, 11) java: cannot find symbol
  symbol:   method setIntField(int)
  location: variable sc of type com.example.jumbokea.SomeClass
Error:(12, 41) java: cannot find symbol
  symbol:   method getIntField()
  location: variable sc of type com.example.jumbokea.SomeClass
Error:(14, 33) java: constructor AnotherClass in class com.example.jumbokea.AnotherClass cannot be applied to given types;
  required: no arguments
  found: float,com.example.jumbokea.SomeClass
  reason: actual and formal argument lists differ in length
Error:(16, 46) java: cannot find symbol
  symbol:   method getSomeFloat()
  location: variable ac of type com.example.jumbokea.AnotherClass
Error:(17, 43) java: cannot find symbol
  symbol:   method getSomeClass()
  location: variable ac of type com.example.jumbokea.AnotherClass

The decompiled classes show the getters and setters, but I'm getting these errors when I get IntelliJ to run the 'app' or the Unit tests.

反编译的类显示了 getter 和 setter,但是当我让 IntelliJ 运行“应用程序”或单元测试时,我收到了这些错误。

Getters and setters are there, but not there???

吸气剂和二传手在那里,但不在那里???

采纳答案by mh-dev

You need to enable the annotation preprocessor. See documentation here.

您需要启用注释预处理器。 请参阅此处的文档

Additionally you should install the lombok plugin if not done already

此外,如果尚未安装 lombok 插件,您应该安装

回答by JiHong Jang

It can be seen in the build folder.

它可以在构建文件夹中看到。

enter image description here

在此处输入图片说明

回答by Samantha Catania

I managed to solve this issue by doing the following:

我通过执行以下操作设法解决了这个问题:

  • Install the IDEA Lombok plugin
  • Enable Annotation Processing see documentation
  • Install the Latest Maven on your system and configure IntelliJ to use it (as opposed to the bundled one) see documentation
  • 安装 IDEA Lombok 插件
  • 启用注释处理请参阅文档
  • 在您的系统上安装最新的 Maven 并配置 IntelliJ 以使用它(而不是捆绑的),请参阅文档

回答by wst

In case of Lombok, IntelliJ allows you to search for usages of variable, which will also include getters and setters. Just click variable name and option/alt + f7.

在 Lombok 的情况下,IntelliJ 允许您搜索变量的用法,其中还包括 getter 和 setter。只需单击变量名称和option/alt + f7

This works in Intellij 2016 and Lombok plugin 0.13.16. I am not sure about older versions.

这适用于 Intellij 2016 和 Lombok 插件 0.13.16。我不确定旧版本。

回答by logrusso

In my case the issue occurred after updating IntelliJ via Toolbox.

在我的情况下,问题发生在通过 Toolbox 更新 IntelliJ 之后。

"Enable annotation processing" flag was switched off (despite importing the settings from previous IntelliJ version), so I switched it on. This did not resolve the issue after restart, clean, build.

“启用注释处理”标志已关闭(尽管从以前的 IntelliJ 版本导入设置),因此我将其打开。这在重新启动、清理、构建后没有解决问题。

However, resintalling the lombok plugin helped!

但是,重新安装 lombok 插件有帮助!

EDIT: Seems that it was the problem with compatibility of lombok plugin with IntelliJ 2018.1, plugin upgrade to 0.17 is required for IntelliJ users.

编辑:似乎是 lombok 插件与 IntelliJ 2018.1 的兼容性问题,IntelliJ 用户需要将插件升级到 0.17。

Lombok plugin changelog excerpt:

Lombok 插件更新日志摘录:

Lombok support plugin updated to v0.17: - Fixed (#465): Fix plugin compatibility with IntelliJ 2018.1

Lombok 支持插件更新到 v0.17: - 固定(#465):修复插件与 IntelliJ 2018.1 的兼容性

回答by Lindsey Zhang

After enabled the annotation processor and installed lombok plugin, if still got "can't find symbol error" on lombok generated method, try check the gradle configuration, which is in

启用注释处理器并安装 lombok 插件后,如果在 lombok 生成的方法上仍然出现“找不到符号错误”,请尝试检查 gradle 配置,它在

Preference -> Build,Execution, Deployment -> Gradle,

首选项 -> 构建、执行、部署 -> Gradle,

if you are using local gradle distribution, change to use default gradle wrapper.

如果您使用的是本地 gradle 发行版,请更改为使用默认的 gradle wrapper

I got this error when my project's gradle wrapper is 4.10.2 but my local gradle version is 5.1.1, and I solve it after I change it to use default gradle wrapper.

当我的项目的 gradle 包装器是 4.10.2 但我的本地 gradle 版本是 5.1.1 时,我收到了这个错误,我将它更改为使用默认的 gradle 包装器后解决了这个错误。

回答by Chaklader Asfak Arefe

As mentioned in the other answer, please, install the Lombokplugin and enable the annotation processor. Press double SHIFTin the IntelliJ and type Enable annotation processingin the search box. Afterward, tick the box and restart the IDE.

如另一个答案中所述,请安装Lombok插件并启用注释处理器。SHIFT在 IntelliJ 中按 double并Enable annotation processing在搜索框中键入。然后,勾选该框并重新启动 IDE。

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

回答by TrueIndian 31

Add below dependency in pom.xml along with other solutions mentioned in other answers.

在 pom.xml 中添加以下依赖项以及其他答案中提到的其他解决方案。

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.2</version>
    <scope>provided</scope>
</dependency>

回答by Jan Andrzejewski

Try click on lombok import, and put alt+enter, then short list will appear, choose add maven dependency, then on the right down corner on the screen will appear icon that maven should be enabledd, click on enable automatically. For me it worked.

尝试点击lombok import,然后按alt+enter,会出现短列表,选择添加maven依赖,然后屏幕右下角会出现maven应该启用的图标,点击自动启用。对我来说它奏效了。

回答by Rishikesh Fanse

Even after adding annotation processor and having lombok dependency added might not solve the problem . This is because the class files related to model needs to be in target folder

即使添加了注释处理器并添加了 lombok 依赖项,也可能无法解决问题。这是因为与模型相关的类文件需要在目标文件夹中

Run clean installto generate the class files for model with lombok code generated changes.

运行全新安装以生成带有 lombok 代码生成更改的模型的类文件。