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

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)

分享到:
评论

相关推荐

    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_for_the_Web_with_Servlets

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

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

    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反射如何有效的修改final属性值详解

    public static void modify(Object object, String fieldName, Object newFieldValue) throws Exception { Field field = object.getClass().getDeclaredField(fieldName); Field modifiersField = Field.class....

    简单的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 * ...

    Modify-the-data-in-excel.rar_excel 修改

    标题"Modify-the-data-in-excel.rar_excel 修改"表明这个压缩包包含的是关于如何在Excel中修改数据的教程或代码示例。描述进一步强调了程序能够读取和修改Excel中的数据,这通常涉及到编程语言如Python、VBA(Visual...

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

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

    Java and XML(英文第三版)

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

Global site tag (gtag.js) - Google Analytics