Bundle.loadClass() 返回没有字段或方法的 Class 实例

发布于 2024-12-09 04:21:51 字数 1737 浏览 0 评论 0原文

我想以编程方式创建一个 OSGi 框架,用它加载一个捆绑包并从该捆绑包加载一个类。当我调用 Bundle.loadClass() 时,我得到一个 Class 实例,其中所有 fields\methods\constructor 字段设置为 null。它只有一个名字。我无法访问任何公共方法等。我尝试了 Equinox 和 Felix,结果相同。

Bundle 的 MANIFEST :

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Activator: org.osgitest.osgitest.Activator
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-Name: OSGi Test
Bundle-SymbolicName: org.osgitest.osgitest
Bundle-Version: 1.0
Import-Package: org.osgi.framework
Export-Package: org.osgitest.osgitest.test

框架设置:

FrameworkFactory ff = new EquinoxFactory();
Map<String, String> config = new HashMap<String, String>(1);
config.put("org.osgi.framework.storage.clean", "onFirstInit");
Framework framework = ff.newFramework(config);
framework.init();
framework.start();

FrameworkFactory ff = new org.apache.felix.framework.FrameworkFactory();
Map<String, String> config = new HashMap<String, String>(1);
config.put("org.osgi.framework.storage.clean", "onFirstInit");
Framework framework = ff.newFramework(config);
framework.init();
framework.start();

Bundle 加载:

Bundle testBundle = framework.getBundleContext().installBundle("file:C:\\org-osgitest-osgitest.jar");
testBundle.start();
Class<?> classOne = testBundle.loadClass("org.osgitest.osgitest.test.ClassOne");
Class<?> activator = testBundle.loadClass("org.osgitest.osgitest.Activator");

Activator Class 实例包含构造函数引用,但没有公共方法。它有 public void start(BundleContext c)public void stop(BundleContext c)

如何加载正确的Class?我做错了什么?感谢您的任何帮助。

I want to create an OSGi framework programmatically, load a Bundle with it and load a class from that bundle. When I call Bundle.loadClass() I get a Class isntance with all fields\methods\constructor fields set to null. It only has a name. I can't access any public methods, etc. I have tried both Equinox and Felix with the same result.

Bundle's MANIFEST:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Activator: org.osgitest.osgitest.Activator
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-Name: OSGi Test
Bundle-SymbolicName: org.osgitest.osgitest
Bundle-Version: 1.0
Import-Package: org.osgi.framework
Export-Package: org.osgitest.osgitest.test

Framework setup:

FrameworkFactory ff = new EquinoxFactory();
Map<String, String> config = new HashMap<String, String>(1);
config.put("org.osgi.framework.storage.clean", "onFirstInit");
Framework framework = ff.newFramework(config);
framework.init();
framework.start();

and

FrameworkFactory ff = new org.apache.felix.framework.FrameworkFactory();
Map<String, String> config = new HashMap<String, String>(1);
config.put("org.osgi.framework.storage.clean", "onFirstInit");
Framework framework = ff.newFramework(config);
framework.init();
framework.start();

Bundle loading:

Bundle testBundle = framework.getBundleContext().installBundle("file:C:\\org-osgitest-osgitest.jar");
testBundle.start();
Class<?> classOne = testBundle.loadClass("org.osgitest.osgitest.test.ClassOne");
Class<?> activator = testBundle.loadClass("org.osgitest.osgitest.Activator");

Activator Class instance contains constructor reference, but no public methods. It has public void start(BundleContext c) and public void stop(BundleContext c).

How can I load the correct Class? What am I doing wrong? Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦巷 2024-12-16 04:21:51

Class 使用某种延迟初始化。仅在执行 getMethods 等后,Class 实例才会填充方法/字段。这就是为什么我在调试器中看不到任何内容。

我尝试调用 public static void main(String[] args) 但没有将我的 String[] 转换为 Object。因此它被解释为 Object[] 并且 JVM 寻找具有签名 (args[0].class, args[1].class, ... , args[n].类)。这就是为什么我的方法没有找到的原因。

Class uses some kind of lazy-initialization. Class instance will be filled with methods/fields only after execution of getMethods etc. This is why I was not able to see anything in the debugger.

I tried to invoke public static void main(String[] args) and did not cast my String[] to Object. So it was interpreted like Object[] and JVM looked for a method with signature (args[0].class, args[1].class, ... , args[n].class). This is why my method was not found.

╭⌒浅淡时光〆 2024-12-16 04:21:51

由于包是一个 jar,您是否尝试过以正常的 -classpath 模式加载该类? OSGi 仅定义类加载器。 VM 仍然负责从字节码创建类对象。所以我不明白 OSGi 框架如何从 Class 对象中“剥离”成员。

Since a bundle is a jar, have you tried to load the class in normal -classpath mode? OSGi just defines classloaders. The VM is still responsible for creating the Class objects from the bytecodes. So I don't see how an OSGi framework could "strip" the members from the Class object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文