概述
java.lang.reflect.Field提供类或接口的单个字段的信息和动态访问。反射字段可以是类(静态)字段或实例字段。Field类提供了获取和设置某个类的字段的属性值。
在java.lang.Class获取Field对象的方法总共有四个:
1. getDeclaredField(String name);
返回一个 Field 对象,name参数表示字段的名称(包括私有的)。
2. getDeclaredFields();
返回 Field 对象的一个数组,表示的类或接口所声明的所有字段(包括私有字段)。
3. getField(String name);
返回一个 Field 对象,表示的类或接口的指定公共成员字段,参数name为字段名称。
4. getFields();
返回一个包含某些 Field 对象的数组,表示的类或接口的所有可访问公共字段。
类名
package java.lang.reflect;
public final
class Field extends AccessibleObject implements Member
变量(部分)
Field类的作用是获取和设置成员变量的属性值,所以Field类提供了一系列的get和set方法。
public static void main(String[] args) throws Exception {
Student student = Student.class.newInstance();
Field name = Student.class.getDeclaredField("name");
//private属性,设置可访问
name.setAccessible(true);
name.set(student, "jdk");
Object o = name.get(student);
System.out.println(o);
}
get(Object obj)
@CallerSensitive
public Object get(Object obj)
throws IllegalArgumentException, IllegalAccessException
{
if (!override) {
if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
Class<?> caller = Reflection.getCallerClass();
checkAccess(caller, clazz, obj, modifiers);
}
}
return getFieldAccessor(obj).get(obj);
}
第一步和Method类一样,先检查访问权限,getFieldAccessor也和Method类中的MethodAccessor一样,先获取字段的访问器FieldAccessor。
private FieldAccessor getFieldAccessor(Object obj)
throws IllegalAccessException
{
boolean ov = override;
FieldAccessor a = (ov) ? overrideFieldAccessor : fieldAccessor;
return (a != null) ? a : acquireFieldAccessor(ov);
}
//
private FieldAccessor acquireFieldAccessor(boolean overrideFinalCheck) {
// 先检查是否已经创建
FieldAccessor tmp = null;
if (root != null) tmp = root.getFieldAccessor(overrideFinalCheck);
if (tmp != null) {
if (overrideFinalCheck)
overrideFieldAccessor = tmp;
else
fieldAccessor = tmp;
} else {
// 创建一个并赋值给root
tmp = reflectionFactory.newFieldAccessor(this, overrideFinalCheck);
setFieldAccessor(tmp, overrideFinalCheck);
}
return tmp;
}
在newFieldAccessor里根据字段的修饰符和类型生成相对应的FieldAccessor,从而调用各自的get和set方法。
static FieldAccessor newFieldAccessor(Field var0, boolean var1) {
Class var2 = var0.getType();
boolean var3 = Modifier.isStatic(var0.getModifiers());
boolean var4 = Modifier.isFinal(var0.getModifiers());
boolean var5 = Modifier.isVolatile(var0.getModifiers());
boolean var6 = var4 || var5;
boolean var7 = var4 && (var3 || !var1);
if (var3) {
UnsafeFieldAccessorImpl.unsafe.ensureClassInitialized(var0.getDeclaringClass());
if (!var6) {
if (var2 == Boolean.TYPE) {
return new UnsafeStaticBooleanFieldAccessorImpl(var0);
} else if (var2 == Byte.TYPE) {
return new UnsafeStaticByteFieldAccessorImpl(var0);
} else if (var2 == Short.TYPE) {
return new UnsafeStaticShortFieldAccessorImpl(var0);
} else if (var2 == Character.TYPE) {
return new UnsafeStaticCharacterFieldAccessorImpl(var0);
} else if (var2 == Integer.TYPE) {
return new UnsafeStaticIntegerFieldAccessorImpl(var0);
} else if (var2 == Long.TYPE) {
return new UnsafeStaticLongFieldAccessorImpl(var0);
} else if (var2 == Float.TYPE) {
return new UnsafeStaticFloatFieldAccessorImpl(var0);
} else {
return (FieldAccessor)(var2 == Double.TYPE ? new UnsafeStaticDoubleFieldAccessorImpl(var0) : new UnsafeStaticObjectFieldAccessorImpl(var0));
}
} else if (var2 == Boolean.TYPE) {
return new UnsafeQualifiedStaticBooleanFieldAccessorImpl(var0, var7);
} else if (var2 == Byte.TYPE) {
return new UnsafeQualifiedStaticByteFieldAccessorImpl(var0, var7);
} else if (var2 == Short.TYPE) {
return new UnsafeQualifiedStaticShortFieldAccessorImpl(var0, var7);
} else if (var2 == Character.TYPE) {
return new UnsafeQualifiedStaticCharacterFieldAccessorImpl(var0, var7);
} else if (var2 == Integer.TYPE) {
return new UnsafeQualifiedStaticIntegerFieldAccessorImpl(var0, var7);
} else if (var2 == Long.TYPE) {
return new UnsafeQualifiedStaticLongFieldAccessorImpl(var0, var7);
} else if (var2 == Float.TYPE) {
return new UnsafeQualifiedStaticFloatFieldAccessorImpl(var0, var7);
} else {
return (FieldAccessor)(var2 == Double.TYPE ? new UnsafeQualifiedStaticDoubleFieldAccessorImpl(var0, var7) : new UnsafeQualifiedStaticObjectFieldAccessorImpl(var0, var7));
}
} else if (!var6) {
if (var2 == Boolean.TYPE) {
return new UnsafeBooleanFieldAccessorImpl(var0);
} else if (var2 == Byte.TYPE) {
return new UnsafeByteFieldAccessorImpl(var0);
} else if (var2 == Short.TYPE) {
return new UnsafeShortFieldAccessorImpl(var0);
} else if (var2 == Character.TYPE) {
return new UnsafeCharacterFieldAccessorImpl(var0);
} else if (var2 == Integer.TYPE) {
return new UnsafeIntegerFieldAccessorImpl(var0);
} else if (var2 == Long.TYPE) {
return new UnsafeLongFieldAccessorImpl(var0);
} else if (var2 == Float.TYPE) {
return new UnsafeFloatFieldAccessorImpl(var0);
} else {
return (FieldAccessor)(var2 == Double.TYPE ? new UnsafeDoubleFieldAccessorImpl(var0) : new UnsafeObjectFieldAccessorImpl(var0));
}
} else if (var2 == Boolean.TYPE) {
return new UnsafeQualifiedBooleanFieldAccessorImpl(var0, var7);
} else if (var2 == Byte.TYPE) {
return new UnsafeQualifiedByteFieldAccessorImpl(var0, var7);
} else if (var2 == Short.TYPE) {
return new UnsafeQualifiedShortFieldAccessorImpl(var0, var7);
} else if (var2 == Character.TYPE) {
return new UnsafeQualifiedCharacterFieldAccessorImpl(var0, var7);
} else if (var2 == Integer.TYPE) {
return new UnsafeQualifiedIntegerFieldAccessorImpl(var0, var7);
} else if (var2 == Long.TYPE) {
return new UnsafeQualifiedLongFieldAccessorImpl(var0, var7);
} else if (var2 == Float.TYPE) {
return new UnsafeQualifiedFloatFieldAccessorImpl(var0, var7);
} else {
return (FieldAccessor)(var2 == Double.TYPE ? new UnsafeQualifiedDoubleFieldAccessorImpl(var0, var7) : new UnsafeQualifiedObjectFieldAccessorImpl(var0, var7));
}
}
get和set方法最终都是调用底层native方法getXXX和putXXX方法获取和设置字段值。
更多精彩内容请关注微信公众号:
最后
以上就是虚幻嚓茶为你收集整理的JDK源码(十六):Field的全部内容,希望文章能够帮你解决JDK源码(十六):Field所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复