关于解决eclipse中的插件依赖

本文介绍了解决Eclipse插件开发中的循环依赖问题。通过定义核心插件A的扩展点并在其他插件如B中实现这些扩展点,可以有效地解决插件之间的循环依赖,同时保持各插件的独立性和低耦合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在eclipse插件开发过程中有时会遇到插件循环依赖的问题,其实本可以避免的,但谁也不能保证当初的设计会有问题。俺就遇到了这个问题,不过有一种解决办法就是通过写扩展点。

比如插件A和插件B,A是项目中所有插件的核心,所有的插件必须依赖于插件A,所有插件都使用A提供的接口和服务。但有时为了降低插件的耦合性,或者保持插件的独立性,一些个性的操作接口,都应该尽量定义在每个插件中,尽量不要定义在A中。比如,在插件B中定义了一个个性wizard,但是A要实例化这个wizard,问什么不在B中直接实例化?唉!不是说了嘛,设计失误,没辙B要使用A中的接口,而A要实例化B中的类,循环了!这可咋办?想来想去,只好在A中写个CoreWizard扩展点,在B中需要A来实例化的wizard只要继承A中的CoreWizard,并且在B中plugin.xml里面实现CoreWizard扩展点,这样A就可以把B中的wizard通过扩展点加载当成自己的CoreWizard来处理。具体实现如下:

A中定义扩展点:

 

<extension-point id="coreWizards" name="coreWizards" schema="schema/coreWizards.exsd"/>

 

并实现类CoreWizard.java

public class CoreWizard extends Wizard implements INewWizard{
private String wizardID = null;
@Override
public boolean performFinish() {
// TODO Auto-generated method stub
return false;
}

public void init(IWorkbench workbench, IStructuredSelection selection) {
// TODO Auto-generated method stub
}

public String getWizardID() {
return wizardID;
}

public void setWizardID(String wizardID) {
this.wizardID = wizardID;
}
B中实现扩展点:

<extension
point="coreWizards">
<coreWizards
class="com.mytest.wizards.TemplateCreateWizard"
wizardID
="com.mytest.wizards.TemplateCreateWizardID"/>
</extension>
类com.mytest.wizards.TemplateCreateWizard继承了A中的类CoreWizard。

加载扩展点:

 

 

public static CoreWizard getCoreWizard() {
        String project 
= "projectA";
        String ID 
= "coreWizards";
        String attr 
= "class";
        String attr1 
= "wizardID";
        String wizardID 
= null;
        String wizardClass 
= null;
        
        IExtensionPoint extPoint 
= Platform.getExtensionRegistry().getExtensionPoint(project, ID);
        IConfigurationElement[] extensions 
= null;
        
        
if (null != extPoint) {
            extensions 
= extPoint.getConfigurationElements();
            
for (int i = 0; i < extensions.length; i++) {
                IConfigurationElement ic 
= extensions[i];
                wizardID 
= ic.getAttribute(attr1);
                wizardClass 
= ic.getAttribute(attr);
                CoreWizard ss 
= null;
                ss 
= (CoreWizard) ic.createExecutableExtension(attr);
                
return ss.getClass().newInstance();    
            }
        }

        
return null;
 }
通过上面的方法就可以获得CoreWizard的实例,实际上也就是TemplateCreateWizard类的实例。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值