1

I have a source class that defines string attributes as CharSequence (unfortunately).

So the following:

@Mapper(source="charSeq", target="str")

gives me:

Can't map property "java.lang.CharSequence charSeq" to "java.lang.String str". Consider to declare/implement a mapping method: "java.lang.String map(java.lang.CharSequence value)"

How can I implement this mapper method and make it available to all my mappers so that I do it once and for all?

Phate
  • 6,066
  • 15
  • 73
  • 138

1 Answers1

4

Create a String-CharSequence mapper:

@Mapper
public interface CharSequenceMapper {
    default String map(CharSequence charSequence) {
        return charSequence.toString();
    }

    default CharSequence map(String string) {
        return string;
    }
}

And use it with your mapper:

@Mapper(uses = CharSequenceMapper.class)
public interface MyMapper {
    // some code
}

IMHO CharSequence-String conversion should be built into the framework. Consider filing a a feature request at https://github.com/mapstruct/mapstruct/issues.

jannis
  • 4,843
  • 1
  • 23
  • 53