开发者

How to use default values with Lombok and Eclipse save actions

I use Ecli开发者_运维技巧pse 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜