I have a class with a private static final field that, unfortunately, I need to change it at run-time.
Using reflection I get this error: java.lang.IllegalAccessException: Can not set static final boolean field
Is there any way to change the value?
Field hack = WarpTransform2D.class.getDeclaredField("USE_HACK");
hack.setAccessible(true);
hack.set(null, true);
18 Answers 18
Assuming no SecurityManager is preventing you from doing this, you can use setAccessible to get around private and resetting the modifier to get rid of final, and actually modify a private static final field.
Here's an example:
import java.lang.reflect.*;
public class EverythingIsTrue {
static void setFinalStatic(Field field, Object newValue) throws Exception {
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, newValue);
}
public static void main(String args[]) throws Exception {
setFinalStatic(Boolean.class.getField("FALSE"), true);
System.out.format("Everything is %s", false); // "Everything is true"
}
}
Assuming no SecurityException is thrown, the above code prints "Everything is true".
What's actually done here is as follows:
- The primitive
booleanvaluestrueandfalseinmainare autoboxed to reference typeBoolean"constants"Boolean.TRUEandBoolean.FALSE - Reflection is used to change the
public static final Boolean.FALSEto refer to theBooleanreferred to byBoolean.TRUE - As a result, subsequently whenever a
falseis autoboxed toBoolean.FALSE, it refers to the sameBooleanas the one refered to byBoolean.TRUE - Everything that was
"false"now is"true"
Related questions
- Using reflection to change
static final File.separatorCharfor unit testing - How to limit setAccessible to only "legitimate" uses?
- Has examples of messing with
Integer's cache, mutating aString, etc
- Has examples of messing with
Caveats
Extreme care should be taken whenever you do something like this. It may not work because a SecurityManager may be present, but even if it doesn't, depending on usage pattern, it may or may not work.
JLS 17.5.3 Subsequent Modification of Final Fields
In some cases, such as deserialization, the system will need to change the
finalfields of an object after construction.finalfields can be changed via reflection and other implementation dependent means. The only pattern in which this has reasonable semantics is one in which an object is constructed and then thefinalfields of the object are updated. The object should not be made visible to other threads, nor should thefinalfields be read, until all updates to thefinalfields of the object are complete. Freezes of afinalfield occur both at the end of the constructor in which thefinalfield is set, and immediately after each modification of afinalfield via reflection or other special mechanism.Even then, there are a number of complications. If a
finalfield is initialized to a compile-time constant in the field declaration, changes to thefinalfield may not be observed, since uses of thatfinalfield are replaced at compile time with the compile-time constant.Another problem is that the specification allows aggressive optimization of
finalfields. Within a thread, it is permissible to reorder reads of afinalfield with those modifications of a final field that do not take place in the constructor.
See also
- JLS 15.28 Constant Expression
- It's unlikely that this technique works with a primitive
private static final boolean, because it's inlineable as a compile-time constant and thus the "new" value may not be observable
- It's unlikely that this technique works with a primitive
Appendix: On the bitwise manipulation
Essentially,
field.getModifiers() & ~Modifier.FINAL
turns off the bit corresponding to Modifier.FINAL from field.getModifiers(). & is the bitwise-and, and ~ is the bitwise-complement.
See also
Remember Constant Expressions
Still not being able to solve this?, have fallen onto depression like I did for it? Does your code looks like this?
public class A {
private final String myVar = "Some Value";
}
Reading the comments on this answer, specially the one by @Pshemo, it reminded me that Constant Expressions are handled different so it will be impossible to modify it. Hence you will need to change your code to look like this:
public class A {
private final String myVar;
private A() {
myVar = "Some Value";
}
}
if you are not the owner of the class... I feel you!
For more details about why this behavior read this?
34 Comments
getDeclaredField() instead of getField() for target classfinal String myConstant = "x"; and will fail: remember that compile time constants will be inlined by compiler so when you will write code like System.out.println(myConstant); it will be compiled as System.out.println("x"); because compiler knows value of constant at compile time. To get rid of this problem you need to initialize your constants at runtime like final String myConstant = new String("x");. Also in case of primitives like final int myField = 11 use final int myField = new Integer(11); or final Integer myField = 11;If the value assigned to a static final boolean field is known at compile-time, it is a constant. Fields of primitive or
String type can be compile-time constants. A constant will be inlined in any code that references the field. Since the field is not actually read at runtime, changing it then will have no effect.
The Java language specification says this:
If a field is a constant variable (§4.12.4), then deleting the keyword final or changing its value will not break compatibility with pre-existing binaries by causing them not to run, but they will not see any new value for the usage of the field unless they are recompiled. This is true even if the usage itself is not a compile-time constant expression (§15.28)
Here's an example:
class Flag {
static final boolean FLAG = true;
}
class Checker {
public static void main(String... argv) {
System.out.println(Flag.FLAG);
}
}
If you decompile Checker, you'll see that instead of referencing Flag.FLAG, the code simply pushes a value of 1 (true) onto the stack (instruction #3).
0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
3: iconst_1
4: invokevirtual #3; //Method java/io/PrintStream.println:(Z)V
7: return
5 Comments
public static final Boolean FALSE = new Boolean(false) not public static final boolean FALSE = falseA little curiosity from the Java Language Specification, chapter 17, section 17.5.4 "Write-protected Fields":
Normally, a field that is final and static may not be modified. However, System.in, System.out, and System.err are static final fields that, for legacy reasons, must be allowed to be changed by the methods System.setIn, System.setOut, and System.setErr. We refer to these fields as being write-protected to distinguish them from ordinary final fields.
Source: http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.5.4
Comments
I also integrated it with joor library
Just use
Reflect.on(yourObject).set("finalFieldName", finalFieldValue);
Also I fixed an issue with override which the previous solutions seem to miss.
However use this very carefully, only when there's no other good solution.
Along with top ranked answer you may use a bit simpliest approach. Apache commons FieldUtils class already has particular method that can do the stuff. Please, take a look at FieldUtils.removeFinalModifier method. You should specify target field instance and accessibility forcing flag (if you play with non-public fields). More info you can find here.
3 Comments
java.lang.UnsupportedOperationException: In java 12+ final cannot be removed.Since Java 12 onwards, the answers given will not work.
Here is an example on how to modify a private static final field since Java 12 (based on this answer).
private Object modifyField(Object newFieldValue, String fieldName, Object classInstance) throws NoSuchFieldException, IllegalAccessException {
Field field = classInstance.getClass().getDeclaredField(fieldName);
VarHandle MODIFIERS;
field.setAccessible(true);
var lookup = MethodHandles.privateLookupIn(Field.class, MethodHandles.lookup());
MODIFIERS = lookup.findVarHandle(Field.class, "modifiers", int.class);
int mods = field.getModifiers();
if (Modifier.isFinal(mods)) {
MODIFIERS.set(field, mods & ~Modifier.FINAL);
}
Object previousValue = field.get(classInstance);
field.set(null, newFieldValue);
return previousValue;
}
See this thread for more details.
9 Comments
--illegal-access=permit--illegal-access=permit was removed in Java 17.static final fields. That never really worked. It may appear to work, but in the end - relying on that effect makes your code just non-portable.In case of presence of a Security Manager, one can make use of AccessController.doPrivileged
Taking the same example from accepted answer above:
import java.lang.reflect.*;
public class EverythingIsTrue {
static void setFinalStatic(Field field, Object newValue) throws Exception {
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
// wrapping setAccessible
AccessController.doPrivileged(new PrivilegedAction() {
@Override
public Object run() {
modifiersField.setAccessible(true);
return null;
}
});
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, newValue);
}
public static void main(String args[]) throws Exception {
setFinalStatic(Boolean.class.getField("FALSE"), true);
System.out.format("Everything is %s", false); // "Everything is true"
}
}
In lambda expression, AccessController.doPrivileged, can be simplified to:
AccessController.doPrivileged((PrivilegedAction) () -> {
modifiersField.setAccessible(true);
return null;
});
1 Comment
Even in spite of being final a field can be modified outside of static initializer and (at least JVM HotSpot) will execute the bytecode perfectly fine.
The problem is that Java compiler does not allow this, but this can be easily bypassed using objectweb.asm. Here is p̶e̶r̶f̶e̶c̶t̶l̶y̶ ̶v̶a̶l̶i̶d̶ ̶c̶l̶a̶s̶s̶f̶i̶l̶e̶ an invalid classfile from the JVMS specification standpoint, but it passes bytecode verification and then is successfully loaded and initialized under JVM HotSpot OpenJDK12:
ClassWriter cw = new ClassWriter(0);
cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, "Cl", null, "java/lang/Object", null);
{
FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL, "fld", "I", null, null);
fv.visitEnd();
}
{
// public void setFinalField1() { //... }
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "setFinalField1", "()V", null, null);
mv.visitMaxs(2, 1);
mv.visitInsn(Opcodes.ICONST_5);
mv.visitFieldInsn(Opcodes.PUTSTATIC, "Cl", "fld", "I");
mv.visitInsn(Opcodes.RETURN);
mv.visitEnd();
}
{
// public void setFinalField2() { //... }
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "setFinalField2", "()V", null, null);
mv.visitMaxs(2, 1);
mv.visitInsn(Opcodes.ICONST_2);
mv.visitFieldInsn(Opcodes.PUTSTATIC, "Cl", "fld", "I");
mv.visitInsn(Opcodes.RETURN);
mv.visitEnd();
}
cw.visitEnd();
In Java, the class looks roughly speaking as follows:
public class Cl{
private static final int fld;
public static void setFinalField1(){
fld = 5;
}
public static void setFinalField2(){
fld = 2;
}
}
which cannot be compiled with javac, but can be loaded and executed by JVM.
JVM HotSpot has special treatment of such classes in the sense that it prevents such "constants" from participating in constant folding. This check is done on the bytecode rewriting phase of class initialization:
// Check if any final field of the class given as parameter is modified
// outside of initializer methods of the class. Fields that are modified
// are marked with a flag. For marked fields, the compilers do not perform
// constant folding (as the field can be changed after initialization).
//
// The check is performed after verification and only if verification has
// succeeded. Therefore, the class is guaranteed to be well-formed.
InstanceKlass* klass = method->method_holder();
u2 bc_index = Bytes::get_Java_u2(bcp + prefix_length + 1);
constantPoolHandle cp(method->constants());
Symbol* ref_class_name = cp->klass_name_at(cp->klass_ref_index_at(bc_index));
if (klass->name() == ref_class_name) {
Symbol* field_name = cp->name_ref_at(bc_index);
Symbol* field_sig = cp->signature_ref_at(bc_index);
fieldDescriptor fd;
if (klass->find_field(field_name, field_sig, &fd) != NULL) {
if (fd.access_flags().is_final()) {
if (fd.access_flags().is_static()) {
if (!method->is_static_initializer()) {
fd.set_has_initialized_final_update(true);
}
} else {
if (!method->is_object_initializer()) {
fd.set_has_initialized_final_update(true);
}
}
}
}
}
}
The only restriction that JVM HotSpot checks is that the final field should not be modified outside of the class that the final field is declared at.
3 Comments
EVIL and beautiful.With JDK 18 this won't be possible anymore due to the reimplementation of the core reflection over invokedynamic and MethodHandles as part of JEP-416 (PR).
Quote of Mandy Chung – who is the main author of this incredible work – in the following comment. Emphasis are mine.
If the underlying field is final, a
Fieldobject has write access if and only if
setAccessible(true)has succeeded for thisFieldobject;- the field is non-static; and
- the field's declaring class is not a hidden class; and
- the field's declaring class is not a record class.
Comments
Many of the answers here are useful, but I've found none of them to work on Android, in particular. I'm even a pretty hefty user of Reflect by joor, and neither it nor apache's FieldUtils - both mentioned here in some of the answers, do the trick.
Problem with Android
The fundamental reason why this is so is because on Android there's no modifiers field in the Field class, which renders any suggestion involving this code (as in the marked answer), useless:
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
In fact, to quote from FieldUtils.removeFinalModifier():
// Do all JREs implement Field with a private ivar called "modifiers"?
final Field modifiersField = Field.class.getDeclaredField("modifiers");
So, the answer is no...
Solution
Pretty easy - instead of modifiers, the field name is accessFlags. This does the trick:
Field accessFlagsField = Field.class.getDeclaredField("accessFlags");
accessFlagsField.setAccessible(true);
accessFlagsField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
Side-note #1: this can work regardless of whether the field is static in the class, or not.
Side-note #2: Seeing that the field itself could be private, it's recommended to also enable access over the field itself, using
field.setAccessible(true)(in addition toaccessFlagsField.setAccessible(true).
1 Comment
java.lang.NoSuchFieldException: No field modifiers in class Ljava/lang/reflect/Field; (declaration of 'java.lang.reflect.Field' appears in /apex/com.android.runtime/javalib/core-oj.jar). The suggested solution works in my case. (This error currently has only one google result, so hopefully people will now find this page)Just saw that question on one of the interview question, if possible to change final variable with reflection or in runtime. Got really interested, so that what I became with:
/**
* @author Dmitrijs Lobanovskis
* @since 03/03/2016.
*/
public class SomeClass {
private final String str;
SomeClass(){
this.str = "This is the string that never changes!";
}
public String getStr() {
return str;
}
@Override
public String toString() {
return "Class name: " + getClass() + " Value: " + getStr();
}
}
Some simple class with final String variable. So in the main class import java.lang.reflect.Field;
/**
* @author Dmitrijs Lobanovskis
* @since 03/03/2016.
*/
public class Main {
public static void main(String[] args) throws Exception{
SomeClass someClass = new SomeClass();
System.out.println(someClass);
Field field = someClass.getClass().getDeclaredField("str");
field.setAccessible(true);
field.set(someClass, "There you are");
System.out.println(someClass);
}
}
The output will be as follows:
Class name: class SomeClass Value: This is the string that never changes!
Class name: class SomeClass Value: There you are
Process finished with exit code 0
According to documentation https://docs.oracle.com/javase/tutorial/reflect/member/fieldValues.html
2 Comments
static final field, so this code doesn't work. setAccessible(true) only works for setting final instance fields.The accepted answer worked for me until deployed on JDK 1.8u91.
Then I realized it failed at field.set(null, newValue); line when I had read the value via reflection before calling of setFinalStatic method.
Probably the read caused somehow different setup of Java reflection internals (namely sun.reflect.UnsafeQualifiedStaticObjectFieldAccessorImpl in failing case instead of sun.reflect.UnsafeStaticObjectFieldAccessorImpl in success case) but I didn't elaborate it further.
Since I needed to temporarily set new value based on old value and later set old value back, I changed signature little bit to provide computation function externally and also return old value:
public static <T> T assignFinalField(Object object, Class<?> clazz, String fieldName, UnaryOperator<T> newValueFunction) {
Field f = null, ff = null;
try {
f = clazz.getDeclaredField(fieldName);
final int oldM = f.getModifiers();
final int newM = oldM & ~Modifier.FINAL;
ff = Field.class.getDeclaredField("modifiers");
ff.setAccessible(true);
ff.setInt(f,newM);
f.setAccessible(true);
T result = (T)f.get(object);
T newValue = newValueFunction.apply(result);
f.set(object,newValue);
ff.setInt(f,oldM);
return result;
} ...
However for general case this would not be sufficient.
Comments
The top rated answer does not work with the new Reflection implementation of JEP416 in e.g. Java 21 that uses MethodHandles and ignores the flags value on the Field abstraction object.
One solution is to use Unsafe, however with this JEP Unsafe and the important long objectFieldOffset(Field f) and
long staticFieldOffset(Field f) methods are getting deprecated for removal so for example this will not work in the future:
final Unsafe unsafe = //..get Unsafe (...and add subsequent --add-opens statements for this to work)
final Field ourField = Example.class.getDeclaredField("changeThis");
final Object staticFieldBase = unsafe.staticFieldBase(ourField);
final long staticFieldOffset = unsafe.staticFieldOffset(ourField);
unsafe.putObject(staticFieldBase, staticFieldOffset, "it works");
I do not recommend this but it is possible in Java 21 with the new reflection implementation when making heavy use of the internal API if really needed.
Java 21+ solution without Unsafe
The gist of it is to use a MethodHandle that can write to a static final field by getting it from the internal getDirectFieldNoSecurityManager(...) method of the Lookup by providing it with a MemberName that is manipulated via Reflection to remove the final flag from it.
You will need to add the following JVM options for this to work:
--add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.base/java.lang.reflect=ALL-UNNAMED
public class FinalFieldUtil {
private static class MemberNameWrapper {
protected final Class<?> MEMBER_NAME_CLASS;
private final Constructor<?> MEMBER_NAME_CONSTRUCTOR;
private final Field MEMBER_NAME_FLAGS_FIELD;
private final Method MEMBER_NAME_GET_REFERENCE_KIND_METHOD;
{
try {
MEMBER_NAME_CLASS = Class.forName("java.lang.invoke.MemberName");
MEMBER_NAME_CONSTRUCTOR = MEMBER_NAME_CLASS.getDeclaredConstructor(Field.class, boolean.class); //e.g. new MemberName(myField, true);
MEMBER_NAME_CONSTRUCTOR.setAccessible(true);
MEMBER_NAME_FLAGS_FIELD = MEMBER_NAME_CLASS.getDeclaredField("flags");
MEMBER_NAME_FLAGS_FIELD.setAccessible(true);
MEMBER_NAME_GET_REFERENCE_KIND_METHOD = MEMBER_NAME_CLASS.getDeclaredMethod("getReferenceKind");
MEMBER_NAME_GET_REFERENCE_KIND_METHOD.setAccessible(true);
} catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException e) {
throw new RuntimeException(e);
}
}
final Object instance;
public MemberNameWrapper(Field field, boolean makeSetter) {
try {
instance = MEMBER_NAME_CONSTRUCTOR.newInstance(field, makeSetter);
removeFinality(instance);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private final void removeFinality(Object memberNameInstance) throws IllegalAccessException {
//Manipulate flags to remove hints to it being final
final int initialFlags = MEMBER_NAME_FLAGS_FIELD.getInt(memberNameInstance);
if (!Modifier.isFinal(initialFlags)) {
return;
}
final int nonFinalFlags = initialFlags & ~Modifier.FINAL;
MEMBER_NAME_FLAGS_FIELD.setInt(memberNameInstance, nonFinalFlags);
}
protected Object getMemberNameInstance(){
return instance;
}
protected byte getReferenceKind() {
try {
return (byte) MEMBER_NAME_GET_REFERENCE_KIND_METHOD.invoke(instance);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
}
private static class LookupWrapper {
private final Class<?> LOOKUP_CLASS;
private final Method LOOKUP_GET_FIELD_VAR_HANDLE_NO_SECURITY_MANAGER_METHOD;
{
try {
LOOKUP_CLASS = Lookup.class;
LOOKUP_GET_FIELD_VAR_HANDLE_NO_SECURITY_MANAGER_METHOD = LOOKUP_CLASS.getDeclaredMethod("getDirectFieldNoSecurityManager", byte.class, Class.class, Class.forName("java.lang.invoke.MemberName"));
LOOKUP_GET_FIELD_VAR_HANDLE_NO_SECURITY_MANAGER_METHOD.setAccessible(true);
} catch (NoSuchMethodException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
private final Lookup lookup;
private LookupWrapper(Lookup lookup) {
this.lookup = lookup;
}
public MethodHandle unreflectVarHandleUnrestricted(Field field) {
final MemberNameWrapper memberNameSetterWrapper = new MemberNameWrapper(field, true);
final byte setterReferenceKind = memberNameSetterWrapper.getReferenceKind();
final Object memberNameSetter = memberNameSetterWrapper.getMemberNameInstance();
try {
return (MethodHandle) LOOKUP_GET_FIELD_VAR_HANDLE_NO_SECURITY_MANAGER_METHOD.invoke(lookup, setterReferenceKind, field.getDeclaringClass(), memberNameSetter);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
}
public static void setStaticFinalField(Field field, Object value) throws Throwable {
if (Modifier.isFinal(field.getModifiers()) && field.getType().isPrimitive()) {
throw new IllegalArgumentException("primitive finals are not supported, because their modification depends on very specific circumstances.");
}
final LookupWrapper lookupWrapper = new LookupWrapper(MethodHandles.privateLookupIn(field.getDeclaringClass(), MethodHandles.lookup()));
final MethodHandle methodHandle = lookupWrapper.unreflectVarHandleUnrestricted(field);
methodHandle.invoke(value);
}
}
And then you can
public class TestModelClassWithFinalFields {
public static final String myStr; //can be set
public static final List<Object> myList = List.of(); //can be set
public static final String myInlineStr = "init"; //cannot be set with this method!
static {
myStr = "initial static value";
}
}
FinalFieldUtil.setStaticFinalField(TestModelClassWithFinalFields.class.getDeclaredField("myStr"), "new value!");
System.out.println(FinalFieldUtil.myStr);
Please note that this will not work for pure String literals and primitive values when set inline (e.g. static final String = "str")
See my answer here for a the initial attempt and thoughts regarding using the internal API to set a final field in Java 21 without Unsafe.
2 Comments
To make this worked with JDK 21 you can use option -Djdk.reflect.useDirectMethodHandle=false
Make accessable for JDK 11 - 17
public static void setFieldAccessible(Field field) throws Exception {
field.setAccessible(true);
Method getDeclaredFields0 = Class.class.getDeclaredMethod("getDeclaredFields0", boolean.class);
getDeclaredFields0.setAccessible(true);
Field[] fields = (Field[]) getDeclaredFields0.invoke(Field.class, false);
for (Field each : fields) {
if ("modifiers".equals(each.getName())) {
each.setAccessible(true);
each.setInt(field, field.getModifiers() & ~Modifier.FINAL);
break;
}
}
}
2 Comments
-Djdk.reflect.useDirectMethodHandle=false --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.lang.reflect=ALL-UNNAMEDIf your field is simply private you can do this:
MyClass myClass= new MyClass();
Field aField= myClass.getClass().getDeclaredField("someField");
aField.setAccessible(true);
aField.set(myClass, "newValueForAString");
and throw/handle NoSuchFieldException
Comments
Note that there is work ongoing in OpenJDK to "make final mean final": https://openjdk.org/jeps/500
The ability to mutate final instance fields via reflection was added in Java 5 in 2004 to support third-party serialization libraries. This is now regarded as a historical mistake, because it interferes with integrity and with performance optimizations.
They plan to add an alternative "limited-purpose API" for serialization libraries, and then crack down on final field mutation. This will also close the loopholes that allow manipulating the modifiers to mutate final static fields.
For now this is still a candidate JEP, not yet targeted at a particular Java version. Assuming it goes thru, at some point, the ability to mutate any final fields via reflection will generate a nuisance warning on the console, and later, an error, unless you add a special command line override. (They are not proposing to kill the ability entirely... yet.)
Already it's impossible to mutate a final field of a record and it will also be impossible to mutate a "strict" final field, which all fields of Valhalla value classes will be (which will include some classes which exist today if they are upgraded to value classes).
There will always be some kind of workaround, even if it's just hex-editing the bytecode of the class files before the VM sees them. Or compiling your own VM. But the point is that mutating final fields is going to keep getting more difficult. If you can find some way to do what you need within the language instead, it will be easier in the long run, even if it's a nuisance right now.
Comments
In openjdk17 I had to remove final modifier not just from Field.modifiers, but also from Field.root.modifiers.
Field.root is used internally in field.set(...) here.
So my code is this:
private static void makeStaticFinalFieldWritable( Field field ) {
try {
var lookup = MethodHandles.privateLookupIn( field.getClass(), MethodHandles.lookup() );
var modifiersHandle = lookup.findVarHandle( field.getClass(), "modifiers", int.class );
var modifiers = field.getModifiers();
modifiersHandle.set( field, modifiers & ~Modifier.FINAL );
var rootHandle = lookup.findVarHandle( field.getClass(), "root", Field.class );
var root = ( Field ) rootHandle.get( field );
if ( root != null && root != field ) {
makeStaticFinalFieldWritable( root );
}
}
catch ( IllegalAccessException | NoSuchFieldException e ) {
throw new Error( e );
}
}
1 Comment
The whole point of a final field is that it cannot be reassigned once set. The JVM uses this guarentee to maintain consistency in various places (eg inner classes referencing outer variables). So no. Being able to do so would break the JVM!
The solution is not to declare it final in the first place.
12 Comments
final has a special role in multithreaded execution - changing final values would break the Java memory model too.final should not be declared static.Explore related questions
See similar questions with these tags.
System.out/in/errare so "special" that the Java Memory Model has to make special mention of them. They are not examples which should be followed.