Apologies for the delay getting back to you on this.
The error I am seeing from the example code below is about an unmapped target property. This is created as I have only partially mapped the fields in the mapper, leaving the mapping of two source fields into one target field to an @AfterMapping.
...../src/test/java/mapstruct/InternalMapper.java:14: warning: Unmapped target property: "fullName".
package mapstruct;
class DTO {
private int age;
private String first;
private String last;
public DTO(int age, String first, String last) {
this.age = age;
this.first = first;
this.last = last;
}
public int getAge() {
return age;
}
public String getFirst() {
return first;
}
public String getLast() {
return last;
}
}
package mapstruct;
class Internal
{
private int ageInYears;
private String fullName;
public Internal() {
}
public void setAgeInYears(int ageInYears) {
this.ageInYears = ageInYears;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public int getAgeInYears() {
return ageInYears;
}
public String getFullName() {
return fullName;
}
}
package mapstruct;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
@Mapper(uses = NameMapper.class)
public interface InternalMapper {
@Mappings(
{
@Mapping(source = "age", target = "ageInYears")
}
)
Internal internalFromDTO(DTO dto);
}
package mapstruct;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
@Mapper
public abstract class NameMapper {
@AfterMapping
void after(DTO dto, @MappingTarget Internal internal)
{
internal.setFullName(String.format("%s, %s", dto.getLast(), dto.getFirst()));
}
}
package mapstruct;
import org.junit.Test;
import org.mapstruct.factory.Mappers;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
/**
* Generates warning about unmapped target field, but have used @AfterMapping to set this field
* .../src/test/java/mapstruct/InternalMapper.java:14: warning: Unmapped target property: "fullName".
Internal internalFromDTO(DTO dto);
^
*/
public class UnusedSourceFieldsTests {
@Test
public void causesUnusedSourceFieldWarnings()
{
final InternalMapper mapper = Mappers.getMapper(InternalMapper.class);
final DTO dto = new DTO(20, "Wish", "iWas");
final Internal internal = mapper.internalFromDTO(dto);
assertThat(internal.getFullName(), is("iWas, Wish"));
}
}