So I discovered how to Change private static final field using Java reflection a while back and have been using it to modify final fields with reflection ever since, however I then got to wondering why I couldn't apply the same logic to the type for the field. I came up with
public static void changeFieldType(Field field, Class newType){
try {
Field typeField = Field.class.getDeclaredField("type");
typeField.setAccessible(true);
typeField.set(field, newType);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException ex) {
LogUtil.printErr("Failed to edit field type for " + field.getName());
}
}
However this led me to wondering how this would effect an application if you, say, set the type for a field to Object
when it was originally an ArrayList
or some other higher level class. Would this create internal errors within the JVM or is this value simply a clone of the JVM's data and changing it would have no effect. Any light you can shed on the subject is greatly appreciated.
Aucun commentaire:
Enregistrer un commentaire