歡迎您光臨本站 註冊首頁

Eclipse重構功能:擴展點的使用

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0

Eclipse中提供了幾個擴展點,方便擴展重構功能.

基本的重構功能有,

Rename,Move,Create,Delete,Copy.對應擴展點即為:

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大同小異.

實現這個擴展點的基本語法:

< 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>

這裡默認對響應所有改名事件,如果需要過濾可以在元素< enablement/>中加以定義.不贅述.實現改名擴展的關鍵在實現類,必須是org.eclipse.ltk.core.refactoring.participants.RenameParticipant;的子類

下面代碼進行了簡單的Eclipse重構功能實現.

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方法內實現過於粗糙,僅僅是為了可以讓大家看到結果.

Eclipse重構功能結果預覽

通過利用擴展點,我們就自然的將重構時的差異比較,警告,preview,重構history,redo/undo等,eclipse平台提供的基本功能加以利用了.

Preview的結果如下圖.

Eclipse重構功能:特殊需求

下面我來介紹,通過擴展點實現特殊需求.

除了增,刪,改,移等基本重構外,可以增加特殊需求的重構,比如JDT中對類,方法,變數名的重構.

實現特殊需求,就要實現自己的Refactoring類,繼承類org.eclipse.ltk.core.refactoring.Refactoring實現相關方法,這個類的結構與RenameParticipant等類的結構基本一致,直接上代碼,不再贅述.

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";
}

}

這個類負責處理特殊需求與重構的特殊邏輯.

除了邏輯層,還需要對表現層有實現:

RefactoringWizard 及 RefactoringWizardPage.

實現了Refactoring,Wizard,WizardPage后,即完成了,UI到邏輯的實現.

通過相應的Action的配置,使用RefactoringWizardOpenOperation.即完成了特殊重構需求的開發.

為了方便對特殊需求的Refactoring邏輯部分的重用,eclipse提供了一個擴展點:

org.eclipse.ltk.core.refactoring.refactoringContributions

通過擴展點的配置,使用時通過ID即可隨時得到Refactoring對象.

本文來自站在eclipse的肩膀上:《簡單介紹eclipse中的重構》.


[火星人 ] Eclipse重構功能:擴展點的使用已經有659次圍觀

http://coctec.com/docs/java/show-post-61097.html