publicissapient-france/selma

mapping multiple generic fields of different data-types gives type-erasure error

nikhilvjain opened this issue · 2 comments

Hi,
I'm using Selma 1.0 and I'm getting type-erasure error for the below mapping.
There's a simple generic class,

public class Value<T> {
	private T value;
	private String unit;
        //getter, setter
}

And I'm using the above class with multiple fields, which looks like,

public class Records {
	private Value<String> type;
	private Value<Double> speed;
        //getter, setter
}

And my mapper is,

@Mapper(withIoC = IoC.SPRING)
public interface RecordMapper {
	RecordsDAO asRecordsDAO(Records records);
}

And here is the Selma generated Class,

// GENERATED BY S3LM4
@org.springframework.stereotype.Service("")
public final class RecordMapperSelmaGeneratedClass
    implements RecordMapper {

  @Override
  public final RecordsDAO asRecordsDAO(Records inRecords) {
    demo.RecordsDAO out = null;
    if (inRecords != null) {
      out = new demo.RecordsDAO();
      out.setSpeed(asValue(inRecords.getSpeed()));
      out.setType(asValue(inRecords.getType()));
    }
    return out;
  }

  public final Value<String> asValue(Value<String> inString) {
    demo.Value<java.lang.String> out = null;
    if (inString != null) {
      out = new demo.Value<java.lang.String>();
      out.setUnit(inString.getUnit());
      out.setValue(inString.getValue());
    }
    return out;
  }

  public final Value<Double> asValue(Value<Double> inDouble) {
    demo.Value<java.lang.Double> out = null;
    if (inDouble != null) {
      out = new demo.Value<java.lang.Double>();
      out.setUnit(inDouble.getUnit());
      out.setValue(inDouble.getValue());
    }
    return out;
  }

  /**
   * Single constructor
   */
  public RecordMapperSelmaGeneratedClass() {
  }
}

The above conversion complains that,
SelmaRecordMapperSelmaGeneratedClass.java:[30,30] name clash: asValue(Value<java.lang.Double>) and asValue(Value<java.lang.String>) have the same erasure

This can be solved by adding CustomMapper but i've many such fields and dataTypes which clashes, so do we have a simple solution to this kind of problem?
for eg, instead of generating method name asValue(), can it also include the dataType along?
like asValueDouble(), asValueString() which can help in differentiating the methods?

Hi, I've just commited a fix extending the name of the generated in such case.
It will not work in all cases because the type argument is not properly resolved but it should work for you.

Thanks @slemesle , it works like charm 👍