Eclipse重構(gòu)功能:擴(kuò)展點(diǎn)的使用
Eclipse中提供了幾個(gè)擴(kuò)展點(diǎn),方便擴(kuò)展重構(gòu)功能。
基本的重構(gòu)功能有,
Rename,Move,Create,Delete,Copy。對(duì)應(yīng)擴(kuò)展點(diǎn)即為:
- 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項(xiàng)與ReName大同小異。
實(shí)現(xiàn)這個(gè)擴(kuò)展點(diǎn)的基本語(yǔ)法:
- < 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)對(duì)響應(yīng)所有改名事件,如果需要過(guò)濾可以在元素< enablement/>中加以定義。不贅述。實(shí)現(xiàn)改名擴(kuò)展的關(guān)鍵在實(shí)現(xiàn)類,必須是org.eclipse.ltk.core.refactoring.participants.RenameParticipant;的子類
下面代碼進(jìn)行了簡(jiǎn)單的Eclipse重構(gòu)功能實(shí)現(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)實(shí)現(xiàn)過(guò)于粗糙,僅僅是為了可以讓大家看到結(jié)果。
Eclipse重構(gòu)功能結(jié)果預(yù)覽
通過(guò)利用擴(kuò)展點(diǎn),我們就自然的將重構(gòu)時(shí)的差異比較,警告,preview,重構(gòu)history,redo/undo等,eclipse平臺(tái)提供的基本功能加以利用了。
Preview的結(jié)果如下圖。
Eclipse重構(gòu)功能:特殊需求
下面我來(lái)介紹,通過(guò)擴(kuò)展點(diǎn)實(shí)現(xiàn)特殊需求。
除了增,刪,改,移等基本重構(gòu)外,可以增加特殊需求的重構(gòu),比如JDT中對(duì)類,方法,變量名的重構(gòu)。
實(shí)現(xiàn)特殊需求,就要實(shí)現(xiàn)自己的Refactoring類,繼承類org.eclipse.ltk.core.refactoring.Refactoring實(shí)現(xiàn)相關(guān)方法,這個(gè)類的結(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";
- }
- }
這個(gè)類負(fù)責(zé)處理特殊需求與重構(gòu)的特殊邏輯。
除了邏輯層,還需要對(duì)表現(xiàn)層有實(shí)現(xiàn):
RefactoringWizard 及 RefactoringWizardPage。
實(shí)現(xiàn)了Refactoring,Wizard,WizardPage后,即完成了,UI到邏輯的實(shí)現(xiàn)。
通過(guò)相應(yīng)的Action的配置,使用RefactoringWizardOpenOperation。即完成了特殊重構(gòu)需求的開(kāi)發(fā)。
為了方便對(duì)特殊需求的Refactoring邏輯部分的重用,eclipse提供了一個(gè)擴(kuò)展點(diǎn):
org.eclipse.ltk.core.refactoring.refactoringContributions
通過(guò)擴(kuò)展點(diǎn)的配置,使用時(shí)通過(guò)ID即可隨時(shí)得到Refactoring對(duì)象。
本文來(lái)自站在eclipse的肩膀上:《簡(jiǎn)單介紹eclipse中的重構(gòu)》。
【編輯推薦】