// java.lang.reflect.Field
public class FinalFieldModify {
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
System.out.println("Before modify: contains c = " + DataHolder.contains("c"));
Field field = DataHolder.class.getDeclaredField("data");
field.setAccessible(true);
List<String> origin = (List<String>) field.get(null);
final Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
// 由于 List.of 返回的是不可变 List,
// 所以 只能新建一个 List,增加元素,再替换原来的 List
ArrayList<String> cur = new ArrayList<>(origin.size() + 1);
cur.addAll(origin);
cur.add("c");
field.set(null, cur);
System.out.println("After modify: contains c = " + DataHolder.contains("c"));
}
private static class DataHolder {
private static final List<String> data = List.of("a", "b");
private static boolean contains(String s) {
return data.contains(s);
}
}
}
... 略
field.setAccessible(true);
final Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
List<String> origin = (List<String>) field.get(null);
// 由于 List.of 返回的是不可变 List,
// 所以 只能新建一个 List,增加元素,再替换原来的 List
ArrayList<String> cur = new ArrayList<>(origin.size() + 1);
Before modify: contains c = false
After modify: contains c = true
public final class Field extends AccessibleObject implements Member
{
// Cached field accessor created with override
private FieldAccessor overrideFieldAccessor;
public Object get(Object obj) {
return getFieldAccessor(obj).get(obj);
}
public void set(Object obj, Object value) {
getFieldAccessor(obj).set(obj, value);
}
private FieldAccessor getFieldAccessor(Object obj) {
FieldAccessor a = overrideFieldAccessor;
return (a != null) ? a : acquireFieldAccessor(true);
}
private FieldAccessor acquireFieldAccessor(boolean overrideFinalCheck) {
FieldAccessor tmp = reflectionFactory.newFieldAccessor(this, overrideFinalCheck);
setFieldAccessor(tmp, overrideFinalCheck);
return tmp;
}
private void setFieldAccessor(FieldAccessor accessor, boolean overrideFinalCheck) {
this.overrideFieldAccessor = accessor;
}
}