2014-04-14 30 views
2

我將我的插件項目導出爲Product,當我運行該產品(eclipse應用程序)時,主插件(org.example.framework.core)無法找到在另一個插件中定義的類插件(org.example.abc),它實現了由主插件提供的擴展點的擴展。該類是擴展中定義的元素之一。但是,當我在Eclipse中運行項目時,一切運行良好!在主插件中找不到定義在其他插件中的類--Eclipse Product

這裏是日誌(atvste.ppt.ptfwDescription.abc.decoderInfo是在org.example.abc plugin一個包中):

0 [Worker-2] ERROR org.example.framework.core.ptfw.codec.decode.MsgDecoderInfo org.example.framework.core.ptfw.codec.decode.MsgDecoderInfo.createInstance(MsgDecoderInfo.java:114) : can not create class for :atvste.ppt.ptfwDescription.abc.decoderInfo.MsgDecoderInfoABC atvste.ppt.ptfwDescription.abc.decoderInfo.MsgDecoderInfoABC cannot be found by org.example.framework.core_1.0.0.201404111439 java.lang.ClassNotFoundException: atvste.ppt.ptfwDescription.abc.decoderInfo.MsgDecoderInfoABC cannot be found by org.example.framework.core_1.0.0.201404111439 at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:501) at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:421) at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:412) at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at org.example.framework.core.ptfw.codec.decode.MsgDecoderInfo.createInstance(MsgDecoderInfo.java:104) at org.example.framework.core.ptfw.codec.decode.DecoderInfo.<init>(DecoderInfo.java:56) at org.example.framework.core.ptfw.codec.decode.DecoderInfo.createInstance(DecoderInfo.java:125) at org.example.framework.core.ptfw.codec.ptfwObjectsFactory.decoderInfoItf_createInstance(ptfwObjectsFactory.java:200) at org.example.framework.persistence.jaxb.ptfwDescriptionPersistenceJaxb.createptfwDescription(ptfwDescriptionPersistenceJaxb.java:326) at org.example.framework.persistence.jaxb.ptfwDescriptionPersistenceJaxb.fillptfwDescription(ptfwDescriptionPersistenceJaxb.java:247) at org.example.framework.persistence.jaxb.ptfwDescriptionPersistenceJaxb.createInstance(ptfwDescriptionPersistenceJaxb.java:232) at org.example.framework.persistence.jaxb.ptfwDescriptionPersistenceJaxb.open(ptfwDescriptionPersistenceJaxb.java:146) at org.example.framework.core.ptfw.codec.ptfwDescription.createInstance(ptfwDescription.java:152) at org.example.framework.core.ptfw.codec.command.CmdLoadptfwDescription.loadptfwDescription(CmdLoadptfwDescription.java:50) at org.example.framework.core.ptfw.codec.command.CmdLoadptfwDescription.execute(CmdLoadptfwDescription.java:40) at org.example.framework.core.runtime.JobService$2.run(JobService.java:93) at org.eclipse.core.internal.jobs.Worker.run(Worker.java:53)

編輯:功能創建的類的實例沒有發現

public static IMessageDecoderInfo createInstance(XmlgMsgDecoderInfo pMsgDecoderInfoType, 
     IMessageDecoderInfo msgDecoder) 
{ 

    String className = pMsgDecoderInfoType.getClassName(); 
    if(className!=null) 
    { 
     try 
     { 
      Class<?> formalArgs[] = new Class[1]; 
      formalArgs[0] = XmlgMsgDecoderInfo.class; 
      Class<?> clazz; 
      if (msgDecoder != null) 
      { 
       clazz = msgDecoder.getClass(); 
      } 
      else 
      { 
       clazz = Class.forName(className); 
      } 
      Constructor<?> constructor = clazz.getConstructor(formalArgs); 
      java.lang.Object actualArgs[] = 
      { pMsgDecoderInfoType }; 

      return (IMessageDecoderInfo)constructor.newInstance(actualArgs); 
     }catch(Exception e) { 
      String error = "can not create class for :" +className+ " " + e.getMessage(); 
      if (LOGGER.isEnabledFor(Level.ERROR)) { 
       LOGGER.error(error, e); 
      } 
      throw new CreatePtfwElementRuntimeException(error, e); 
     } 
    } 
    return new MsgDecoderInfo(pMsgDecoderInfoType); 
}` 
+0

如何'MsgDecoderInfo.createInstance'創建類的實例插件ID? –

+0

使用反射...我應該提供代碼嗎? – schwarz

+0

是的顯示代碼 –

回答

2

由於複雜的Eclipse使用的類加載器系統不能使用Class.forName在另一個插件中加載類。

如果您的代碼正在處理擴展點定義,那麼您將擁有指定類名的配置元素的IConfigurationElement。您可以使用

IConfigurationElement configElement = ....; 

Object classInstance = configElement.createExecutableExtension("class attribute name"); 

其中'class attribute name'是擴展點元素中指定要加載的類的屬性的名稱。正在創建的類必須具有無參數構造函數。

另外,您可以使用加載類:

Bundle bundle = Platform.getBundle("plugin id"); 

Class<?> theClass = bundle.loadClass("class name"); 

... construct as usual ... 

如果你有IConfigurationElement你可以使用

String pluginId = configElement.getContributor().getName(); 
+0

我有一個類似的問題,但不是用上面的方法訪問類,而是在依賴項中添加了包含該類的包。情況不一樣嗎? –

+1

@JohnDoe你可以添加依賴 - 這並不總是可能的。你可能希望人們使用你的擴展點編寫他們自己的插件 - 他們不能添加依賴到你的插件。 –

相關問題