2

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.

  1. java.lang.instrument.ClassFileTransformer does not get "array classes", so no way to hook into "constructor of array". And "array classes are never modifiable"
  2. no JVMTI event corresponds to this

Any suggestions?

Nikem
  • 5,716
  • 3
  • 32
  • 59
  • There most likely is a better way to achieve your desired result than capturing when every array is created. What are you trying to do? – jzd Jan 04 '11 at 12:43

1 Answers1

3

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:

  1. Create a JVMTI agent which intercepts the classes you're interested in.
  2. Pass the classes you want to instrument to an ASM class transformer.
  3. In the class transformer, you can intercept the Java opcodes related to constructing an array, e.g. ANEWARRAY (see the JVM spec for more).
axw
  • 6,908
  • 1
  • 24
  • 14
  • I want to capture every arrays creation. Are you suggesting then capturing every method of every class in order to find those anewarray instructions? – Nikem Jan 04 '11 at 13:40
  • @Nikem: You modify the method definition, you're not intercepting the method calls at runtime. There is no additional runtime overhead other than the code you add to handle array creation. – axw Jan 04 '11 at 13:51