Bundle.loadClass() returns Class instance without fields or methods
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.FrameworkFac开发者_开发问答tory();
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.
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.
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.
精彩评论