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中的重构》。
【编辑推荐】