Change private static final field using Java reflection

前端 未结 12 2314
天涯浪人
天涯浪人 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:14

    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

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

    A 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

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

    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.

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

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

    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 boolean values true and false in main are autoboxed to reference type Boolean "constants" Boolean.TRUE and Boolean.FALSE
    • Reflection is used to change the public static final Boolean.FALSE to refer to the Boolean referred to by Boolean.TRUE
    • As a result, subsequently whenever a false is autoboxed to Boolean.FALSE, it refers to the same Boolean as the one refered to by Boolean.TRUE
    • Everything that was "false" now is "true"

    Related questions

    • Using reflection to change static final File.separatorChar for unit testing
    • How to limit setAccessible to only “legitimate” uses?
      • Has examples of messing with Integer's cache, mutating a String, etc

    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 final fields of an object after construction. final fields 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 the final fields of the object are updated. The object should not be made visible to other threads, nor should the final fields be read, until all updates to the final fields of the object are complete. Freezes of a final field occur both at the end of the constructor in which the final field is set, and immediately after each modification of a final field via reflection or other special mechanism.

    Even then, there are a number of complications. If a final field is initialized to a compile-time constant in the field declaration, changes to the final field may not be observed, since uses of that final field are replaced at compile time with the compile-time constant.

    Another problem is that the specification allows aggressive optimization of final fields. Within a thread, it is permissible to reorder reads of a final field 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

    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

    • Wikipedia/Bitwise operation

    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?

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

    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.

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