Bundle.loadClass() 返回没有字段或方法的 Class 实例
我想以编程方式创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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 ofgetMethods
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 myString[]
toObject
. So it was interpreted likeObject[]
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.由于包是一个 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.