Код: Выделить всё
public class RewardClaim {
private PlayerEntity owner; // PlayerEntity extends Entity which has a .getId()
}
Код: Выделить всё
public class RewardClaimResponseModel {
private String ownerId;
}
Код: Выделить всё
Converter entityToEntityIdConverter = context -> {
Entity entity = context.getSource();
return entity != null ? entity.getId() : null;
};
modelMapper.typeMap(RewardClaim.class, RewardClaimResponseModel.class)
.addMappings(mapper -> mapper.using(entityToEntityIdConverter)
.map(RewardClaim::getOwner, RewardClaimResponseModel::setOwnerId));
Код: Выделить всё
private UserEntity user; -> private String userId; // Another example
Код: Выделить всё
modelMapper.typeMap(Object.class, Object.class)
.addMappings(mapper -> mapper.using(entityToEntityIdConverter)
.map(FIND HERE THE get FUNCTION, FIND HERE THE set FUNCTION));
Одна из последних попыток здесь, но нет никакого представления о том, как получить исходный класс и целевой класс.
Код: Выделить всё
Converter entityToEntityIdConverter = context -> {
Entity entity = context.getSource();
return entity != null ? entity.getId() : null;
};
modelMapper.typeMap(Object.class, Object.class).addMappings(mapper -> {
Class sourceClass = null; //TODO: HOW TO GET THIS DYNAMICALLY?
Class destinationClass = null; //TODO: HOW TO GET THIS DYNAMICALLY?
for (Method setMethod : destinationClass.getDeclaredMethods()) {
if (setMethod.getName().endsWith("Id") && setMethod.getReturnType() == String.class) {
String propertyName = setMethod.getName().substring(3, setMethod.getName().length() - 2); // Remove "set" and "id"
try {
Method getMethod = sourceClass.getMethod("get" + propertyName, sourceClass);
mapper.using(entityToEntityIdConverter).map(src -> {
// Try catch here removed for simplicity
return getMethod.invoke(src);
}, (dest, v) -> {
setMethod.invoke(dest, v);
});
} catch (NoSuchMethodException e) {
// Setter method not found, ignore
}
}
}
});
Подробнее здесь: https://stackoverflow.com/questions/787 ... odelmapper
Мобильная версия