7 Replies Latest reply on Jan 18, 2005 3:28 AM by sirrurg

    Adding Methodes

    sirrurg

      Hi everyone!

      I have a question about bytecode manipulation with Javassist.
      Is it possible to modify more than one class a time and
      compile them together?!?
      I think of doing sth like this:

      class A{
       void dosth(){
       ...
       B.dosthmore();
       ....
       }
      }
      class B{
       void dosthmore(){...}
      }
      

      And i want to change it in sth like this ..
      class A{
       void dosth$impl(){
       ...
       B.dosthmore$impl();
       ...
       }
       void dosth(){
       ...
       dosth$impl();
       ....
       }
      }
      class B{
       void dosthmore$impl(){...}
       void dosthmore(){...}
      }
      


      As i saw in the tutorial it is easy to modify one class a time,
      is this also true for more than 1?!? Or do i have to
      split this up into 2 "Loops" ?

      Sincerely
      Sirrurg

        • 1. Re: Adding Methodes
          chiba

          Yes, but you must first add dosth$impl() as an abstract
          method so that the compiler can resolve that method name.
          You can later give a method body to dosth$imple().

          • 2. Re: Adding Methodes
            sirrurg

            ok ... thx!
            So if i understand this correctly, to add the "cross"-call to B.dosthmore(), this class must also already contain a method called dosthmore$impl(),
            before i can add the new method-body to A.dosth$impl().

            As consequence i must first add all new methods as "abstract"
            to all classes which should be changed and then in an 2nd step add
            the implementation of this methodes, like in the following code?

            ...
            foreach(CtClass cls in classes){
             cls.stopPruning(true);
             addAbstractMethodes(cls);
             cls.writeFile();
            }
            foreach(CtClass cls in classes){
             addImplOfMethodes(cls);
             // here probably cls.stopPruning(false);
             cls.writeFile();
            }
            // everythings done hopefully
            


            • 3. Re: Adding Methodes
              chiba

              Exactly. I could not come up with a better idea than this.
              If you have one, please let me know. Thanks. :-)

              • 4. Re: Adding Methodes
                sirrurg

                OK... thx very much, i ll try this out.

                • 5. Re: Adding Methodes
                  sirrurg

                  Hi,

                  i ll tried this 2 step method out and it works.
                  And by loading all classes at the beginning of the processing,
                  i dont have to use stopPruning.

                  • 6. Re: Adding Methodes
                    chiba

                    Ah, pruning is now off by default.

                    • 7. Re: Adding Methodes
                      sirrurg

                      OK ... thx, good to know ;)