如何在 Lombok 和 Eclipse 保存操作中使用默认值

发布于 2024-12-02 06:54:34 字数 455 浏览 3 评论 0原文

我使用 Eclipse Save 操作,通常发现它们非常有用。然而,我在使用 Lombok 时遇到了一个令人恼火的情况。

在下面的课程中,我想为字段设置默认值以防止出现空值,同时仍然允许设置器。

@Data
public class Foo {
    @NonNull
    private String value = "myDefaultValue";
}

这太棒了,直到我点击保存,Eclipse 使该字段成为最终的!我通常不想禁用保存操作,因为我喜欢他们大多数时间所做的事情。只是不是在这种情况下!

这个问题表明我在不运行保存操作的情况下无法保存,这很痛苦在编辑文件时继续启用/禁用保存操作。

I use Eclipse Save actions, and generally find them very useful. However, I've hit a scenario when using Lombok that is enfuriating.

In the following class, I want to set a default value for a field to prevent nulls, whilst still allowing a setter.

@Data
public class Foo {
    @NonNull
    private String value = "myDefaultValue";
}

This is great, until I hit save, when Eclipse makes the field final! I generally don't want' to disable save actions, as I like what they are doing most of the time. Just not in this instance!

This question suggests that I can't save without running save actions, and it's a pain to keep enabling/disabling save actions whilst editing the file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霓裳挽歌倾城醉 2024-12-09 06:54:34

这是一个错误,已在问题 263 中修复。最初,此修复是在 Lombok 0.10.4 中提供的,但作为副作用,引入了一个新问题,该问题已在 0.10.8 版本中得到解决。

披露:我是 Lombok 项目的开发人员之一。

This was a bug, and has been fixed in Issue 263. Originally, this fix was delivered in Lombok 0.10.4, but as a side effect a new problem was introduced that has been addressed in the 0.10.8 release.

Disclosure: I am one of the Project Lombok developers.

街道布景 2024-12-09 06:54:34

恐怕这不是龙目岛的答案。但根据我的经验,final 弊大于利。它不会阻止您更改引用指向的对象(当然,不可变的 String 除外)。优化的帮助微乎其微。我在 Java 中避免使用 final,但在 C++ 中则避免使用 const

您可以编辑“保存”操作并排除添加 final 关键字,同时仍执行其他操作。

This is not a Lombok answer, I’m afraid. But in my experience final does more harm than good. It does not stop you from changing the object the reference points to (except, of course, for the immutable String). Optimization only helps little. I avoid final in Java, but like const in C++.

You can edit your Save actions and exlude adding the final keywords while still performing the other actions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文