3 Replies Latest reply on Aug 25, 2005 12:46 AM by chiba

    instrumenting a loaded class

    nick.collier

      Hi,

      I'm using a javaassist in a mechanism for creating dyanamic listeners. The idea is that the user can specify a class and field to watch for field access and then an instance and method to call on that instance when the field is accessed. As for implementation, I replace the field access with the access ($proceed($$)) and a method that notifies the listener.

      This works well when I specify the listener and what to watch before the class I want to watch is loaded. The onLoad method of my translator adds the code into the class I want to watch and everything works OK.

      However, if I want to choose what to watch after the class I want to watch is loaded, that is, after onLoad has been called for that class, this doesn't seem to work. What I'm doing in that case, is just getting the appropriate CtClass from the ClassPool, defrosting() it and then replacing the field access. This change doesn't seem to be picked up by the JVM though as the class behaves normally when the field is accessed.

      Any suggestions? I'm hoping this is possible without unloading the class from the JVM but I'm beginning to wonder.

      thanks,

      Nick

        • 1. Re: instrumenting a loaded class
          chiba

          For instrumenting a loaded class, you must use HotSwap
          provided by Sun's JVM. An upcoming new version of
          Javassist provides a utility class for using HotSwap.
          So if you are intrested, please down load the CVS HEAD
          and test it.

          • 2. Re: instrumenting a loaded class
            nick.collier

            chiba,

            Thanks very much!

            I've gotten the code from CVS and taken a look at the HotSwapper. Do you have any sense of what sort of overhead running the virtual machine in this kind of debug mode adds?

            thanks,

            Nick

            • 3. Re: instrumenting a loaded class
              chiba

              According to our experiment with HotSwap of JDK1.4
              (JDK1.4 has HotSwap although it has a different name),
              the overheaeds is less than 5% (normally about 2%).
              We used SPECjvm98, which is a set of relatively small
              programs. So if you run a really big program, the amount
              of the overhead may change. I expect the overhead
              will be still negligible, though.