В настоящее время есть две функции, которые можно было бы заставить работать вместе:
- только @Context можно передавать другим методы сопоставления, но его нельзя использовать в качестве источник.
- вторичный параметр (не @Context) может использоваться в качестве источника, но он не передается другим методам сопоставления.
Пример:
Код: Выделить всё
// source classes : `Instant timestamp` is a field I obtain separately
Instant timestamp;
class WrapperSource
List nested;
class NestedSource
String name;
// target classes : I want to map the nested and name field but also to insert the timestamp in both the WrapperTarget and every NestedTarget in the nested list
class WrapperTarget
Instant timestamp;
List nested;
class NestedTarget
String name;
Instant timestamp;
Код: Выделить всё
// Currently this doesn't work because we can't reference the @Context in the source attribute
@Mapping(target = "nested", source="source.nested")
@Mapping(target = "timestamp", source="timestamp")
WrapperTarget map(WrapperSource source, @Context Instant timestamp);
@Mapping(target = "name", source="source.name")
@Mapping(target = "timestamp", source="timestamp")
NestedTarget map(NestedSource source, @Context Instant timestamp);
Код: Выделить всё
// Currently this doesn't work because the second method with 2 sources in not called by the first generated method
@Mapping(target = "nested", source="source.nested")
@Mapping(target = "timestamp", source="timestamp")
WrapperTarget map(WrapperSource source, Instant timestamp);
@Mapping(target = "name", source="source.name")
@Mapping(target = "timestamp", source="timestamp")
NestedTarget map(NestedSource source, Instant timestamp);
Код: Выделить всё
// @Context is passed around and I can manually use it as a source in an @AfterMapping but it requires additional code
WrapperTarget map(WrapperSource source, @Context Instant timestamp);
@AfterMapping
void map(WrapperSource source, @MappingTarget WrapperTarget target, @Context Instant timestamp) {
target.setTimestamp(timestamp);
}
NestedTarget map(NestedSource source, @Context Instant timestamp);
@AfterMapping
void map(NestedSource source, @MappingTarget NestedTarget target, @Context Instant timestamp) {
target.setTimestamp(timestamp);
}
Есть ли лучший обходной путь для этой проблемы?
Подробнее здесь: https://stackoverflow.com/questions/637 ... attributes
Мобильная версия