I have this method that returns a list of Fields objects that are of String type.
public static List<Field> getStringFields(Class<?> clazz) {
List<Field> toReturn = new ArrayList<Field>();
Field[] allFields = clazz.getDeclaredFields();
for (Field f : allFields) {
Class<?> type = f.getType();
if (type == String.class) {
stringFields.add(f);
}
}
return stringFields;
}
And this method, that should captalize all the string fields:
public void capitalizeStringFields() {
List<Field> stringFieldsToCapitalize = Utils.getStringFields(someClass.class);
try {
for (Field field : stringFieldsToCapitalize) {
field.set(this, field.get(this).toString().toUpperCase());
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The problem is: the "field.set(...)" is not working. It supposed to capitalize the value and set it as the new value for that field, but it's not working... Any idea of how can I solve this? (PS: In the real code the "someClass.class" is set as the real class name...)
The error being generated is IllegalArgumentException in the field.get(this) method (insde the field.set method).
Aucun commentaire:
Enregistrer un commentaire