千家信息网

Eclipse扩展点怎么用

发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,小编给大家分享一下Eclipse扩展点怎么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!Eclipse中提供了几个扩展点
千家信息网最后更新 2026年11月02日Eclipse扩展点怎么用

小编给大家分享一下Eclipse扩展点怎么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

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扩展点怎么用"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

重构 特殊 需求 功能 逻辑 方法 篇文章 结果 代码 内容 结构 配置 一致 粗糙 大同小异 不怎么 事件 元素 关键 变量 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 科技服务与互联网 迪茉互联网科技 朗涯互联网科技 深圳艾佳互联网科技 丰台科技园 互联网公司 安控科技rtu产品工业互联网 宁波青枫互联网科技有限公司 承德科技产业互联网发展 乌苏新华互联网科技做什么的 天津腾达互联网科技有限公司 梅州GM互联网络科技有限公司 启辰互联网科技 互联网相关科技技术 2021世界互联网领先科技成果发布 万物互联网智能科技 互联网时代高科技布局 玉溪新华互联网科技怎么选 三亚科技创新互联网基地 现代科技的标志之一的互联网 伍佰万互联网科技是干嘛的 互联网科技园 中资互联网科技 成都仁贤领冠互联网科技有限公司 互联网科技公司需要的职位 科技与互联网的利与弊 青岛中康互联网医疗科技有限公司 互联网5g黑科技有哪些 山东星卡互联网科技有限公司官网 广州富喜互联网络科技 互联网科技和医药行业
0