简体   繁体   English

MapStruct:抽象类的麻烦

[英]MapStruct: trouble with abstract class

I have some trouble with MapStruct using abstract class. 我在使用抽象类的MapStruct上遇到了一些麻烦。 I have 2 mappers: 我有2个映射器:

MapperA extends AbstractMapper<U,V>
MapperB extends AbstractMapper<U,V>

MapperA uses MapperB MapperA使用MapperB

I have a method in AbstractMapper 我在AbstractMapper有一个方法

public <T extends AbstractReference> T resolveReference(String id, @TargetType Class<T> entityClass) {
    // Some implementation
}

While clean install, i got the ambiguous method error. 全新安装时,我得到了模棱两可的方法错误。

It seems that Mapstruct find the method twice, one frome each Mapper that extends the same class. 似乎Mapstruct找到了两次该方法,每个扩展相同类的Mapper都有一个。

I did some search on the qualifier thing but it seems usefull when using different methods with the same signature. 我对限定符进行了一些搜索,但在使用具有相同签名的不同方法时似乎很有用。 But in my case, it's the same one!! 但是就我而言,是同一个人!

If you have any ints. 如果您有任何整数。

Thanks 谢谢

Edit: 编辑:

    @Mapper(componentModel = "cdi", uses = {MapperB.class}) 
    @ApplicationScoped 
    public abstract class MapperA extends AbstractMapper<U1,V1> {} 

MapperB does not use any other mapper. MapperB不使用任何其他映射器。

    @Mapper(componentModel = "cdi")
    @ApplicationScoped
    public abstract class MapperB extends AbstractMapper<U2,V2> {}

You don't actually have one method, you have 2 methods. 您实际上没有一种方法,只有两种方法。 One in MapperA and another one in MapperB . 一个在MapperA ,另一个在MapperB MapStruct does not care about the inheritance and where each method is located. MapStruct并不关心继承以及每个方法的位置。

Also your method 还有你的方法

public <T extends AbstractReference> T resolveReference(String id, @TargetType Class<T> entityClass) {
    // Some implementation
}

is a generic one that has the same erasure and does not depend on the AbstractMapper generic parameters, which leads to the ambiguous method error by MapStruct. 是具有相同擦除并且不依赖于AbstractMapper通用参数的通用变量,这导致MapStruct产生模棱两可的方法错误。

I am not sure what you are actually doing in the method. 我不确定您实际上在用这种方法做什么。 But if it is something generic that does not depend on the AbstractMapper generic parameters U and V , then I would suggest that you extract this method to a different class and add that class to the uses annotation variable. 但是,如果它是某种不依赖AbstractMapper通用参数UV通用对象,那么我建议您将该方法提取到另一个类中,然后将该类添加到uses注释变量中。

If the method is dependent on the parameters then make sure that they are part of it, then MapStruct will work correctly as they would have a different type during compilation ( U1, V1 or U2, V2 ) 如果方法依赖于参数,请确保它们是参数的一部分,然后MapStruct将正常工作,因为它们在编译期间具有不同的类型( U1, V1U2, V2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM