1. ICompilationUnit originalUnit =...; // Get original compilation unit
The method for obtaining an ICompilationUnit instance is simple and will not be detailed here.
2. originalUnit. becomeWorkingCopy (new SubProgressMonitor (monitor, 1); // get the working copy of the compilation unit
Note: switching the compilation unit to the working copy mode means creating a place in the memory to store a copy of Java code, that is, the Working Assistant
This cache. In work copy mode, the Work copy can get the work copy cache, an IBuffer instance. This instance is similar
Modify the StringBuffer API to modify the Java elements associated with it. Before submitting code
The changes are stored in the work copy until they are explicitly submitted.
3. IBuffer buffer = originalUnit. getBuffer (); // get the working copy Cache
Eg:
// Modify buffer and reconcile
IBuffer buffer = (IOpenable) workingCopy). getBuffer ();
Buffer. append ("class X {}");
4. JavaModelUtil. reconcile (originalUnit); // synchronous
Or workingCopy. reconcile (ICompilationUnit. NO_AST, false, null, null );
5. originalUnit. commitWorkingCopy (true, monitor); // save. After the modification is completed, you must submit the code changes to save the changes to the disk.
6. if (originalUnit! = Null)
CopyCU. discardWorkingCopy (); // to prevent resource waste, discard the copy after submission
The appendix provides instructions and sample Process Code provided by the Eclipse official help.
Note: You can also use applyTextEdit.
For example the following code snippet creates a working copy on a compilation unit using a custom working copy owner. the snippet modifies the buffer, reconciles the changes, commits the changes to disk and finally discards the working copy.
// Get original compilation unit
ICompilationUnit originalUnit = ...;
// Get working copy owner
WorkingCopyOwner owner = ...;
// Create working copy
ICompilationUnit workingCopy = originalUnit. getWorkingCopy (owner, null );
// Modify buffer and reconcile
IBuffer buffer = (IOpenable) workingCopy). getBuffer ();
Buffer. append ("class X {}");
WorkingCopy. reconcile (ICompilationUnit. NO_AST, false, null, null );
// Commit changes
WorkingCopy. commitWorkingCopy (false, null );
// Destroy working copy
WorkingCopy. discardWorkingCopy ();
The compilation unit's buffer can also be modified using the ICompilationUnit. applyTextEdit method.
// Get original compilation unit
ICompilationUnit originalUnit = ...;
// Get working copy owner
WorkingCopyOwner owner = ...;
// Create working copy
ICompilationUnit workingCopy = originalUnit. getWorkingCopy (owner, null );
// Get text edits
TextEdit edit = ...;
// Modify buffer and reconcile
WorkingCopy. applyTextEdit (edit, null );
WorkingCopy. reconcile (ICompilationUnit. NO_AST, false, null, null );
// Commit changes
WorkingCopy. commitWorkingCopy (false, null );
// Destroy working copy
WorkingCopy. discardWorkingCopy ();
In short, the step for modifying a single compilation unit is to convert the unit into a work copy to get the work copy cache; modify the cache and regularly synchronize it with the original file; submit changes and discard copies to save files. In addition, you can use the APIS provided by DOM/AST (more powerful), which will be summarized in subsequent blog posts.
Author: cuser_online