2

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?

2
  • Isn't the purpose of getters and setters to encapsulate functionality such as this? Commented Nov 16, 2010 at 12:39
  • Yes, but I want to avoid the hand-coding of functionality that will need to be repeated for each field Commented Nov 16, 2010 at 12:46

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.