gpt4 book ai didi

java - 属性文件中的枚举常量显示名称

转载 作者:太空宇宙 更新时间:2023-11-04 11:30:08 25 4
gpt4 key购买 nike

我的 EMF 模型中有一个枚举数据类型。

这就是我想要的:

  1. 我希望枚举常量有一个漂亮的显示名称。
  2. 显示名称应在 plugin.properties 文件中设置。
  3. 该机制必须与 EMF 系统很好地集成,以从属性中获取值,以便我可以以统一的方式处理所有值(无论是否为枚举)。这可能意味着该解决方案必须以某种方式使用 IItemPropertyDescriptor

我可以看到枚举常量在我的 EMF Edit 项目的 plugin.properties 中生成了条目。所以应该有某种方法来获取这些名称。但我不知道怎么办。

我可以在 Xcore 模型文件中设置显示名称,但这不是我想要的。我希望从我的 plugin.properties 文件中读取它们。

从属性文件中手动获取枚举显示名称很简单。但应该有某种方法让 EMF 处理这个问题。每次从模型中获取值时,我都必须编写特殊的代码来处理枚举值,这似乎很奇怪。

最佳答案

这是一个例子:

public enum Constants {
PROP1,
PROP2;

private static final String PATH = "/constants.properties";

private static final Logger logger = LoggerFactory.getLogger(Constants.class);

private static Properties properties;

private String value;

private void init() {
if (properties == null) {
properties = new Properties();
try {
properties.load(Constants.class.getResourceAsStream(PATH));
}
catch (Exception e) {
logger.error("Unable to load " + PATH + " file from classpath.", e);
System.exit(1);
}
}
value = (String) properties.get(this.toString());
}

public String getValue() {
if (value == null) {
init();
}
return value;
}

}

这可以是 constans.properties 文件:

#This is property file...
PROP1=some text
PROP2=some other text

关于java - 属性文件中的枚举常量显示名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43895262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com