类型不匹配:无法从类

时间:2022-02-28 10:22:28

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 而不仅仅是类:

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!

如果有任何原因,我应该考虑选择更通用的类,请让我知道!

1 个解决方案

#1


5  

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,则在运行时进行放大。不是在cast位置,而是在稍后您尝试实际使用类时。所以检查一下这个可能是个好主意

    IDatasetProvider.class.isAssignableFrom(datasetType)
    

(see Javadoc).

(见Javadoc)。

#1


5  

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,则在运行时进行放大。不是在cast位置,而是在稍后您尝试实际使用类时。所以检查一下这个可能是个好主意

    IDatasetProvider.class.isAssignableFrom(datasetType)
    

(see Javadoc).

(见Javadoc)。