Pages

Wednesday, August 8, 2012

Easily Modify Java Class Files With Javassist



Javassist is a free source java library that can be used to edit java class file and can make almost any kind of change. The library include two levels of modification. One is high level and other is lower level called byte level. While editing class files in byte level requires lot of skill, the high level modification can be done just like making a simple program. Here is a simple hello world program in java that we will modify using javassist:

public class TestClass
{
  public static void main(String a[])
  {
    System.out.println("Hello World");
  }
}

After compiling , TestClass.class file will be generated and it will display "Hello World" in the console when executed. Now I will show you how to add a new line of code in this class file using simple high level coding.
Suppose I save this class file in E:/myfolder.

Since the executable code is stored in the main method of this class, we will modify this method and add a new line after the "System.out.println("Hello World");" of original method.

I make a new class file Modifier that will contain the modify the code of this exiting class file upon execution.


STEP 1 
Make a new class file that contains main() method


public class Modifier
{
  public static void main(String a[])
  {
    
  }
}


Now rest of coding will be done inside main() method

STEP 2
Create a pool object that will give a link to the class file that we are modifying

ClassPool pool=ClassPool.getDefault(); // Writing this is compulsory to initialize pool.inserClassPath("E"//myfolder"); //Setting the location to search the class file

STEP 3
Now we will select the class file named as "TestClass" as the file to edit. For this, we will create a CtClass      object. Once this Object is created and is linked to a class file, then all the properties of that class file, like constant pool, methods, fields etc. can be accessed from this object. Here is the code for this :

CtClass cc=pool.get("TestClass") ; // Selecting a class and storing its property in object

STEP 4
Now we will select the method of the class to be edited, which is "main" here, and store this method in CtMethod object. After this, we can manipulate the code of main method using the CtMethod Object

CtMethod m=cc.getDeclaredMethod("main"); // Selecting the "main" method

STEP 5
Now we will add the line "System.out.println("This is extra added text");" after the hello message in the method. The code will be :

m.insertAtEnd("System.out.println("("This is extra added text");"); // Adding new line

cc.writeFile(); // Write the edited file to disk

Complete Code of Modifier class will look like this:


public class Modifier
{
  public static void main(String a[])
  {
      ClassPool pool=ClassPool.getDefault();    
      pool.inserClassPath("E"//myfolder");   
      CtClass cc=pool.get("TestClass") ; 
      CtMethod m=cc.getDeclaredMethod("main");    
      m.insertAtEnd("System.out.println("("This is extra added text");");
      cc.writeFile(); 

  }
}


After you run the program, a new TestClass.class file will be created with following code if viewed from a decompiler :    


public class TestClass
{
  public static void main(String a[])
  {
    System.out.println("Hello World");
    System.out.println("("This is extra added text"); // New Line Added
  }
}

There are lot of things you can do with this library. Here is the link of official sourceforge project. It also imcludes users manual.

Download Official Project Library From SourceForge