I was able to loop through the fields of a class and grab private fields values using reflection. However I'm not able to set field values and the only solutions I can find is using newInstance() which seems to affect the value of a new instance of the class.
How can I set the value of a field in a running object Class?
Here is my compile and run error free code that still doesn't set the value of current running class I want to target.
Field[] allFields = c.getDeclaredFields();
for (Field field : allFields) {
field.setAccessible(true);
String fieldName = field.getName();
if (fieldName == "theNameOfFieldIneed"){
try {
Object objectInstance = a.newInstance();
field.setInt( objectInstance, 1000);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
Aucun commentaire:
Enregistrer un commentaire