Question

Is it possible to add "hooks" to a class via bytecode enhancement that execute code whenever a class 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?

Was it helpful?

Solution

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

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top