Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8682013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:39:29+00:00 2026-06-12T21:39:29+00:00

I have a eclipse plugin code to manipulate a class (smcho.Hello) in a project/workspace.

  • 0

I have a eclipse plugin code to manipulate a class (smcho.Hello) in a project/workspace.
I could create a CompilationUnit and did some modifications on it, but I need to save the result in different file to check the differences between the two version.

This is the code how I get the CompilationUnit.

IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject("Hello");
project.open(null);
IJavaProject javaProject = JavaCore.create(project);
IType lwType = javaProject.findType("smcho.Hello");
org.eclipse.jdt.core.ICompilationUnit lwCompilationUnit = lwType.getCompilationUnit();
final ASTParser parser = ASTParser.newParser(AST.JLS3); 
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(lwCompilationUnit);
parser.setResolveBindings(true); // we need bindings later on
CompilationUnit unit = (CompilationUnit) parser.createAST(null /* IProgressMonitor */); 
// modify the unit AST node

How can I save this modified unit into a new file?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:39:30+00:00Added an answer on June 12, 2026 at 9:39 pm

    You can use an ASTRewriter to do so.

    // get the ast rewriter
    final ASTRewrite rewriter = ASTRewrite.create(ast);
    // get the current document source
    final Document document = new Document(unit.getSource());
    // compute the edits you have made to the compilation unit
    final TextEdit edits = rewriter.rewriteAST();
    // apply the edits to the document
    edits.apply(document);
    // get the new source
    String newSource = document.get();
    // now write this source to some other file.
    

    Check the link below. This gives insight on how to write the AST changes to the file.

    http://www.eclipse.org/articles/article.php?file=Article-JavaCodeManipulation_AST/index.html

    Update:
    This is how i write to the file:

    File file = new File(destFile);
    FileUtils.writeStringToFile(File file, String newSource) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Eclipse plugin project that has several file resources that I would
I have installed eclipse JDE plugin (ie BlackBerry_JDE_PluginFull_1.0.0.67.exe) , i can create a blackberry
I am developing an Eclipse plugin and have tests for it. Some are regular
I have the following setup: eclipse a standard Java project (A) an eclipse plugin
I am writing an Eclipse plugin, and I have a wizard that create my
Does anyone have a hello world sample or tutorial for creating an Eclipse plugin
I have a maven project imported into Eclipse. I'm trying to understand the code
I have a task to create a wysiwyg editor as an eclipse plugin. Please
I'm developing an Eclipse Plugin. Inside my plugin I have the following code: Bundle
I have Eclipse Indigo with the Scala IDE plugin. I downloaded a lift project

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.