Java agent, instrumentation and array creation
-
15-10-2019 - |
Question
I need to write an agent for my java application, that does some specific stuff on every array creation. So far I was unable to find any way to run my code on this event.
- java.lang.instrument.ClassFileTransformer does not get "array classes", so no way to hook into "constructor of array". And "array classes are never modifiable"
- no JVMTI event corresponds to this
Any suggestions?
Solution
You'll need to modify the byte-code of your application to do that. I've found ObjectWeb ASM to be the best tool for the job. The general idea is to:
- Create a JVMTI agent which intercepts the classes you're interested in.
- Pass the classes you want to instrument to an ASM class transformer.
- In the class transformer, you can intercept the Java opcodes related to constructing an array, e.g. ANEWARRAY (see the JVM spec for more).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow