6

I am trying to delete a method from a class file using Javassist.

Target class:"RemoveMethod".

Target method:"DoubleCheck".

My codes:

package javassist;     
        import java.io.IOException;
        import java.lang.reflect.Method;
        import javassist.*;

public class cRepair {
    public static void main(String[] args) throws NotFoundException, IOException, CannotCompileException{
    ClassPool pool = ClassPool.getDefault();  
    CtClass ctClass = pool.get("javassist.RemoveMethod");  
    CtMethod ctm = ctClass.getDeclaredMethod("DoubleCheck");  
    ctClass.removeMethod(ctm);
    ctClass.writeFile("C:/Users/workspace/Javaproject1/src/javassis"); 
 }
}

Then,run the code using the file "javassist.jar":

javac -cp javassist.jar cRepair.java

Then check the target class:

javap -verbose RemoveMethod.class

The method "DoubleCheck" is still there!

This looks really odd. Why could this happen and how to fix it?

Delibz
  • 99
  • 2
  • 9
  • the javac call just compiles your code, it doesn't run it. – muued May 17 '15 at 10:08
  • @muued, Thanks man. It does make sense! I tried to run the code using: "java cRepair", but the system says "could not find or load the main class cRepair" – Delibz May 17 '15 at 10:57
  • run `java -cp javassist.jar javassist.cRepair` – muued May 17 '15 at 11:04
  • @muued, thanks for your reply. However, the system still says "could not find or load the main class cRepair" Really odd. – Delibz May 17 '15 at 11:06
  • just learn how to run a compiled java class, thats completely unrelated – muued May 17 '15 at 11:09
  • @muued. I mean...after compiling, I used the command 'java -cp javassist.jar javassist.cRepair' as you mentioned previously to run the code, but the main class cRepair cannot be found or loaded... – Delibz May 17 '15 at 11:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/78013/discussion-between-muued-and-delibz). – muued May 17 '15 at 11:20

1 Answers1

3

Your code reads the bytecode of your class into memory and removes the method. But it does not write the modified bytecode back to a .class file. You can call CtClass#writeFile() to do that.

Stefan Ferstl
  • 5,135
  • 3
  • 33
  • 41
  • Thank you so much for your commend. I just modified my code(as shown) and retried it again, but the new class "ModifiedMethod" was not generated...... – Delibz May 17 '15 at 08:37
  • The parameter in `writeFile()` defines the *directory* where your class will be written. So after running your code you should have a directory called `ModifiedMethod`which contains the modified `RemoveMethod` class. I ran your example code and it worked. Javadoc of CtClass: http://www.csg.ci.i.u-tokyo.ac.jp/~chiba/javassist/html/javassist/CtClass.html#writeFile-- – Stefan Ferstl May 17 '15 at 08:56
  • Sorry to put you out but I am still confused...I set up the directory and retried my code....the expected class file was generated on my desk after running the code....Thank you for your patience once more... – Delibz May 17 '15 at 09:25
  • @Delibz So, problem solved, right? I guess you want to replace your original `RemoveMethod.class` file with the modified one. In that case you should tell the `writeFile()` method to write the new `.class` file to the location where the original file resides. – Stefan Ferstl May 17 '15 at 09:38
  • Could you please let me know how did you run the code? I just tried: "java cRepair", but error occured: "Could not find or load the main class cRepair" – Delibz May 17 '15 at 10:59