java 泛型:无法从 <capture#1-of 转换?将 Object,D> 扩展到 <S,D>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27133142/
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-11-02 11:18:00  来源:igfitidea点击:

Generics: cannot convert from <capture#1-of ? extends Object,D> to <S,D>

javagenerics

提问by Daniel Rusev

I have the following class structure:

我有以下类结构:

public interface CopyMapper<S, D> {
    public D map(S sourceObject);
}

public interface CopyMapperFactory {
    public <S, D> CopyMapper<S, D> getMapper(Class<S> sourceClass, Class<D> destinationClass);
}

public class Mapper {
    public <S, D> D map(S source, Class<D> destinationClass) {
        //This is where I get compile time error
        CopyMapper<S, D> copyMapper = mapperFactory.getMapper(source.getClass(), destinationClass);
        return copyMapper.map(source);
    }

My Eclipse compilator gives me the following error:

我的 Eclipse 编译器给了我以下错误:

Type mismatch: cannot convert from CopyMapper<capture#1-of ? extends Object,D> to CopyMapper<S,D>

As far as I know, all generic types extend Object, so I don't see where the problem is?

据我所知,所有泛型类型都扩展了Object,所以我看不出问题出在哪里?

We are trying to preserve an interface. This is the original method of the interface:

我们试图保留一个接口。这是接口的原始方法:

<T> T map(Object source, Class<T> destinationClass)

I tweaked it a little bit so that the classes that use the interface don't get affected:

我稍微调整了一下,以便使用该接口的类不会受到影响:

<S, D> D map(S source, Class<D> destinationClass);

Basically, we are mapping Pojo's, we've been using DozerMapper, but now, the major architect wants compile time safety, and the DozerMapper isn't. For example if a pojo's field gets updated (renamed, deleted) we need to manually update the xml, that describes the mapping between the pojo's (the xml is used in case of nontrivial mapping, for example, when the names of fields of the pojo's don't correspond completely, which is often the case)

基本上,我们正在映射 Pojo,我们一直在使用 DozerMapper,但是现在,主要架构师想要编译时安全,而 DozerMapper 不是。例如,如果 pojo 的字段被更新(重命名、删除),我们需要手动更新 xml,它描述了 pojo 之间的映射(xml 用于非平凡映射,例如,当 pojo 的字段名称不完全对应,这种情况经常发生)

Now, we have copy classes, hundreds of them, one for each mapping between pojo's. We are trying to use the Factory Design patter to return a specific mapper class (implementing the CopyMapperinterface) based on the source class and destination class.

现在,我们有复制类,数百个,一个用于 pojo 之间的每个映射。我们正在尝试使用工厂设计模式CopyMapper根据源类和目标类返回特定的映射器类(实现接口)。

采纳答案by Tobías

The getClassmethod returns Class<?>and not Class<S>, as I think you are expecting. See Object#getClassin the API.

getClass方法返回Class<?>而不是Class<S>,正如我认为您所期望的那样。Object#getClass在 API 中查看。

As it returns Class<?>you lose type information, so you really have this:

当它返回时,Class<?>你会丢失类型信息,所以你真的有这个:

CopyMapper<?, D> copyMapper = mapperFactory.getMapper(source.getClass(), destinationClass);

You know source class is Sso I think you can safely add a cast, but you will get a warning:

您知道源类是S这样,我认为您可以安全地添加演员表,但您会收到警告:

CopyMapper<S, D> copyMapper = mapperFactory.getMapper((Class<S>)source.getClass(), destinationClass);