开发者

eclipse rcp : class loading problem

开发者 https://www.devze.com 2023-03-21 12:59 出处:网络
I have plugin A which depends on Plugin B, I am trying to load a class defined in Plugin B from plugin A. And I am sure the class is in the exported packages. but I got ClassNotFoundException, did I m

I have plugin A which depends on Plugin B, I am trying to load a class defined in Plugin B from plugin A. And I am sure the class is in the exported packages. but I got ClassNotFoundException, did I miss any thing ?

page = (IPage) Class.forName(pageClass).newInstance();

error messages :

java.lang.ClassNotFoundException: amarsoft.dbmp.credit.web.dataobject.DOListPage
    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:513)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:429)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:417)
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at amarsoft.rcp.base.widgets.DefaultPageRegistry.findPage(DefaultPageRegistry.java:27)
    at amarsoft.rcp.base.widgets.AbstractPageControler.showPage(AbstractPageControler.java:27)
    at amarsoft.rcp.base.widgets.AbstractPageControler.showPage(AbstractPageControler.java:37)
    at amarsoft.dbmp.func开发者_如何学Gotion.ui.FunctionListPage$3.linkActivated(FunctionListPage.java:261)
    at org.eclipse.ui.forms.widgets.AbstractHyperlink.handleActivate(AbstractHyperlink.java:233)
    at org.eclipse.ui.forms.widgets.AbstractHyperlink.handleMouseUp(AbstractHyperlink.java:327)
    at org.eclipse.ui.forms.widgets.AbstractHyperlink.access$2(AbstractHyperlink.java:311)
    at org.eclipse.ui.forms.widgets.AbstractHyperlink$4.handleEvent(AbstractHyperlink.java:125)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4165)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3754)
    at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2696)
    at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2660)
    at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2494)
    at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:674)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:667)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
    at amarsoft.adt.rcp.ADTApplication.start(ADTApplication.java:29)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:344)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:622)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:577)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1410)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1386)


If you Require-Bundle; pluginB from pluginA, then a Class.forName(*) should have worked. Although I'll point out that using PageClass class = new PageClass(); would also work. Why are you trying to use reflection when you can just depend on that class directly?

For the common pattern where you provide a framework behaviour that other plugins should extend by providing implementations, eclipse provides the notion of Extension Points/Extensions.

Plugin A would provide an extension point. Plugin B would list an extension for that extension point, and provide an implementation class. In this case, Plugin B requires Plugin A, not the other way around.

Then Plugin A uses org.eclipse.core.runtime.IExtensionRegistry.getConfigurationElementsFor(String) to read all of the extension contributed and org.eclipse.core.runtime.IConfigurationElement.createExecutableExtension(String) to instantiate the contributed implementations (the registry takes care of making sure the class can be instantiated).

0

精彩评论

暂无评论...
验证码 换一张
取 消