Pages

Wednesday, October 31, 2012

Writing file from CompilationUnit (ASTNode)

When you have an object of iCompilationUnit, you can save it to the file.
  1. You need to create Document object from ICompilationUnit
  2. You need a ASTRewrite object from CompilationUnit
  3. Filling in the Document object using apply() method
  4. Storing the source code string from document.get() method
Document document = new Document(iCompilationUnit.getSource());
ASTRewrite rewriter = ASTRewrite.create(compilationUnit); // ? check if the parameter object type is correct

rewrite.rewriteAST().apply(document);
String source = document.get();

File file = new File(DEST_FILE_PATH);
FileUtils.writeStringToFile(file, source) 

FileUtils

You need to save apache file utilities to use the FileUtils.

References

9 comments:

  1. After a long search for a simple example of writing a Compilation unit to File that works, I found this one.

    Most of the example on AST you find on google are useless.
    Very nice Thank You

    ReplyDelete
  2. "Enhance your data analytics skills with power bi course online , designed for beginners and professionals alike. Learn to create interactive dashboards, visualize data effectively, and boost your career prospects."

    ReplyDelete
  3. "Boost your career with sales force admin course mastering the skills to manage CRM, automate processes, and drive business growth."

    ReplyDelete
  4. "Enhance your career with our comprehensive sales force developer training designed for beginners and professionals alike.

    ReplyDelete
  5. Learn how to structure and organize data efficiently using models that support accurate analysis and decision-making.data modeling certification course

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Learn core Java programming concepts to build secure, scalable, and high-performance applications.java training online

    ReplyDelete
  8. Learn to design user-friendly and visually appealing digital products by focusing on user experience and interface design. ui courses online

    ReplyDelete
  9. Learn to access, verify, and manage digital land records using the Bhoomi land information system. dell boomi full course

    ReplyDelete