Saturday 2 May 2020

Javassist: Get CtClass object from InputStream

Step 1: Create InputStream to the class file
String classFilePath = "....";
File classFile = new File(classFilePath);
InputStream inputStream = new FileInputStream(classFile);

Step 2: Get CtClass instance from input stream
ClassPool classPool = ClassPool.getDefault();
CtClass ctClass = classPool.makeClass(inputStream);

App.java
package com.sample.app;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import javassist.ClassPool;
import javassist.CtClass;

public class App {

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

  String classFilePath = "...."; // Add class file path here
  File classFile = new File(classFilePath);
  InputStream inputStream = new FileInputStream(classFile);

  ClassPool classPool = ClassPool.getDefault();
  CtClass ctClass = classPool.makeClass(inputStream);

  System.out.println(ctClass.getName());

 }
}


Previous                                                    Next                                                    Home

No comments:

Post a Comment