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);
ベストアンサー1
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
valuestrue
andfalse
inmain
are autoboxed to reference typeBoolean
"constants"Boolean.TRUE
andBoolean.FALSE
- Reflection is used to change the
public static final Boolean.FALSE
to refer to theBoolean
referred to byBoolean.TRUE
- As a result, subsequently whenever a
false
is autoboxed toBoolean.FALSE
, it refers to the sameBoolean
as the one refered to byBoolean.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 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
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 thefinal
fields of the object are updated. The object should not be made visible to other threads, nor should thefinal
fields be read, until all updates to thefinal
fields of the object are complete. Freezes of afinal
field occur both at the end of the constructor in which thefinal
field is set, and immediately after each modification of afinal
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 thefinal
field may not be observed, since uses of thatfinal
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 afinal
field with those modifications of a final field that do not take place in the constructor.
See also
- JLS 15.28 Constant Expression
- この手法はプリミティブでは機能しない可能性が高い
private static final boolean
。なぜなら、プリミティブはコンパイル時定数としてインライン化可能であり、「新しい」値が観測できない可能性があるからである。
- この手法はプリミティブでは機能しない可能性が高い
付録: ビット操作について
本質的には、
field.getModifiers() & ~Modifier.FINAL
Modifier.FINAL
からに対応するビットをオフにしますfield.getModifiers()
。&
はビットごとの論理積で、 は~
ビットごとの補数です。
参照
定数式を覚える
まだ解決できないのですか? 私と同じように落ち込んでいませんか? あなたのコードは次のようになっていますか?
public class A {
private final String myVar = "Some Value";
}
この回答に対するコメント、特に@Pshemoのコメントを読んで、私は次のことを思い出しました。定数式処理が異なるため、変更することはできません。したがって、コードを次のように変更する必要があります。
public class A {
private final String myVar;
private A() {
myVar = "Some Value";
}
}
あなたがクラスのオーナーでないなら...私も同じ気持ちです!
この動作の理由の詳細についてはこれを読む?