eclipse 如何在 equinox 中获取包的类加载器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1503304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 14:03:18  来源:igfitidea点击:

How to get classloader for a bundle in equinox?

eclipseosgiclassloaderbundleequinox

提问by Suraj Chandran

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?

我已经为此阅读了很多 equinox 代码,但仍然无法找出在 eclipse equinox 设置中获取 osgi 包的类加载器的非hacky 方法。有吗?

采纳答案by SteveD

The short answer (certainly for OSGi 4.1, not sure of 4.2) is you can't get a bundle's classloader. However the Bundleinterface 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 BundleDelegatingClassLoaderclass instead.

简短的回答(当然对于 OSGi 4.1,不确定 4.2)是你不能得到一个包的类加载器。然而,该Bundle接口公开了一个loadClass()方法,这将允许您编写一个类加载器来包装捆绑 API 并委托给该loadClass()方法。或者您可以节省一些时间并改用 Spring DM 的BundleDelegatingClassLoader类。

回答by Balazs Zsoldos

In OSGi 4.3 you can use:

在 OSGi 4.3 中,您可以使用:

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

回答by tux2323

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

bundle的类加载器可以通过BundleWiring接口获取。这里有一个简短的例子:

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

回答by Andrew Niefer

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

在普通的java代码中,你可以得到加载给定对象的类加载器

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.

这同样适用于 Equinox,只需使用您感兴趣的包中的对象或类型即可。