プロパティファイルをオブジェクトにマップするためのソースコードが次のようになっています。オブジェクトをオブジェクトにマップするJavaのリフレクション
private Configuration MapPropertiesToObjectOrNull(Properties defaultProps){
Configuration config = new Configuration();
Enumeration e = defaultProps.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
try {
Field currField = config.getClass().getDeclaredField(key);
try {
currField.set(config, defaultProps.getProperty(key));
} catch (IllegalAccessException e1) {
e1.printStackTrace();
}
} catch (NoSuchFieldException e1) {
e1.printStackTrace();
}
}
return config;
}
私のソースコードにハードコードされた "Key" -Stringを避けるための良い "マッピング方法"ですか?多分あなたは他のアイディアを持っていますか?
をあなたは、動的キーを処理したかった、それを何ですか? – developer
ClassおよびPropertyfileのPropertynamesは同等でなければなりません。私はそれを動的にする可能性はないと思う? –