开发者

Bytecode enhancement for fields in a class

Is it possible to add "hooks" to a class via bytecode enhancement that execute code whenever a c开发者_开发技巧lass field is read or written? For example, I'd like to automatically set a "dirty" flag whenever a new value is assigned to a field.

If so, which libraries are best suited to implement this functionality?


I'd suggest you to read about AspectJ. Probably this is the tool you are looking for.


Here is how to generate getters and setters using the ASM framework. That should get you started.

  • http://asm.ow2.org/doc/faq.html#Q9

You can even let your bytecode-rewriter hook into the class-loader and do the rewriting on the fly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜