使用 Java 反射更改私有静态最终字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3301635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change private static final field using Java reflection
提问by fixitagain
I have a class with a private static final
field that, unfortunately, I need to change it at run-time.
我有一个带有private static final
字段的类,不幸的是,我需要在运行时更改它。
Using reflection I get this error: java.lang.IllegalAccessException: Can not set static final boolean field
使用反射我得到这个错误: 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);
采纳答案by polygenelubricants
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.
假设 noSecurityManager
阻止您这样做,您可以使用setAccessible
绕过private
并重置修饰符以摆脱final
,并实际修改private static final
字段。
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"
.
假设SecurityException
抛出no ,上面的代码打印"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"
- 原始
boolean
值true
和false
inmain
被自动装箱以引用类型Boolean
“常量”Boolean.TRUE
和Boolean.FALSE
- 反射用于将
public static final Boolean.FALSE
引用更改为引用的Boolean
引用Boolean.TRUE
- 结果,随后每当 a
false
被自动装箱到 时Boolean.FALSE
,它所指的与Boolean
由Boolean.TRUE
"false"
现在的一切都是"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
- 使用反射来改变
static final File.separatorChar
单元测试 - 如何将 setAccessible 限制为仅“合法”使用?
- 有搞乱
Integer
缓存、改变 aString
等的例子
- 有搞乱
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.
每当您执行此类操作时,都应格外小心。它可能不起作用,因为 aSecurityManager
可能存在,但即使不存在,根据使用模式,它可能会也可能不会起作用。
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.
在某些情况下,例如反序列化,系统将需要
final
在构造后更改对象的字段。final
字段可以通过反射和其他依赖于实现的方式来改变。具有合理语义的唯一模式是构造一个对象然后final
更新对象的字段。在对象final
字段的所有更新final
完成之前,不应使对象对其他线程可见,也不应读取字段。final
字段的冻结发生在final
设置该字段的构造函数的末尾,以及在每次final
通过反射或其他特殊机制修改字段后立即发生。即便如此,仍有许多并发症。如果
final
字段在字段声明中被初始化为编译时常量,则final
可能不会观察到对该字段的更改,因为该final
字段的使用在编译时被替换为编译时常量。另一个问题是规范允许对
final
字段进行积极的优化。在一个线程中,允许对一个final
字段的读取重新排序,并在构造函数中没有发生对 final 字段的那些修改。
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
- JLS 15.28 常量表达式
- 这种技术不太可能适用于原语
private static final boolean
,因为它作为编译时常量是内联的,因此“新”值可能无法观察到
- 这种技术不太可能适用于原语
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.
关闭对应于Modifier.FINAL
from的位field.getModifiers()
。&
是按位与,~
是按位补码。
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 Expressionsare handled different so it will be impossibleto modify it. Hence you will need to change your code to look like this:
阅读对此答案的评论,特别是@Pshemo 的评论,它提醒我常量表达式的处理方式不同,因此无法对其进行修改。因此,您需要将代码更改为如下所示:
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?
有关此行为原因的更多详细信息,请阅读此内容?
回答by thecoop
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!
final
字段的全部意义在于它一旦设置就不能重新分配。JVM 使用此保证来维护各个地方的一致性(例如,引用外部变量的内部类)。所以不行。能够这样做会破坏JVM!
The solution is not to declare it final
in the first place.
解决方案不是首先声明它final
。
回答by erickson
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.
如果分配给static final boolean
字段的值在编译时已知,则它是一个常量。原始字段或String
类型字段
可以是编译时常量。任何引用该字段的代码都将内联一个常量。由于该字段实际上并未在运行时读取,因此更改它不会产生任何影响。
The Java language specificationsays this:
在Java语言规范这样说:
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)
如果一个字段是一个常量变量(第 4.12.4 节),那么删除关键字 final 或更改其值不会通过导致它们不运行而破坏与预先存在的二进制文件的兼容性,但它们不会看到任何新的用法值除非它们被重新编译。即使用法本身不是编译时常量表达式也是如此(第 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).
如果您反编译Checker
,您将看到Flag.FLAG
代码只是将值 1 ( true
) 压入堆栈(指令 #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
回答by Stephan Markwalder
A little curiosity from the Java Language Specification, chapter 17, section 17.5.4 "Write-protected Fields":
来自 Java Language Specification,第 17 章,第 17.5.4 节“写保护字段”的一点好奇:
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.
通常,不能修改最终和静态的字段。但是,System.in、System.out 和 System.err 是静态最终字段,出于遗留原因,必须允许通过 System.setIn、System.setOut 和 System.setErr 方法更改这些字段。我们将这些字段称为写保护字段,以将它们与普通的最终字段区分开来。
Source: http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.5.4
来源:http: //docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.5.4
回答by iirekm
I also integrated it with joor library
我还将它与joor库集成
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.
我还修复override
了以前的解决方案似乎错过的问题。但是,只有在没有其他好的解决方案时才非常小心地使用它。
回答by hasskell
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;
一些带有最终 String 变量的简单类。所以在主类中导入 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
根据文档 https://docs.oracle.com/javase/tutorial/reflect/member/fieldValues.html
回答by VanagaS
In case of presence of a Security Manager, one can make use of AccessController.doPrivileged
如果存在安全管理员,则可以使用 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:
在 lambda 表达式中,AccessController.doPrivileged
, 可以简化为:
AccessController.doPrivileged((PrivilegedAction) () -> {
modifiersField.setAccessible(true);
return null;
});
回答by Tomá? Zálusky
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.
接受的答案对我有用,直到部署在 JDK 1.8u91 上。然后我意识到field.set(null, newValue);
当我在调用setFinalStatic
方法之前通过反射读取值时它失败了。
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.
可能读取导致 Java 反射内部的某种不同设置(即sun.reflect.UnsafeQualifiedStaticObjectFieldAccessorImpl
在失败的情况下而不是sun.reflect.UnsafeStaticObjectFieldAccessorImpl
在成功的情况下),但我没有进一步详细说明。
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.
然而,对于一般情况,这还不够。
回答by nndru
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.
除了排名靠前的答案,您还可以使用最简单的方法。Apache commonsFieldUtils
类已经有可以做这些事情的特定方法。请看一下FieldUtils.removeFinalModifier
方法。您应该指定目标字段实例和可访问性强制标志(如果您使用非公共字段)。您可以在此处找到更多信息。
回答by Philip Rego
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
并抛出/处理 NoSuchFieldException