温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Eclipse扩展点怎么用

发布时间:2021-12-04 10:03:38 来源:亿速云 阅读:290 作者:小新 栏目:编程语言

小编给大家分享一下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扩展点怎么用

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI