Witam. Stworzyłem agenta, attachuje go do wybranej VM i staram się zmienić wartość stałej. Kod jest prawidłowy - kopiuję klase za to odpowiedzialną do innego projektu i uruchamiam w normalny sposób - działa.
public static void setField(Object obj, Field field, String value) throws NoSuchFieldException {
if (isParsable(field.getType())) {
field.setAccessible(true);
if ((field.getModifiers() & Modifier.FINAL) != 0) {
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
try {
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
try {
field.set(obj, parsers.get(field.getType()).apply(value));
} catch (IllegalAccessException e) {
System.err.println("Exception! Modifiers: " + Modifier.toString(field.getModifiers()) + " Accessible: " + field.isAccessible());
e.printStackTrace();
}
} else throw new Error("field needs to be primitive or String");
}
Out:
Exception! Modifiers: public static Accessible: true
java.lang.IllegalAccessException: Can not set static final java.lang.Integer ...
Czy ktoś się kiedyś spotkał z czymś takim?