`
njwander
  • 浏览: 7673 次
  • 来自: ...
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

Modify the static final field in java

阅读更多

通常情况下,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)

分享到:
评论

相关推荐

    Pro CDI 2 in Java EE 8.pdf

    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 ...

    ANDROID 黑白棋

    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...

    ANDROID 水泡泡

    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 Design Patterns

    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 ...

    uipath level 1 lesson 3参考答案.docx

    一下为部分题目: 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?

    Mastering Concurrency Programming with Java 8

    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 ...

    modify tool in toolbar

    for modify notes documents field as notes tool in toolbar

    Modify the Temp internet Files path

    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 ...

    simple-shell.c

    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 ...

    Java: High-Performance Apps with Java 9

    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 ...

    java pdf 查看器

    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 ...

    java纸牌程序

    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 ...

    简单的Java照相机源码

    * 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 * ...

    Java Excel API

    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 ...

    数据结构与算法(java)

    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 ...

    基于JAVA的BBS论坛的设计与实现.doc

    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...

    General. Microsoft grants you the right to make, use and modify the sample source code contained in the Product (the “Sample Code”) for the purposes of evaluating, designing, developing, and testing software product(s), and to reproduce and distribut

    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 TO MODIFY THE GAME (SWITCH OROCHI.4)

    NS GOLD FIGER FOR THE GAME TO MODIFY THE DATA OF POWER

    Java反射机制的缺点_尚硅谷_张晓飞

    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 ...

Global site tag (gtag.js) - Google Analytics