可以通过java中的反射设置字段的瞬态属性/标志吗? [英] Can a field's transient property/flag be set through reflection in java?

查看:62
本文介绍了可以通过java中的反射设置字段的瞬态属性/标志吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以通过反射在 Java 中指定一个字段是否应该是瞬态的,类似于如何使用 setAccessible() 设置字段的可访问性标志?

Is there a simple way to specify if a field should be transient or not in Java with reflection, similar to how a field's accessibility flag can be set with setAccessible()?

推荐答案

反射本身不能改变代码.

Reflection itself cannot alter code.

Java 代理应该允许您在加载类时对其进行重写.

Java Agents should allow you to rewrite the class as it is loaded.

在序列化机制缓存类数据之前,如果 serialPersistentFields 存在(不太可能),您可以使用反射来更改它.您可以以高度特定于版本的方式使用反射来更改序列化机制内的数据.

You can use reflection to alter serialPersistentFields if it exists (unlikely), before the serialisation mechanism caches the class data. You could use reflection in a highly version specific way to alter the data inside the serialisation mechanism.

这篇关于可以通过java中的反射设置字段的瞬态属性/标志吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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