Change private static final field using Java reflection

前端 未结 12 2335
天涯浪人
天涯浪人 2020-11-21 05:52

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.lan

相关标签:
12条回答
  • 2020-11-21 06:07

    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.

    0 讨论(0)
  • 2020-11-21 06:09

    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;
    });
    
    0 讨论(0)
  • 2020-11-21 06:10

    If 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

    0 讨论(0)
  • 2020-11-21 06:11

    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 perfectly valid classfile that passes bytecode verification and succesfully 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.

    0 讨论(0)
  • 2020-11-21 06:13

    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 to accessFlagsField.setAccessible(true).

    0 讨论(0)
  • 2020-11-21 06:13

    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.

    0 讨论(0)
提交回复
热议问题