2012-01-24 65 views
0

我有一個問題,我無法解決。 我瞭解春分變換,我無法解決這個麻煩,我激活我把這段代碼:Equinox轉換激活器

Properties properties = new Properties(); 
properties.put("equinox.transformerType", "xslt"); //$NON-NLS-1$ //$NON-NLS-2$ 
registration = context.registerService(URL.class.getName(), context.getBundle().getEntry("/transform.csv"), properties); //$NON-NLS-1$ 

但是Eclipse告訴我,registerService方法不能與參數一起使用(String,Url,Properties),它只接受(String,Url,Dictionary)。 Equinox_Transforms中的示例使用與我使用的方法相同的方法,但在這些情況下它可以正常工作。

有什麼問題?

我改變示例代碼在我的激活與此:

Dictionary properties = new Hashtable(); 
properties.put("equinox.transformerType", "xslt"); 
registration = context.registerService(URL.class.getName(), context.getBundle().getEntry("/transform.csv"), properties); 

是不是?

回答

0

當你使用一個Properties對象什麼是導入語句? 看起來你可能沒有使用java.util.Properties(java.util.Dictionary的一個子類)。有很多稱爲Property的類。

+0

我正在使用java.util.Properties –

+0

嗯...我假設上下文對象是一個BundleContext? – katsharp

+0

是的。是。我所做的只是將Equinox示例中的所有代碼複製到Activator中。有趣的是,這個例子在運行時給了我一個錯誤,但之前沒有。 –

2

從BundleContext類型的registerService(String,Object,Dictionary)中得到的編譯錯誤不適用於參數(String,URL,Properties)是正確的。 這是因爲java中的泛型。 java.util.Properties類擴展了Hashtable,它遵循通用規則。 現在,如果你看到

ServiceRegistration<?> registerService(String clazz, Object service, Dictionary<String, ?> properties); 

可以清楚地提到,期待第三個參數作爲字典< .String()由BundleContext.reregisterService預期的論點,?>。

所以,當你使用簡單的屬性時,它無法在編譯時識別第三個參數的類型。

所以,你第二個方法是正確的:

Dictionary properties = new Hashtable(); 
properties.put("equinox.transformerType", "xslt"); 
registration = context.registerService(URL.class.getName(),  
context.getBundle().getEntry("/transform.csv"), properties); 

你甚至可以通過改變字典參考

Dictionary<Object,Object> properties = new Hashtable(); 

檢查這將再次給你相同的編譯時間錯誤

您可以從仿製藥here獲得更多信息。

您可以在Equinox Transform revealed上閱讀有關Equinox變換和示例示例的更多信息。

+0

清除一個小的疑問 – Arun