新聞中心
Eclipse中提供了幾個擴展點,方便擴展重構(gòu)功能。

十多年專注成都網(wǎng)站制作,成都定制網(wǎng)頁設(shè)計,個人網(wǎng)站制作服務(wù),為大家分享網(wǎng)站制作知識、方案,網(wǎng)站設(shè)計流程、步驟,成功服務(wù)上千家企業(yè)。為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計及定制高端網(wǎng)站建設(shè)服務(wù),專注于成都定制網(wǎng)頁設(shè)計,高端網(wǎng)頁制作,對成都戶外休閑椅等多個方面,擁有多年的營銷推廣經(jīng)驗。
基本的重構(gòu)功能有,
Rename,Move,Create,Delete,Copy。對應(yīng)擴展點即為:
- org.eclipse.ltk.core.refactoring.renameParticipants
- org.eclipse.ltk.core.refactoring.moveParticipants
- org.eclipse.ltk.core.refactoring.createParticipants
- org.eclipse.ltk.core.refactoring.deleteParticipants
- org.eclipse.ltk.core.refactoring.copyParticipants
以ReName為例,其余4項與ReName大同小異。
實現(xiàn)這個擴展點的基本語法:
- < extension point="org.eclipse.ltk.core.refactoring.renameParticipants">
- < renameParticipant
- id="jp.co.intramart.app.producer.refactoring.renameTypeParticipant"
- name="Ebuilder RenameTypeParticipant"
- class="jp.co.intramart.app.producer.refactoring.TypeRenameParticipant">
- < enablement>
- < /enablement>
- < /renameParticipant>
- < /extension>
這里默認(rèn)對響應(yīng)所有改名事件,如果需要過濾可以在元素< enablement/>中加以定義。不贅述。實現(xiàn)改名擴展的關(guān)鍵在實現(xiàn)類,必須是org.eclipse.ltk.core.refactoring.participants.RenameParticipant;的子類
下面代碼進(jìn)行了簡單的Eclipse重構(gòu)功能實現(xiàn)。
- import org.eclipse.core.resources.IFile;
- import org.eclipse.core.resources.ResourcesPlugin;
- import org.eclipse.core.runtime.CoreException;
- import org.eclipse.core.runtime.IProgressMonitor;
- import org.eclipse.core.runtime.OperationCanceledException;
- import org.eclipse.ltk.core.refactoring.Change;
- import org.eclipse.ltk.core.refactoring.RefactoringStatus;
- import org.eclipse.ltk.core.refactoring.TextFileChange;
- import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
- import org.eclipse.ltk.core.refactoring.participants.RenameParticipant;
- import org.eclipse.text.edits.ReplaceEdit;
- public class TypeRenameParticipant extends RenameParticipant {
- public TypeRenameParticipant() {
- }
- @Override
- public RefactoringStatus checkConditions(IProgressMonitor pm,
- CheckConditionsContext context) throws OperationCanceledException {
- return new RefactoringStatus();
- }
- @Override
- public Change createChange(IProgressMonitor pm) throws CoreException,
- OperationCanceledException {
- IFile file = ResourcesPlugin.getWorkspace().getRoot().getProject("a")
- .getFile("a");
- TextFileChange textFileChange = new TextFileChange("File Changed ",
- file);
- ReplaceEdit edit = new ReplaceEdit(0, 1, "haha");
- textFileChange.setEdit(edit);
- return textFileChange;
- }
- @Override
- public String getName() {
- return "Ebuilder RenameTypeParticipant";
- }
- @Override
- protected boolean initialize(Object element) {
- // need sub
- return true;
- }
- }
CreateChange方法內(nèi)實現(xiàn)過于粗糙,僅僅是為了可以讓大家看到結(jié)果。
Eclipse重構(gòu)功能結(jié)果預(yù)覽
通過利用擴展點,我們就自然的將重構(gòu)時的差異比較,警告,preview,重構(gòu)history,redo/undo等,eclipse平臺提供的基本功能加以利用了。
Preview的結(jié)果如下圖。
Eclipse重構(gòu)功能:特殊需求
下面我來介紹,通過擴展點實現(xiàn)特殊需求。
除了增,刪,改,移等基本重構(gòu)外,可以增加特殊需求的重構(gòu),比如JDT中對類,方法,變量名的重構(gòu)。
實現(xiàn)特殊需求,就要實現(xiàn)自己的Refactoring類,繼承類org.eclipse.ltk.core.refactoring.Refactoring實現(xiàn)相關(guān)方法,這個類的結(jié)構(gòu)與RenameParticipant等類的結(jié)構(gòu)基本一致,直接上代碼,不再贅述。
- import org.eclipse.core.runtime.CoreException;
- import org.eclipse.core.runtime.IProgressMonitor;
- import org.eclipse.core.runtime.OperationCanceledException;
- import org.eclipse.ltk.core.refactoring.Change;
- import org.eclipse.ltk.core.refactoring.Refactoring;
- import org.eclipse.ltk.core.refactoring.RefactoringStatus;
- public class ProducerRefactoring extends Refactoring {
- @Override
- public RefactoringStatus checkFinalConditions(IProgressMonitor pm)
- throws CoreException, OperationCanceledException {
- // need sub
- return new RefactoringStatus();
- }
- @Override
- public RefactoringStatus checkInitialConditions(IProgressMonitor pm)
- throws CoreException, OperationCanceledException {
- // need sub
- return new RefactoringStatus();
- }
- @Override
- public Change createChange(IProgressMonitor pm) throws CoreException,
- OperationCanceledException {
- // need sub
- return null;
- }
- @Override
- public String getName() {
- return "ProducerRefactoring";
- }
- }
這個類負(fù)責(zé)處理特殊需求與重構(gòu)的特殊邏輯。
除了邏輯層,還需要對表現(xiàn)層有實現(xiàn):
RefactoringWizard 及 RefactoringWizardPage。
實現(xiàn)了Refactoring,Wizard,WizardPage后,即完成了,UI到邏輯的實現(xiàn)。
通過相應(yīng)的Action的配置,使用RefactoringWizardOpenOperation。即完成了特殊重構(gòu)需求的開發(fā)。
為了方便對特殊需求的Refactoring邏輯部分的重用,eclipse提供了一個擴展點:
org.eclipse.ltk.core.refactoring.refactoringContributions
通過擴展點的配置,使用時通過ID即可隨時得到Refactoring對象。
本文來自站在eclipse的肩膀上:《簡單介紹eclipse中的重構(gòu)》。
【編輯推薦】
- 在Eclipse中安裝pydev插件的經(jīng)驗分享
- 自定義Eclipse菜單項:去除多余的UI圖標(biāo)
- 六大便捷Eclipse JDT特性一覽
- 部署Eclipse RAP到Tomcat的步驟詳解
- Eclipse平臺應(yīng)用與開發(fā)專題
分享題目:Eclipse重構(gòu)功能:擴展點的使用
鏈接地址:http://m.fisionsoft.com.cn/article/djcessi.html


咨詢
建站咨詢
