在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中实现扩展点:
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;
}
}
<extension
point="coreWizards">
<coreWizards
class="com.mytest.wizards.TemplateCreateWizard"
wizardID="com.mytest.wizards.TemplateCreateWizardID"/>
</extension>
类com.mytest.wizards.TemplateCreateWizard继承了A中的类CoreWizard。point="coreWizards">
<coreWizards
class="com.mytest.wizards.TemplateCreateWizard"
wizardID="com.mytest.wizards.TemplateCreateWizardID"/>
</extension>
加载扩展点:
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类的实例。
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;
}