考虑 Java 代码:
ReadProperty.get("info")
还有一个 my_stettings.properties
文件:
info=Lorem ipsum
server=computer01
我想知道是否可以使用代码分析工具(Checkstyle、FindBugs、PMD...)来检查我的 get()
的 String
参数是否有效> 方法可在 my_stettings.properties
文件中找到。
ReadProperty.get("servers") //should produce a warning
ReadProperty.get("server") //is OK
您对我如何实现这一目标有什么意见吗?
最佳答案
我不知道有任何现有工具可以执行此操作。但是,Android 平台上使用的一些模式可以用于解决您的问题,在本例中是 R 资源类模式,它从属性文件生成类。所以文件my_stettings.properties
:
info=Lorem ipsum
server=computer01
应该导致一个类似的类
public final class R {
public static final int info=1;
public static final int server=1;
}
在这种情况下,您将使用它
ReadProperty.get(R.info);
如果该属性不可用,则不会编译。
- 这个答案在这里https://stackoverflow.com/a/3004518/734687生成一个几乎相同的枚举。
- 这个答案在这里https://stackoverflow.com/a/1578106/734687使用接口(interface)来访问值。
- 这个问题在这里Tool for checking Java resource bundles (i18n)还要求提供工具并提供指向 http://rscbundlecheck.sourceforge.net/ 的链接。德国博客文章描述了这里的用法:http://www.catalysts.cc/wissenswertes/resourcebundle-checking/
- Maven 资源包检查插件会对您的资源文件运行一些健全性检查,请参阅 http://labs.consol.de/projects/maven/maven-rbc-plugin/
- 编译器辅助本地化提供编译时检查,请参阅 http://cal10n.qos.ch/
关于code-analysis - 代码分析: check if a String parameter of a given function is available in a *.属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9408852/