1 Reply Latest reply on Dec 15, 2012 10:42 AM by doctorx

    Can I delete the modifier of a method with Javassist?

    scalable

      Hello, everyone.

       

      I have a class file.

       

      public class ClsSync {

           

           public synchronized void biat(){

                

           }

       

      }

       

      I want to change it to

       

      public  void biat(){   // delete the synchronized keyword

                

      }

       

      Can I implement it using Javassist?

       

      Any suggestions are welcome!

       

      Thanks

       

       

        • 1. Re: Can I delete the modifier of a method with Javassist?
          doctorx

          I would have done something like this:

           

           

          ClassPool pool = ClassPool.getDefault();
          CtClass cc = pool.get("ClsSync");
          
          
          CtMethod m = cc.getDeclaredMethod("biat");
          int oldMod = m.getModifiers();
          int mod = Modifier.clear(oldMod, Modifier.SYNCHRONIZED);
          m.setModifiers(mod);
          
          
          Class newClass = cc.toClass(); // or cc.writeFile() if you wish to keep the changes