简体   繁体   中英

How to get classloader for a bundle in equinox?

I have read a lot of equinox code for this, but still can't figure out a non-hacky way of getting the classloader for a osgi bundle in eclipse equinox setup. Is there one?

在OSGi 4.3中,您可以使用:

bundle.adapt(BundleWiring.class).getClassLoader()

The short answer (certainly for OSGi 4.1, not sure of 4.2) is you can't get a bundle's classloader. However the Bundle interface exposes a loadClass() method and this would allow you to write a classloader that wraps the bundle API and delegates to that loadClass() method. Or you can save some time and use Spring DM's BundleDelegatingClassLoader class instead.

The class loader of a bundle can be obtained through the BundleWiring interface. Here a short example:

Bundle bundle = bundleContext.getBundle();
BundleWiring bundleWiring = bundle.adapt(BundleWiring.class);
ClassLoader classLoader = bundleWiring.getClassLoader();

In normal java code, you can get the class loader that loaded a given object with

object.getClass().getClassLoader();

Or even just

SomeType.class.getClassLoader();

The same applies to Equinox, just use an object or type that comes from the bundle you are interested in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM