2011-10-12 24 views
1

我想以編程方式創建一個OSGi框架,使用它加載一個Bundle並從該Bundle中加載一個類。當我打電話給Bundle.loadClass()時,我得到一個Class,其中所有fields \ methods \ constructor字段都設爲null。它只有一個名字。我無法訪問任何公共方法等。我已經嘗試了Equinox和Felix,結果相同。Bundle.loadClass()返回沒有字段或方法的類實例

捆綁的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 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"); 

激活碼Class實例包含構造函數引用,但沒有公共方法。它有public void start(BundleContext c)public void stop(BundleContext c)

如何加載正確的Class?我究竟做錯了什麼?謝謝你的幫助。

回答

1

Class使用某種懶惰初始化。 Class實例只有在執行getMethods後纔會填寫方法/字段。這就是爲什麼我無法在調試器中看到任何東西。

我試圖調用public static void main(String[] args),並沒有將我的String[]轉換爲Object。所以它被解釋爲像Object[]和JVM尋找簽名爲(args[0].class, args[1].class, ... , args[n].class)的方法。這就是爲什麼我的方法沒有找到。

0

由於bundle是一個jar,你是否試圖在正常的classpath模式下加載類? OSGi只是定義了類加載器。 VM仍然負責從字節碼創建Class對象。所以我沒有看到OSGi框架如何從Class對象中「剝離」成員。

相關問題