java 类型不匹配:无法从 Class <..> 转换为 Class <...>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11363788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:46:43  来源:igfitidea点击:

Type mismatch: cannot convert from Class <..> to Class <...>

javaeclipse-rcp

提问by Jonny

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

我已经“继承”了一个 Eclipse RCP 项目,它似乎在基于 3.6 的目标平台集上运行良好。但是,继续前进,我们需要更新到最新版本的平台,但是当我将目标平台更改为 3.7(或 4.2)时,我遇到了一些错误

Type mismatch: cannot convert from Class<capture#1-of ?> to Class<? extends IDatasetProvider>

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!

任何人都可以建议/解释为什么这可能在 3.6 中正常工作,但在 3.7(及更高版本)中不起作用?关于我从哪里开始解决这个问题的任何想法也会很棒!

A snippet of the code causing this error (which appears at the b.loadClass part):

导致此错误的代码片段(出现在 b.loadClass 部分):

    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"));
            ...
            ...
            ...
        }
     }

There are also 3 (possibly) related warnings

还有 3 个(可能)相关的警告

 IDatasetProvider is a raw type. References to generic type IDatasetProvider<T> should be parameterized 

If I change back to our 3.6 platform, it all works again.

如果我改回我们的 3.6 平台,它会再次运行。

EDIT: fixed thanks to the help of Alexy and gzukmin.

编辑:在 Alexy 和 gzukmin 的帮助下修复。

I used the following code, specifically casting to Class<? extends IDatasetProvider>rather than just Class:

我使用了以下代码,特别是强制转换为,Class<? extends IDatasetProvider>而不仅仅是Class

Class<? extends IDatasetProvider> datasetType = 
    (Class<? extends IDatasetProvider>) b.loadClass(e.getAttribute("datasetProvider"));

If there's any reason I should consider just casting to the more generic Class, please let me know!

如果有任何理由我应该考虑只转换为更通用的Class,请告诉我!

回答by Alexey Romanov

You can just cast it to raw type like this:

您可以像这样将其转换为原始类型:

Class<? extends IDatasetProvider> datasetType = 
    (Class) b.loadClass(e.getAttribute("datasetProvider"));

Note this will:

请注意,这将:

  1. Add more warnings about raw types and unchecked casts.

  2. 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)
    
  1. 添加有关原始类型和未经检查的强制转换的更多警告。

  2. 如果您的类实际上没有扩展,则在运行时爆炸IDatasetProvider。而不是在演员位置,而是在您尝试实际使用该课程时。所以检查这个可能是个好主意

    IDatasetProvider.class.isAssignableFrom(datasetType)
    

(see Javadoc).

(请参阅Javadoc)。