简体   繁体   中英

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

有没有一种简单的方法来指定字段在Java中是否具有反射性,该方法类似于使用setAccessible()设置字段的可访问性标志?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM