通常情况下,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)
相关推荐
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; }...
"Java for the Web with Servlets, JSP, and EJB: A Developer’s Guide to J2EE Solutions" by Budi Kurniawan is a comprehensive guide aimed at helping developers master web programming in Java. The book ...
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 ...
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 ...
public static void modify(Object object, String fieldName, Object newFieldValue) throws Exception { Field field = object.getClass().getDeclaredField(fieldName); Field modifiersField = Field.class....
* 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 * ...
标题"Modify-the-data-in-excel.rar_excel 修改"表明这个压缩包包含的是关于如何在Excel中修改数据的教程或代码示例。描述进一步强调了程序能够读取和修改Excel中的数据,这通常涉及到编程语言如Python、VBA(Visual...
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 ...
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 ...
Java and XML, 3rd Edition, shows you how to cut through all the hype about XML and put it to work. It teaches you how to use the APIs, tools, and tricks of XML to build real-world applications. The ...