Friday, 1 May 2020

Javassist: Get CtClass object from byte array

Step 1: Define byte array.
byte[] byteArray = { -54, -2, -70, -66, 0, 0, 0, 52, 0, 12, 1, 0, 26, 99, 111, 109, 47, 115, 97, 109, 112, 108,
                  101, 47, 97, 112, 112, 47, 109, 111, 100, 101, 108, 47, 80, 111, 105, 110, 116, 7, 0, 1, 1, 0, 16, 106,
                  97, 118, 97, 47, 108, 97, 110, 103, 47, 79, 98, 106, 101, 99, 116, 7, 0, 3, 1, 0, 10, 83, 111, 117, 114,
                  99, 101, 70, 105, 108, 101, 1, 0, 10, 80, 111, 105, 110, 116, 46, 106, 97, 118, 97, 1, 0, 6, 60, 105,
                  110, 105, 116, 62, 1, 0, 3, 40, 41, 86, 12, 0, 7, 0, 8, 10, 0, 4, 0, 9, 1, 0, 4, 67, 111, 100, 101, 0,
                  33, 0, 2, 0, 4, 0, 0, 0, 0, 0, 1, 0, 1, 0, 7, 0, 8, 0, 1, 0, 11, 0, 0, 0, 17, 0, 1, 0, 1, 0, 0, 0, 5,
                  42, -73, 0, 10, -79, 0, 0, 0, 0, 0, 1, 0, 5, 0, 0, 0, 2, 0, 6 };

Step 2: Define class name
String className = "com.sample.app.model.Point";

Step 3: Insert a ClassPath object at the head of the search path.
ClassPool pool = ClassPool.getDefault();
pool.insertClassPath(new ByteArrayClassPath(className, byteArray));

Step 4: Get the CtClass object from className.
CtClass cc = pool.get(className);

Find the below working application.

App.java
package com.sample.app;

import javassist.ByteArrayClassPath;
import javassist.ClassPool;
import javassist.CtClass;

public class App {

 public static void main(String args[]) throws Exception {

  String className = "com.sample.app.model.Point";
  byte[] byteArray = { -54, -2, -70, -66, 0, 0, 0, 52, 0, 12, 1, 0, 26, 99, 111, 109, 47, 115, 97, 109, 112, 108,
    101, 47, 97, 112, 112, 47, 109, 111, 100, 101, 108, 47, 80, 111, 105, 110, 116, 7, 0, 1, 1, 0, 16, 106,
    97, 118, 97, 47, 108, 97, 110, 103, 47, 79, 98, 106, 101, 99, 116, 7, 0, 3, 1, 0, 10, 83, 111, 117, 114,
    99, 101, 70, 105, 108, 101, 1, 0, 10, 80, 111, 105, 110, 116, 46, 106, 97, 118, 97, 1, 0, 6, 60, 105,
    110, 105, 116, 62, 1, 0, 3, 40, 41, 86, 12, 0, 7, 0, 8, 10, 0, 4, 0, 9, 1, 0, 4, 67, 111, 100, 101, 0,
    33, 0, 2, 0, 4, 0, 0, 0, 0, 0, 1, 0, 1, 0, 7, 0, 8, 0, 1, 0, 11, 0, 0, 0, 17, 0, 1, 0, 1, 0, 0, 0, 5,
    42, -73, 0, 10, -79, 0, 0, 0, 0, 0, 1, 0, 5, 0, 0, 0, 2, 0, 6 };

  ClassPool pool = ClassPool.getDefault();
  pool.insertClassPath(new ByteArrayClassPath(className, byteArray));
  CtClass cc = pool.get(className);

  System.out.println(cc.getClass());

  cc.writeFile("/Users/Shared/assistDemos");

 }
}

Run App.java, you can see below message in console.

class javassist.CtClassType

You can observe Point.class file is generated at folder /Users/Shared/assistDemos.
$tree /Users/Shared/assistDemos
/Users/Shared/assistDemos
└── com
    └── sample
        └── app
            └── model
                └── Point.class

4 directories, 1 file

Open Point.class file from decompiler to see the soure code.


Previous                                                    Next                                                    Home

No comments:

Post a Comment