java

位置:IT落伍者 >> java >> 浏览文章

Eclipse重构功能:扩展点的使用


发布日期:2021年09月05日
 
Eclipse重构功能:扩展点的使用

Eclipse中提供了几个扩展点方便扩展重构功能

基本的重构功能有

RenameMoveCreateDeleteCopy对应扩展点即为

rerefactoringrenameParticipants

rerefactoringmoveParticipants

rerefactoringcreateParticipants

rerefactoringdeleteParticipants

pyParticipants

以ReName为例其余项与ReName大同小异

实现这个扩展点的基本语法

< extension point=rerefactoringrenameParticipants>

< renameParticipant

id=intramartappproducerrefactoringrenameTypeParticipant

name=Ebuilder RenameTypeParticipant

class=intramartappproducerrefactoringTypeRenameParticipant>

< enablement>

< /enablement>

< /renameParticipant>

< /extension>

这里默认对响应所有改名事件如果需要过滤可以在元素< enablement/>中加以定义不赘述实现改名扩展的关键在实现类必须是rerefactoringparticipantsRenameParticipant的子类

下面代码进行了简单的Eclipse重构功能实现

import reresourcesIFile;

import reresourcesResourcesPlugin;

import reruntimeCoreException;

import reruntimeIProgressMonitor;

import reruntimeOperationCanceledException;

import rerefactoringChange;

import rerefactoringRefactoringStatus;

import rerefactoringTextFileChange;

import rerefactoringparticipantsCheckConditionsContext;

import rerefactoringparticipantsRenameParticipant;

import orgeclipsetexteditsReplaceEdit;

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 = ResourcesPlugingetWorkspace()getRoot()getProject(a)

getFile(a);

TextFileChange textFileChange = new TextFileChange(File Changed

file);

ReplaceEdit edit = new ReplaceEdit( haha);

textFileChangesetEdit(edit);

return textFileChange;

}

@Override

public String getName() {

return Ebuilder RenameTypeParticipant;

}

@Override

protected boolean initialize(Object element) {

// need sub

return true;

}

}

CreateChange方法内实现过于粗糙仅仅是为了可以让大家看到结果

Eclipse重构功能结果预览

通过利用扩展点我们就自然的将重构时的差异比较警告preview重构historyredo/undo等eclipse平台提供的基本功能加以利用了

Preview的结果如下图

)thisstylewidth=; border= twffan=done>

Eclipse重构功能特殊需求

下面我来介绍通过扩展点实现特殊需求

除了增移等基本重构外可以增加特殊需求的重构比如JDT中对类方法变量名的重构

实现特殊需求就要实现自己的Refactoring类继承类rerefactoringRefactoring实现相关方法这个类的结构与RenameParticipant等类的结构基本一致直接上代码不再赘述

cellPadding= width=% align=center border=>ff>

import reruntimeCoreException;

import reruntimeIProgressMonitor;

import reruntimeOperationCanceledException;

import rerefactoringChange;

import rerefactoringRefactoring;

import rerefactoringRefactoringStatus;

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

实现了RefactoringWizardWizardPage后即完成了UI到逻辑的实现

通过相应的Action的配置使用RefactoringWizardOpenOperation即完成了特殊重构需求的开发

为了方便对特殊需求的Refactoring逻辑部分的重用eclipse提供了一个扩展点

rerefactoringrefactoringContributions

通过扩展点的配置使用时通过ID即可随时得到Refactoring对象

本文来自站在eclipse的肩膀上《简单介绍eclipse中的重构》

上一篇:hibernate关联关系-一对一

下一篇:Struts用户和开发指南(前言之二)