深入理解 Java 反射:Field (成员变量)
2017-12-14 08:29
771 查看
Thanks
Field 成员变量的介绍
每个成员变量有类型和值。
java.lang.reflect.Field 为我们提供了获取当前对象的成员变量的类型,和重新设值的方法。
获取变量的类型
类中的变量分为两种类型:基本类型和引用类型:
基本类型( 8 种) 整数:byte, short, int, long 浮点数:float, double 字符:char 布尔值:boolean 引用类型 所有的引用类型都继承自 java.lang.Object 类,枚举,数组,接口都是引用类型 java.io.Serializable 接口,基本类型的包装类(比如 java.lang.Double)也是引用类型
java.lang.reflect.Field 提供了两个方法获去变量的类型:
Field.getType():返回这个变量的类型 Field.getGenericType():如果当前属性有签名属性类型就返回,否则就返回 Field.getType()
举个例子:
public class FieldSpy extends BaseTestClass {
public boolean[][] b = {{true, true}, {false, false}};
public String name = “shixinzhang”;
public Integer integer = 23;
public T type;
public static final String CLASS_NAME = "net.sxkeji.shixinandroiddemo2.test.reflection.FieldSpy"; public static void main(String[] args) { try { Class<?> aClass = Class.forName(CLASS_NAME); Field[] fields = aClass.getFields(); for (Field field : fields) { printFormat("Field:%s \n",field.getName()); printFormat("Type:\n %s\n", field.getType().getCanonicalName()); printFormat("GenericType:\n %s\n", field.getGenericType().toString()); printFormat("\n\n"); } } catch (ClassNotFoundException e) { e.printStackTrace(); } }
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
运行结果:
Field:b //二维的布尔值数组
Type:
boolean[][]
GenericType:
class [[Z
Field:name
Type:
java.lang.String
GenericType:
class java.lang.String
Field:integer
Type:
java.lang.Integer
GenericType:
class java.lang.Integer
Field:type //泛型 T 类型,运行时被擦除为 Object
Type:
java.lang.Object
GenericType:
T
Field:CLASS_NAME
Type:
java.lang.String
GenericType:
class java.lang.String
Process finished with exit code 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2425
26
27
28
29
30
3132
33
34
35
36
37
获取成员变量的修饰符
成员变量可以被以下修饰符修饰:
访问权限控制符:public, protected, private 限制只能有一个实例的:static 不允许修改的:final 不会被序列化:transient 线程共享数据的一致性:volatile 注解
类似获取 Class 的修饰符,我们可以使用 Field.getModifiers() 方法获取当前成员变量的修饰符。
返回 java.lang.reflect.Modifier 中定义的整形值。
由于 Field 间接继承了 java.lang.reflect.AnnotatedElement ,因此运行时也可以获得修饰成员变量的注解,当然前提是这个注解被 java.lang.annotation.RetentionPolicy.RUNTIME 修饰。
获取和修改成员变量的值
拿到一个对象后,我们可以在运行时修改它的成员变量的值,对运行时来说,反射修改变量值的操作和类中修改变量的结果是一样的。
举个例子:
enum MyHabit {
LOL,
CODE
}
public class People extends BaseTestClass {
public long idCarNumber = 1000000000;
public String[] name = {“shi”, “xin”};
public MyHabit habit = MyHabit.CODE;
public static void main(String[] args) { People shixin = new People(); String fmt = "%6s: %s = %s \n"; Class<? extends People> cls = shixin.getClass(); try { Field idCarNumber = cls.getDeclaredField("idCarNumber"); System.out.format(fmt, "before", idCarNumber.getName(), shixin.idCarNumber); idCarNumber.setLong(shixin, 123456); System.out.format(fmt, "after", idCarNumber.getName(), shixin.idCarNumber); Field name = cls.getDeclaredField("name"); System.out.format(fmt, "before", name.getName(), Arrays.asList(shixin.name)); name.set(shixin, new String[]{"hei", "hei"}); System.out.format(fmt, "after", name.getName(), Arrays.asList(shixin.name)); Field habit = cls.getDeclaredField("habit"); System.out.format(fmt, "before", habit.getName(), shixin.habit); habit.set(shixin, MyHabit.LOL); System.out.format(fmt, "after", habit.getName(), shixin.habit); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } }
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2425
26
27
28
29
30
3132
33
34
35
36
37
38
运行结果:
before: idCarNumber = 1000000000
after: idCarNumber = 123456
before: name = [shi, xin]
after: name = [hei, hei]
before: habit = CODE
after: habit = LOL
Process finished with exit code 0
1 2 3 4 5 6 7 8
常见错误 1 :无法转换类型导致的 java.lang.IllegalArgumentException
下面的代码和上个例子的代码很相似,但是运行时却报错:
public class FieldTrouble extends BaseTestClass {
public Integer value;
public static void main(String[] args) { FieldTrouble fieldTrouble = new FieldTrouble(); Class<? extends FieldTrouble> cls = fieldTrouble.getClass(); try { Field value = cls.getField("value"); value.setInt(fieldTrouble, 23); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } }
}
1 2 3 4 5 6 7 89
10
1112
13
14
15
16
17
Exception in thread “main” java.lang.IllegalArgumentException: Can not set java.lang.Integer field net.sxkeji.shixinandroiddemo2.test.reflection.FieldTrouble.value to (int)23
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:191)
at sun.reflect.UnsafeObjectFieldAccessorImpl.setInt(UnsafeObjectFieldAccessorImpl.java:114)
at java.lang.reflect.Field.setInt(Field.java:949)
at net.sxkeji.shixinandroiddemo2.test.reflection.FieldTrouble.main(FieldTrouble.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
1 2 3 4 5 6 7 89
10
11
为什么我们无法给 Integer 类型的 value 使用 setInt() 方法重新设值呢?
这是因为在使用反射获取或者修改一个变量的值时,编译器不会进行自动装/拆箱。
因此我们无法给一个 Integer 类型的变量赋整型值,必须给它赋一个 Integer 对象才可以。
使用 Field.set(Object obj, Object value) 方法解决这个问题:
f.set(ft, new Integer(43));
1
常见错误 2:反射非 public 的变量导致的 NoSuchFieldException
如果你使用 Class.getField() 或者 Class.getFields() 获取非 public 的变量,编译器会报 java.lang.NoSuchFieldException 错。
下面这张图说明了获取变量的四个方法所左右的对象类型:
shixinzhang
常见错误 3 :修改 final类型的变量导致的 IllegalAccessException
当你想要获取或者修改 不可修改(final)的变量时,会导致IllegalAccessException。
举个例子:
public class FieldTrouble extends BaseTestClass {
public Integer value;
public final boolean wannaPlayGame = true;
public static void main(String[] args) { FieldTrouble fieldTrouble = new FieldTrouble(); Class<? extends FieldTrouble> cls = fieldTrouble.getClass(); try {
// Field value = cls.getField(“value”);
// value.setInt(fieldTrouble, 23);
// value.set(fieldTrouble, new Integer(23));
Field wannaPlayGame = cls.getDeclaredField("wannaPlayGame");
// wannaPlayGame.setAccessible(true); //加上这句就没问题
wannaPlayGame.setBoolean(fieldTrouble, false);
System.out.format(“Field:%s %s\n”, wannaPlayGame.getName(),fieldTrouble.wannaPlayGame);
} catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } }
}“`
上面的代码想要通过反射修改 wannaPlayGame 变量,运行后会报错:
“`java
java.lang.IllegalAccessException: Can not set final boolean field net.sxkeji.shixinandroiddemo2.test.reflection.FieldTrouble.wannaPlayGame to (boolean)false
at sun.reflect.UnsafeFieldAccessorImpl.throwFinalFieldIllegalAccessException(UnsafeFieldAccessorImpl.java:76)
at sun.reflect.UnsafeFieldAccessorImpl.throwFinalFieldIllegalAccessException(UnsafeFieldAccessorImpl.java:84)
at sun.reflect.UnsafeQualifiedBooleanFieldAccessorImpl.setBoolean(UnsafeQualifiedBooleanFieldAccessorImpl.java:96)
at java.lang.reflect.Field.setBoolean(Field.java:801)
at net.sxkeji.shixinandroiddemo2.test.reflection.FieldTrouble.main(FieldTrouble.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
相关文章推荐
- 深入理解 Java 反射:Field (成员变量)
- 深入理解 Java 反射:Field (成员变量)
- JAVA反射(2) - Field成员变量 学习笔记
- Java Reflection深入理解变量(Field)
- 深入理解 Java 反射:Method (成员方法)
- Java反射06 : 成员变量Field学习示例
- 对Java方法内部使用this访问成员变量的深入理解
- 深入理解 Java 反射:Method (成员方法)
- 深入理解 Java 反射:Method (成员方法)
- 黑马程序员--Java基础加强--16.利用反射操作泛型V【通过Constructor反射解析泛型构造方法】【通过Field反射解析泛型成员变量】【个人总结】
- 深入理解Java:类加载机制及反射
- 使用java反射操作类的构造函数,成员变量和成员方法
- 深入理解Java中为什么内部类可以访问外部类的成员
- Java深入(高新技术)(四):反射、Class、Constructor、Method、Field、数组的反射、JavaBean
- 深入理解Java:类加载机制及反射
- java 反射,访问成员变量
- 深入理解 Java final 变量的内存模型
- 18 理解反射的概念19构造方法的反射应用20成员变量的反射 21成员变量反射的综合案例22成员方法的反射
- Java通过反射访问私有成员变量和私有方法
- 类的成员变量通过Field.set方法提示java.lang.NullPointerException