当目标具有与源同名的变量时,带有自定义源和目标变量的 @Mapping 无法按预期工作

@Mapping with custom source and target variable is not working as expected when target has variable with same name as source

我有下面的源和目标 classes,我正在使用 lombok 来生成 getter 和 setter

public class Target {
    private String name;
    private String newName;
}

public class Source {
    private String name;
}

假设我想将 Source.name 映射到 Target.newName 我在下面使用 Mapper class 和 @Mapping 来指定源和目标变量。

但是一旦我编译代码并检查生成的 ClassMapperImpl 它映射 Source.name 到 Target.name 而不是 Target.new Name

@Mapper
public interface ClassMapper {

    @Mapping(source = "name", target = "newName")
    Target sourceToTarget(Source s);
}

我想当我尝试时它们都被映射了:

public class ClassMapperImpl implements ClassMapper {

    @Override
    public Target sourceToTarget(Source s) {
        if ( s == null ) {
            return null;
        }

        Target target = new Target();

        target.setNewName( s.getName() );
        target.setName( s.getName() );

        return target;
    }
}

请忽略 name 属性。

@Mapper
public interface ClassMapper {

    @Mapping(source = "name", target = "newName")
    @Mapping(ignore = true, target = "name")
    Target sourceToTarget(Source s);
}