英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论