Search code examples
javaclassloaderdecompilingjavassist

Obtaining source code of dynamically generated java classes


I am dynamically generating new classes (using Javassist) and I would like to be able to obtain the source code of such generated classes.

Is there a library for programmatically decompiling byte code?. Note that the byte code I want to decompile does not exist in the file system, only in memory.


Solution

  • If you want to decompile (i.e. get the original source), save the CtClass into a class file on disk, and then use a decompiler such as JD-GUI.

    ClassFile cf = someCtClass.getClassFile();
    FileOutputStream os = new FileOutputStream("C:/somefilename.class");
    cf.write(os);
    os.close();
    

    Than just open up C:/somefilename.class in the decompiler.