使用Lombok进行快捷的静态final常量声明

huangapple go评论65阅读模式
英文:

Shortcut static final constant declaration with use of lombok

问题

有没有办法使用lombok来简化Java常量的声明?例如,我想将以下声明:

public static final String CONST_VALUE = "constant-value";

简化为类似以下的形式:

@Constant String CONST_VALUE = "constant-value";

这种方式是否可行?以前我可以使用 @FieldDefaults(level = PUBLIC, makeFinal = true) 来简化至少 public final,但现在已经不再支持这个选项。另外,我不想为此目的使用 interface,因为在数据类中经常只需要1-2个这样的字段,而接口的实现看起来更加冗余。此外,为所有常量使用一个接口似乎也不是很好。

英文:

Is there any way to shortcut java constants declaration with the help of lombok. For example I want to shortcut declaration of

public static final String CONST_VALUE = "constant-value";

to something like

@Constant String CONST_VALUE = "constant-value";

Is this possible somehow? Earlier I could shortcut at least public final with use of @FieldDefaults(level = PUBLIC, makeFinal = true), but it's not an option anymore. Also I don't want to use interface for this purpose, because very often I need 1-2 such fields inside data class, where implementation of interface looks much more redundant. Also one interface for all constant looks like landfill.

答案1

得分: 1

你可以尝试使用@FieldNameConstants,但这是一个实验性功能。文档链接

英文:

You can try @FieldNameConstants, but it's experimental feature. documentation

huangapple
  • 本文由 发表于 2020年9月7日 16:20:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63773839.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定