4 Replies Latest reply on Aug 15, 2003 12:49 PM by tealeaf

    can javassist modify class in memory?

    tealeaf

      Is it possible to use Javassist to modify a class on the fly? If yes, is there some example code I can look at?

      It looks to me that it reads and writes .class files to and from disk. So in order to see the effects of the changes class you have to restart JVM or at least manually reload the class? Am I missing something?

      Thanks,
      -Leo

        • 1. Re: can javassist modify class in memory?
          chiba

          You have to load a modified class if you want
          to see the effects. There is no way to directly
          modify a class in the JVM memory.

          • 2. Re: can javassist modify class in memory?
            tealeaf

            Thank you for your reply! Is the same true with BCEL library as well? Is that a fundamental JVM restriction?

            So, theoretically speaking, if the app runs from a CD-ROM or some other read-only store, you'd have to save the class to an in-memory ByteArrayOutputStream and load it? I don't know much about this. Is that something that's done all the time to achieve on the fly modification?

            • 3. Re: can javassist modify class in memory?
              hlship

              Tapestry does a lot of Javassist-based class enhancement. It leaves the original class alone and creates a subclass. Often the starting class is abstract, and Tapestry uses Javassist to fill in the abstract methods.

              Javassist allows you to create and configure a CtClass instance and then use that to create a java.lang.Class you can instantiate.


              If you want to do something more aspect-like, you can do a lot of juggling of class loaders to ensure that Javassist has a chance to enhance the bytecodes before they are converted into a java.lang.Class; I believe the tutorial discusses how to fit Javassist into the pipeline (but this wasn't something I needed for my work, so I haven't investigated it).

              • 4. Re: can javassist modify class in memory?
                tealeaf

                Ah, I think I understand. Thank you for your reply!
                -Leo