可以使用反射获得类中的瞬态场 [英] Can a transient field in a class be obtained using reflection

查看:164
本文介绍了可以使用反射获得类中的瞬态场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用反射获取类中的 transient 字段吗? (使用 getDeclaredField(..)

Can a transient field in a class be obtained using reflection? (using getDeclaredField(..))

推荐答案

,这是一个普通的领域。您可以通过以下方式检查它是否是瞬态的:

Yes, It is a normal field. You can check whether it is transient by:

Modifier.isTransient(field.getModifiers());




transient
A Java编程语言中的关键字,表示字段不是对象序列化形式的一部分。当对象被序列化时,其瞬态字段的值不包括在串行表示中,而其非瞬态字段的值包括在内。

transient: A keyword in the Java programming language that indicates that a field is not part of the serialized form of an object. When an object is serialized, the values of its transient fields are not included in the serial representation, while the values of its non-transient fields are included.

所以没有合理的理由让它不能被反射访问。它是被忽略的字段的值(有时),而不是字段本身。

So no logical reason for it not to be accessible by reflection. It's the value of the field that is ignored (sometimes), not the field itself.

(顺便说一句,是什么阻碍了你只是试图调用 getDeclaredField(yourTransientField)?)

(btw, what hindered you from just trying to call getDeclaredField("yourTransientField")?)

这篇关于可以使用反射获得类中的瞬态场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆