I fail to fill an object with reflection. can you please tell why?
public void setConfig(WyPreferences wyPreferences, String category, String key, String val) {
String normalizedCategory = stringUtils.normalizeFieldNameToCamelCase(category);
String normalizedKey = stringUtils.normalizeFieldNameToUnderScore(key);
try {
Field field1 = WyPreferences.class.getDeclaredField(normalizedCategory);
Object object1 = field1.get(wyPreferences);
if (object1 == null)
{
object1 = field1.getType().newInstance();
}
Field field2 = field1.getType().getDeclaredField(normalizedKey);
Object object2 = field2.get(object1);
if (object2 == null)
{
object2 = field2.getType().newInstance();
}
field2.set(object2, val);
field1.set(object1, object2);
} catch (Exception ex) {
String a = "";
}
category = providerSearch
key =config_URL
where val = "bla"
and with this class:
public class WyPreferences {
public ProviderSearch providerSearch;
public WyPreferences() {
this.providerSearch = new ProviderSearch();
}
}
public ProviderSearch() {
this.config_URL = "";
this.gas_popup_feature_enabled = "";
}
my code fails on field2.set(object2, val);
with this exception
java.lang.IllegalArgumentException: Can not set java.lang.String field com.w.state.ProviderSearch.config_URL to java.lang.String
Aucun commentaire:
Enregistrer un commentaire