对java Field中的set()方法以及Modifier的一些思考

只前转载了一篇文章《JAVA反射修改常量,以及其局限》,受益匪浅,在这里再次感谢原作者。

在看完文章后,我又试着写了一个测试程序,在期间也遇到了一些问题,写在这里,供大家参考。

程序入下:

import java.lang.reflect.Field;

class My{
    private static final int value= 1;
    private static String str="sds";
    public  static int getValue()
    {
    	return value;
    }
    public String getString()
    {
    	return str;
    }
}
public class FinalReflect{
	public static void main(String[] args) throws Exception
	{
		Class c = Class.forName("reflect.My");
	    My M = (My)c.newInstance();
	    Field f = c.getDeclaredField("value");
	    Field s=c.getDeclaredField("str");
	    s.setAccessible(true);
	    f.setAccessible(true);
	  //  f.set(M,5);//对于static final 变量这里会出错!   
	    s.set(null ,"ss");    
	    System.out.println("str: "+s.get(M));
	    System.out.println("Field.get:"+f.get(M));
	    System.out.println(M.getString());
	    System.out.println(M.getValue());
	}
}
一个问题是,当我将value设为static final 的时候,f.set(M,5)这里会出错;而将final或static去掉的时候,set(M,5)都不会出错。这就奇了怪了,于是我查了下文档,发现这句话:

If the underlying field is final, the method throws an  IllegalAccessException unless setAccessible(true) has succeeded for this Field object and the field is non-static.

也就是说static final 会使得方法抛出一个异常。


那通过反射将final字段修改掉行不行啊

于是我又改了下程序:

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

class My{
    private static final Integer value= 1;
    private static String str="sds";
    public static int getValue()
    {
    	return value;
    }
    public String getString()
    {
    	return str;
    }
}
public class FinalReflect{
	public static void main(String[] args) throws Exception
	{
	    Class c = Class.forName("reflect.My");
	    My M = (My)c.newInstance();
	    Field f = c.getDeclaredField("value");
	    Field s=c.getDeclaredField("str");
	    s.setAccessible(true);
	    f.setAccessible(true);  
     <span style="white-space:pre">	</span>    Field modifiersField = Field.class.getDeclaredField("modifiers");  
            modifiersField.setAccessible(true);  
            System.out.println("Modifier before modify: "+f+" "+f.getModifiers());
            modifiersField.setInt(f, f.getModifiers() & ~Modifier.FINAL);  
            System.out.println("Modifier after modify: "+f+" "+f.getModifiers());        
            f.set(M,5);
	    s.set(null ,"ss");    
	    System.out.println("Field.get: "+f.get(M));
	    System.out.println("Field.get: "+s.get(M));
	    
	}
}
输出为

Modifier before modify: private static final java.lang.Integer reflect.My.value 26
Modifier after modify: private static java.lang.Integer reflect.My.value 10
Field.get: 5
Field.get: ss


我们看到打印出来的前两行,final 真的消失了,value的修饰符被反射修改了   

再看一下Modifier.class中定义的  public static final int FINAL = 0x00000010; 确实是减少的16;

那么剩下的值代表什么呢?

对了,就是static和private

public static final int STATIC  = 0x00000008;

public static final int PRIVAT   = 0x00000002;


到这里终于知道为啥对于static final修饰的变量  反射的时候需要用到Modifier了;而只是final修饰的时候其实并不一定需要。

这里还有一个小知识点,我之前看到网上许多人在使用set()的时候第一个参数用null 然后我就懵了,为啥我用就出错?看了文档才发现

If the underlying field is static, the obj argument is ignored; it may be null.  人家是针对static的,我用的使用写的是final  。。。。


血与泪告诉我们看文档的重要性。特别像是我这样的小白!




  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值