The JDT Core plug-in provides API that allows you to programmatically create, delete and modify Java elements. See manipulating Java code for an introduction to the API provided by JDT Core.
An important concept in the Java model is the use of an in-memory copy of a compilation unit, called a "working copy" (IWorkingCopy). Using a working copy allows you to programmatically change a compilation unit before committing the changes to the underlying resource.
In the Java user interface, a parallel concept is to allow the user to extensively edit a resource before committing the working copy to the file system. By using a copy, your UI implementation can allow the user to either save the Java editor's content to disk or revert it back to its original content.
IWorkingCopyManager manages the working copies of Java compilation units used inside an editor. When you want to modify a compilation unit in an editor, you should obtain a working copy by connecting your editor's input element to the working copy manager. Edits are performed on the working copy.
Below is a code snippet that demonstrates the use of a working copy manager with a compilation unit editor:
void modifyCompilationUnit(ICompilationUnit cunit) throws PartInitException, CoreException { IEditorPart editor= JavaUI.openInEditor(cunit); IEditorInput input= editor.getEditorInput(); IWorkingCopyManager manager= JavaUI.getWorkingCopyManager(); manager.connect(input); try { ICompilationUnit workingCopy= manager.getWorkingCopy(input); // do the modifications on workingCopy using the normal JDT Core API. } finally { manager.disconnect(input); } // either keep the editor dirty or use editor.doSave(IProgressMonitor monitor) // to save the changes programmatically. }