I have a quite large codebase. In many places I have a piece of code like this:
for (MyObjectType myobj : myList) {
//...do something with myobj
}
MyObjectType is the basic object in my application, and I iterate over many of them very often. I have some log4j set up so that I log info about the MyObjectType instance I am dealing with:
for (MyObjectType myobj : myList) {
MDC.put("myobj", myobj.identify());
...
logger.error("this message contains info about myobj")
}
This is really helpful. Unfortunately there are tons of for looks like this where I forgot to use the mdc.put(). So I was thinking...would be some way to instrument the code so that:
- I detect there is a for looping over a collection of MyObjectType
- I insert this as first instruction inside the loop:
MDC.put("myobj", myobj.identify());
If there is a way (using aop, instrumentation, some java agent?), how difficult would it be (maybe it's not worth the effort).
I have never used any java bytecode library, just spring aop lightly.