Can a field's transient property/flag be set through reflection in java?

元气小坏坏 提交于 2019-12-24 10:48:16

问题


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()?


回答1:


Reflection itself cannot alter code.

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

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.




回答2:


I cannot see any direct way to do this in the Java API, but it is possible with something like JRebel or LiveRebel http://www.zeroturnaround.com/jrebel/comparison/. You could use something like ASM from http://asm.ow2.org/ to rewrite the bytecode with transient modifiers on the relevant fields. Then trigger JRebel/LiveRebel to replace the runtime version of the class.

I haven't tried automating this with ASM, but I have used JRebel with IntelliJ IDEA to perform changes like this successfully during interactive development.



来源:https://stackoverflow.com/questions/3384441/can-a-fields-transient-property-flag-be-set-through-reflection-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!