类型不匹配:无法从Class< ..>转换等级< ...> [英] Type mismatch: cannot convert from Class <..> to Class <...>
问题描述
我已经继承了一个Eclipse RCP项目,该项目似乎可以在基于3.6的目标平台上正常工作。但是,向前迈进,我们需要更新到该平台的最新版本,但是当我将目标平台更改为3.7(或4.2)时,会遇到一些错误,类似于
I've 'inherited' an Eclipse RCP project which seems to work fine with a target platform set based around 3.6. However, moving forward we need to update to the latest version of the platform, but when I change the target platform to 3.7 (or 4.2) I get a handful of errors along the lines of
Type mismatch: cannot convert from Class<capture#1-of ?> to Class<? extends IDatasetProvider>
任何人都可以建议/提供解释为什么它在3.6中可行,但在3.7中不可行(然后)?关于从哪里开始解决此问题的任何想法也将很棒!
Can anyone suggest/provide an explanation of why this might work fine in 3.6, but not in 3.7 (and later)? Any ideas as to where I would start in resolving this would also be great!
导致此错误的代码片段(出现在b.loadClass部分):
A snippet of the code causing this error (which appears at the b.loadClass part):
List<Class<? extends IDatasetProvider>> list = new LinkedList<Class<? extends IDatasetProvider>>();
ClassMap<IDatasetProvider, List<String>> map = new ClassMap<IDatasetProvider, List<String>>();
for (IConfigurationElement e : elements)
{
try
{
Bundle b = Platform.getBundle(e.getContributor().getName());
String viewId = e.getAttribute("viewId");
Class<? extends IDatasetProvider> datasetType = b.loadClass(e
.getAttribute("datasetProvider"));
...
...
...
}
}
还有3个(可能)相关的警告
There are also 3 (possibly) related warnings
IDatasetProvider is a raw type. References to generic type IDatasetProvider<T> should be parameterized
如果我改回到我们的3.6平台,那么一切都会再次起作用。
If I change back to our 3.6 platform, it all works again.
编辑:由于Alexy和gzukmin的帮助,已修复。
fixed thanks to the help of Alexy and gzukmin.
我使用以下代码,特别是强制转换为 Class< ;?扩展IDatasetProvider>
而不只是 Class
:
I used the following code, specifically casting to Class<? extends IDatasetProvider>
rather than just Class
:
Class<? extends IDatasetProvider> datasetType =
(Class<? extends IDatasetProvider>) b.loadClass(e.getAttribute("datasetProvider"));
如果有任何原因,我应该考虑转换为更通用的 Class
,请让我知道!
If there's any reason I should consider just casting to the more generic Class
, please let me know!
推荐答案
您可以将其转换为原始类型:
You can just cast it to raw type like this:
Class<? extends IDatasetProvider> datasetType =
(Class) b.loadClass(e.getAttribute("datasetProvider"));
请注意:
-
添加有关原始类型和未经检查的强制类型转换的更多警告。
Add more warnings about raw types and unchecked casts.
如果您的类实际上没有扩展,则在运行时爆炸 IDatasetProvider
。而不是在转换位置,而是稍后您尝试实际使用该类时。因此,最好使用
Blow up at the runtime if your class actually turns out not to extend IDatasetProvider
. And not at the cast location, but later when you try to actually use the class. So it might be a good idea to check this with
IDatasetProvider.class.isAssignableFrom(datasetType)
(请参见 Javadoc )。
这篇关于类型不匹配:无法从Class< ..>转换等级< ...>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!