通常情况下,java对象中的属性被指定为static final后,是不能被修改的,但是可以通过下列代码进行修改。
private static void setStaticFinalField(@NotNull Field field, @Nullable Object value) throws IllegalAccessException
{
Field modifiersField;
try {
modifiersField = Field.class.getDeclaredField("modifiers");
}
catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
modifiersField.setAccessible(true);
int nonFinalModifiers = modifiersField.getInt(field) - FINAL;
modifiersField.setInt(field, nonFinalModifiers);
//noinspection UnnecessaryFullyQualifiedName,UseOfSunClasses
sun.reflect.FieldAccessor accessor =
sun.reflect.ReflectionFactory.getReflectionFactory().newFieldAccessor(field, false);
accessor.set(null, value);
}
如果field只是final类型,那么可以先设定field为accessable,
static void ensureThatMemberIsAccessible(@NotNull AccessibleObject classMember)
{
if (!classMember.isAccessible()) {
classMember.setAccessible(true);
}
}
再通过反射进行修改
field.set(targetObject, value);
代码来源:
mockit.internal.util.FieldReflection.setFieldValue(Field, Object, Object)
相关推荐
They show you how to use CDI in a Java SE environment with the new CDI initialization and configuration API, and how to dynamically modify the configuration of beans at application startup by using ...
public static final class attr { } public static final class drawable { public static final int black=0x7f020000; public static final int empty=0x7f020001; public static final int icon=0x7f...
public static final class attr { } public static final class drawable { public static final int icon=0x7f020000; } public static final class layout { public static final int main=0x7f030000; }...
A CD containing all of the examples in the book, so you can run, edit, and modify the complete working programs, , After reading this tutorial, you will be comfortable with the basics of design ...
一下为部分题目: 2. The String.Format(“Input = {0} and Output = {1}”, “1”,”2”) expression returns which of ...6. Which activity can be used to modify the value of an existing cell in a DataTable?
Design concurrent applications by converting a sequential algorithm into a concurrent oneDiscover how to avoid all the possible problems you can get in concurrent algorithmsUse the Executor framework ...
for modify notes documents field as notes tool in toolbar
Modify the Temp internet Files pathModify the Temp internet Files pathModify the Temp internet Files pathModify the Temp internet Files pathModify the Temp internet Files pathModify the Temp internet ...
1.The first task is to modify the main() function in Figure 3.36 so that a child process is forked and executes the command specified by the user. The next task is to modify the shell interface ...
In 1999, with his wife Luda and two daughters, he emigrated to the USA and has been living in Colorado ever since, working as a Java programmer. In his free time, Nick likes to write and hike in the ...
public final static String TITLE = "SwingLabs PDF Viewer"; /** The current PDFFile */ PDFFile curFile; /** the name of the current document */ String docName; /** The split between thumbs and ...
3.(7.30 Card Shuffling and Dealing) Modify the application of Fig. 7.11 to deal a five-card poker hand. Then modify class DeckOfCards of Fig. 7.10 to include methods that determine whether a hand ...
* restriction, including without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the * ...
In addition, it contains a mechanism which allows java applications to read in a spreadsheet, modify some cells and write out the new spreadsheet. This API allows non Windows operating systems to ...
Not filled with with obtuse mathematics and difficult proofs, MWSS:Data Structures and Algorithms in Java removes the mystique from DSA. It does this in two ways. First, the text is written in a ...
It makes the communication between people, communication becomes easier, especially in the field of IT, we were only able to communicate well, technology will be promoted step by step. Therefore, how...
IMPORTANT - READ CAREFULLY: This End-User License Agreement (“EULA”) is a legal agreement between you (either an individual or a single entity) and Microsoft for the Microsoft software product ...
NS GOLD FIGER FOR THE GAME TO MODIFY THE DATA OF POWER
Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature ...