Search code examples
javasoot

Output class file for a class object created in Soot


For a test generation task, I need to generate .class files programmatically. I'm using the Soot framework to generate the code. Let's say I've created a class through Scene.v().makeSootClass("Test") and added some methods to it. How can I now compile that virtual Java representation of the Soot IR structure down to an actual .class file?

The docs list some output options for their command line interface, but I'd like to do this programmatically through an API in Java, and I cannot find instructions on how to proceed.


Solution

  • After a number of searches, I've found the appropriate documentation explaining to do this. The relevant code snippet from those docs is this one:

    String fileName = SourceLocator.v().getFileNameFor(sClass, Options.output_format_class);
    OutputStream streamOut = new JasminOutputStream(new FileOutputStream(fileName));
    PrintWriter writerOut = new PrintWriter(new OutputStreamWriter(streamOut));
    
    JasminClass jasminClass = new soot.jimple.JasminClass(sClass);
    jasminClass.print(writerOut);
    writerOut.flush();
    streamOut.close();