通過JDT修改(Modify)Java代碼的流程步驟

來源:互聯網
上載者:User

1.ICompilationUnit originalUnit = ...;// Get original compilation unit
擷取ICompilationUnit執行個體的方式很簡單,此處不再詳述。

2.originalUnit.becomeWorkingCopy(new SubProgressMonitor(monitor, 1));//得到編譯單元的工作複本
說明:將編譯單元切換到工作複本模式,就是在記憶體中建立一塊存放Java代碼副本的地方,即工作副
本緩衝。工作複本模式下,工作複本可以得到工作複本緩衝,一個IBuffer的執行個體。該執行個體類似於
StringBuffer的API,對其修改就可以達到修改與之關聯的 Java元素的效果。在提交代碼之前,對緩衝
修改一直儲存在工作複本中,直至被顯式提交。

3.IBuffer buffer = originalUnit.getBuffer(); // 得到工作複本緩衝
eg:
//Modify buffer and reconcile
 IBuffer buffer = ((IOpenable)workingCopy).getBuffer();
 buffer.append("class X {}");
 
4.JavaModelUtil.reconcile(originalUnit); //同步
或workingCopy.reconcile(ICompilationUnit.NO_AST, false, null, null);

5.originalUnit.commitWorkingCopy(true, monitor);//儲存。修改完畢,需要將代碼的變化提交才能儲存到磁碟上
 
6.if(originalUnit != null)
   copyCU.discardWorkingCopy();//為了防止資源浪費,提交之後,丟棄副本
 
 
附錄中是Eclipse官方協助所提供的說明及樣本流程代碼。
註:通過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();
總之,針對單個編譯單元的修改的步驟是將工作單元轉化為工作複本,得到工作複本緩衝;修改緩衝並定時與原檔案同步;提交變化、丟棄副本來儲存檔案。另外,通過DOM/AST所提供的API(更加強大)也是可以的,將在後續博文中進行總結。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.