title: Mapstruct: How to map A extends SingleValue<T> to T and A extends SingleValue<T> to B extends SingleValue<T> with the same mapper?
body: <p>I've been staring at this problem for quite some time and have been unable to find a answer. I'm trying to use mapstruct to map objects that have fields that are implementations of a generic SingleValue objects. </p>
  
  <p>Let me illustrate the problem with a simplified example. (throws are omitted for readability)</p>
  
  <p>We have a Single Value object:</p>
  
  <pre><code>public abstract class SingleValue&lt;T extends Serializable&gt; implements Serializable {
      private T value;
  
      public SingleValue(T value) {
          this.value = value;
      }
  }
  </code></pre>
  
  <p>The concrete Single value class would look like this</p>
  
  <pre><code>public class FirstName extends SingleValue&lt;String&gt;{
  
  }
  </code></pre>
  
  <p>And would be used in different objects that might be mapped to each other</p>
  
  <p>Mappings that should be possible: </p>
  
  <ul>
  <li>String -> FirstName</li>
  <li>FirstName -> String</li>
  <li>FirstName -> another extends SingleValue object</li>
  </ul>
  
  <p>The following mappings are possible with the current solution:</p>
  
  <ul>
  <li>String -> FirstName</li>
  <li>FirstName -> String</li>
  </ul>
  
  <p>Current Single value mapper</p>
  
  <pre><code>@Mapper
  public interface SingleValueMapper {
      SingleValueMapper INSTANCE = Mappers.getMapper(SingleValueMapper.class);
      default &lt;Z extends Serializable, T extends SingleValue&lt;Z&gt;&gt; T singleValue(Z obj, Class&lt;Z&gt; objClass, @TargetType Class&lt;T&gt; targetType) {
          if (obj == null) {
              return null;
          }
          return targetType.getConstructor(objClass).newInstance(obj);
      }
  
      default &lt;T extends Serializable, Z extends SingleValue&lt;T&gt;&gt; T getValue(Z singleValue) {
          if (singleValue == null) {
              return null;
          }
          return singleValue.getValue();
      }
  
      default &lt;T extends SingleValue&lt;String&gt;&gt; T singleValue(String obj, @TargetType Class&lt;T&gt; targetType) {
          return singleValue(obj, String.class, targetType);
  
      }
  }
  </code></pre>
  
  <p>For FirstName -> another extends SingleValue object mapping i came up with the following solution that did not work:</p>
  
  <pre><code>default &lt;T extends SingleValue&lt;String&gt;&gt; T stringSingleValue(SingleValue&lt;String&gt; obj, @TargetType Class&lt;T&gt; targetType)
          throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
      return singleValue(obj.getValue(), String.class, targetType);
  }
  </code></pre>
  
  <p>Problems I have with mapping method mentioned above:</p>
  
  <ul>
  <li>Mapstruct will prefer the getValue(Z singleValue) mapping over this one in automatic selection (I really do not wan't to use Qualifiers because then i need to qualify each field that im mapping)</li>
  <li>Mapstruct will see the mapping method as ambiguous if we for instance also add one for UUID single values</li>
  </ul>
  
  <p>works this call is generated by mapstruct when mapping a String to  A extends SingleValue:</p>
  
  <pre><code>A a = singleValueMapper.singleValue( client.getCompanyName(), A.class )
  </code></pre>
  
  <p>This call is not chosen by mapstruct but we can use it when manually use it with @Mapping as a expression. However we would like mapstruct to automatically map these calls when mapping A extends SingleValue to B extends SingleValue:</p>
  
  <pre><code>B b =singleValueMapper.stringSingleValue(command.getTitle(), B.class)
  </code></pre>
  
  <p>Is there a way to pull this off with mapstruct and java generics?</p>
  
  <p>I'm using Mapstruct version 1.3.0-final.</p>