您的位置:首页 > 编程语言 > Java开发

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

2016-07-27 16:18 519 查看
只前转载了一篇文章《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  。。。。

血与泪告诉我们看文档的重要性。特别像是我这样的小白!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: