use of org.mapstruct.ap.internal.model.common.FormattingParameters in project mapstruct by mapstruct.
the class IterableMapping method fromPrism.
public static IterableMapping fromPrism(IterableMappingPrism iterableMapping, ExecutableElement method, FormattingMessager messager, Types typeUtils) {
if (iterableMapping == null) {
return null;
}
boolean elementTargetTypeIsDefined = !TypeKind.VOID.equals(iterableMapping.elementTargetType().getKind());
NullValueMappingStrategyPrism nullValueMappingStrategy = iterableMapping.values.nullValueMappingStrategy() == null ? null : NullValueMappingStrategyPrism.valueOf(iterableMapping.nullValueMappingStrategy());
if (!elementTargetTypeIsDefined && iterableMapping.dateFormat().isEmpty() && iterableMapping.numberFormat().isEmpty() && iterableMapping.qualifiedBy().isEmpty() && iterableMapping.qualifiedByName().isEmpty() && (nullValueMappingStrategy == null)) {
messager.printMessage(method, Message.ITERABLEMAPPING_NO_ELEMENTS);
}
SelectionParameters selection = new SelectionParameters(iterableMapping.qualifiedBy(), iterableMapping.qualifiedByName(), elementTargetTypeIsDefined ? iterableMapping.elementTargetType() : null, typeUtils);
FormattingParameters formatting = new FormattingParameters(iterableMapping.dateFormat(), iterableMapping.numberFormat(), iterableMapping.mirror, iterableMapping.values.dateFormat(), method);
return new IterableMapping(formatting, selection, iterableMapping.mirror, nullValueMappingStrategy);
}
use of org.mapstruct.ap.internal.model.common.FormattingParameters in project mapstruct by mapstruct.
the class MapperCreationProcessor method createWithElementMappingMethod.
private <M extends ContainerMappingMethod> M createWithElementMappingMethod(SourceMethod method, MappingOptions mappingOptions, ContainerMappingMethodBuilder<?, M> builder) {
FormattingParameters formattingParameters = null;
SelectionParameters selectionParameters = null;
NullValueMappingStrategyPrism nullValueMappingStrategy = null;
if (mappingOptions.getIterableMapping() != null) {
formattingParameters = mappingOptions.getIterableMapping().getFormattingParameters();
selectionParameters = mappingOptions.getIterableMapping().getSelectionParameters();
nullValueMappingStrategy = mappingOptions.getIterableMapping().getNullValueMappingStrategy();
}
return builder.mappingContext(mappingContext).method(method).formattingParameters(formattingParameters).selectionParameters(selectionParameters).nullValueMappingStrategy(nullValueMappingStrategy).build();
}
use of org.mapstruct.ap.internal.model.common.FormattingParameters in project mapstruct by mapstruct.
the class MapperCreationProcessor method getMappingMethods.
private List<MappingMethod> getMappingMethods(MapperConfiguration mapperConfig, List<SourceMethod> methods) {
List<MappingMethod> mappingMethods = new ArrayList<MappingMethod>();
for (SourceMethod method : methods) {
if (!method.overridesMethod()) {
continue;
}
mergeInheritedOptions(method, mapperConfig, methods, new ArrayList<SourceMethod>());
MappingOptions mappingOptions = method.getMappingOptions();
boolean hasFactoryMethod = false;
if (method.isIterableMapping()) {
IterableMappingMethod iterableMappingMethod = createWithElementMappingMethod(method, mappingOptions, new IterableMappingMethod.Builder());
hasFactoryMethod = iterableMappingMethod.getFactoryMethod() != null;
mappingMethods.add(iterableMappingMethod);
} else if (method.isMapMapping()) {
MapMappingMethod.Builder builder = new MapMappingMethod.Builder();
SelectionParameters keySelectionParameters = null;
FormattingParameters keyFormattingParameters = null;
SelectionParameters valueSelectionParameters = null;
FormattingParameters valueFormattingParameters = null;
NullValueMappingStrategyPrism nullValueMappingStrategy = null;
if (mappingOptions.getMapMapping() != null) {
keySelectionParameters = mappingOptions.getMapMapping().getKeySelectionParameters();
keyFormattingParameters = mappingOptions.getMapMapping().getKeyFormattingParameters();
valueSelectionParameters = mappingOptions.getMapMapping().getValueSelectionParameters();
valueFormattingParameters = mappingOptions.getMapMapping().getValueFormattingParameters();
nullValueMappingStrategy = mappingOptions.getMapMapping().getNullValueMappingStrategy();
}
MapMappingMethod mapMappingMethod = builder.mappingContext(mappingContext).method(method).keyFormattingParameters(keyFormattingParameters).keySelectionParameters(keySelectionParameters).valueFormattingParameters(valueFormattingParameters).valueSelectionParameters(valueSelectionParameters).nullValueMappingStrategy(nullValueMappingStrategy).build();
hasFactoryMethod = mapMappingMethod.getFactoryMethod() != null;
mappingMethods.add(mapMappingMethod);
} else if (method.isValueMapping()) {
// prefer value mappings over enum mapping
ValueMappingMethod valueMappingMethod = new ValueMappingMethod.Builder().mappingContext(mappingContext).method(method).valueMappings(mappingOptions.getValueMappings()).build();
mappingMethods.add(valueMappingMethod);
} else if (method.isEnumMapping()) {
messager.printMessage(method.getExecutable(), Message.ENUMMAPPING_DEPRECATED);
EnumMappingMethod.Builder builder = new EnumMappingMethod.Builder();
MappingMethod enumMappingMethod = builder.mappingContext(mappingContext).souceMethod(method).build();
if (enumMappingMethod != null) {
mappingMethods.add(enumMappingMethod);
}
} else if (method.isStreamMapping()) {
StreamMappingMethod streamMappingMethod = createWithElementMappingMethod(method, mappingOptions, new StreamMappingMethod.Builder());
// If we do StreamMapping that means that internally there is a way to generate the result type
hasFactoryMethod = streamMappingMethod.getFactoryMethod() != null || method.getResultType().isStreamType();
mappingMethods.add(streamMappingMethod);
} else {
NullValueMappingStrategyPrism nullValueMappingStrategy = null;
SelectionParameters selectionParameters = null;
if (mappingOptions.getBeanMapping() != null) {
nullValueMappingStrategy = mappingOptions.getBeanMapping().getNullValueMappingStrategy();
selectionParameters = mappingOptions.getBeanMapping().getSelectionParameters();
}
BeanMappingMethod.Builder builder = new BeanMappingMethod.Builder();
BeanMappingMethod beanMappingMethod = builder.mappingContext(mappingContext).souceMethod(method).nullValueMappingStrategy(nullValueMappingStrategy).selectionParameters(selectionParameters).build();
if (beanMappingMethod != null) {
// We can consider that the bean mapping method can always be constructed. If there is a problem
// it would have been reported in its build
hasFactoryMethod = true;
mappingMethods.add(beanMappingMethod);
}
}
if (!hasFactoryMethod) {
// A factory method is allowed to return an interface type and hence, the generated
// implementation as well. The check below must only be executed if there's no factory
// method that could be responsible.
reportErrorIfNoImplementationTypeIsRegisteredForInterfaceReturnType(method);
}
}
return mappingMethods;
}
use of org.mapstruct.ap.internal.model.common.FormattingParameters in project mapstruct by mapstruct.
the class MapMapping method fromPrism.
public static MapMapping fromPrism(MapMappingPrism mapMapping, ExecutableElement method, FormattingMessager messager, Types typeUtils) {
if (mapMapping == null) {
return null;
}
NullValueMappingStrategyPrism nullValueMappingStrategy = mapMapping.values.nullValueMappingStrategy() == null ? null : NullValueMappingStrategyPrism.valueOf(mapMapping.nullValueMappingStrategy());
boolean keyTargetTypeIsDefined = !TypeKind.VOID.equals(mapMapping.keyTargetType().getKind());
boolean valueTargetTypeIsDefined = !TypeKind.VOID.equals(mapMapping.valueTargetType().getKind());
if (mapMapping.keyDateFormat().isEmpty() && mapMapping.keyNumberFormat().isEmpty() && mapMapping.keyQualifiedBy().isEmpty() && mapMapping.keyQualifiedByName().isEmpty() && mapMapping.valueDateFormat().isEmpty() && mapMapping.valueNumberFormat().isEmpty() && mapMapping.valueQualifiedBy().isEmpty() && mapMapping.valueQualifiedByName().isEmpty() && !keyTargetTypeIsDefined && !valueTargetTypeIsDefined && (nullValueMappingStrategy == null)) {
messager.printMessage(method, Message.MAPMAPPING_NO_ELEMENTS);
}
SelectionParameters keySelection = new SelectionParameters(mapMapping.keyQualifiedBy(), mapMapping.keyQualifiedByName(), keyTargetTypeIsDefined ? mapMapping.keyTargetType() : null, typeUtils);
SelectionParameters valueSelection = new SelectionParameters(mapMapping.valueQualifiedBy(), mapMapping.valueQualifiedByName(), valueTargetTypeIsDefined ? mapMapping.valueTargetType() : null, typeUtils);
FormattingParameters keyFormatting = new FormattingParameters(mapMapping.keyDateFormat(), mapMapping.keyNumberFormat(), mapMapping.mirror, mapMapping.values.keyDateFormat(), method);
FormattingParameters valueFormatting = new FormattingParameters(mapMapping.valueDateFormat(), mapMapping.valueNumberFormat(), mapMapping.mirror, mapMapping.values.valueDateFormat(), method);
return new MapMapping(keyFormatting, keySelection, valueFormatting, valueSelection, mapMapping.mirror, nullValueMappingStrategy);
}
use of org.mapstruct.ap.internal.model.common.FormattingParameters in project mapstruct by mapstruct.
the class Mapping method fromMappingPrism.
public static Mapping fromMappingPrism(MappingPrism mappingPrism, ExecutableElement element, FormattingMessager messager, Types typeUtils) {
if (mappingPrism.target().isEmpty()) {
messager.printMessage(element, mappingPrism.mirror, mappingPrism.values.target(), Message.PROPERTYMAPPING_EMPTY_TARGET);
return null;
}
if (!mappingPrism.source().isEmpty() && mappingPrism.values.constant() != null) {
messager.printMessage(element, Message.PROPERTYMAPPING_SOURCE_AND_CONSTANT_BOTH_DEFINED);
return null;
} else if (!mappingPrism.source().isEmpty() && mappingPrism.values.expression() != null) {
messager.printMessage(element, Message.PROPERTYMAPPING_SOURCE_AND_EXPRESSION_BOTH_DEFINED);
return null;
} else if (mappingPrism.values.expression() != null && mappingPrism.values.constant() != null) {
messager.printMessage(element, Message.PROPERTYMAPPING_EXPRESSION_AND_CONSTANT_BOTH_DEFINED);
return null;
} else if (mappingPrism.values.expression() != null && mappingPrism.values.defaultValue() != null) {
messager.printMessage(element, Message.PROPERTYMAPPING_EXPRESSION_AND_DEFAULT_VALUE_BOTH_DEFINED);
return null;
} else if (mappingPrism.values.constant() != null && mappingPrism.values.defaultValue() != null) {
messager.printMessage(element, Message.PROPERTYMAPPING_CONSTANT_AND_DEFAULT_VALUE_BOTH_DEFINED);
return null;
} else if (mappingPrism.values.expression() != null && mappingPrism.values.defaultExpression() != null) {
messager.printMessage(element, Message.PROPERTYMAPPING_EXPRESSION_AND_DEFAULT_EXPRESSION_BOTH_DEFINED);
return null;
} else if (mappingPrism.values.constant() != null && mappingPrism.values.defaultExpression() != null) {
messager.printMessage(element, Message.PROPERTYMAPPING_CONSTANT_AND_DEFAULT_EXPRESSION_BOTH_DEFINED);
return null;
} else if (mappingPrism.values.defaultValue() != null && mappingPrism.values.defaultExpression() != null) {
messager.printMessage(element, Message.PROPERTYMAPPING_DEFAULT_VALUE_AND_DEFAULT_EXPRESSION_BOTH_DEFINED);
return null;
}
String source = mappingPrism.source().isEmpty() ? null : mappingPrism.source();
String constant = mappingPrism.values.constant() == null ? null : mappingPrism.constant();
String expression = getExpression(mappingPrism, element, messager);
String defaultExpression = getDefaultExpression(mappingPrism, element, messager);
String dateFormat = mappingPrism.values.dateFormat() == null ? null : mappingPrism.dateFormat();
String numberFormat = mappingPrism.values.numberFormat() == null ? null : mappingPrism.numberFormat();
String defaultValue = mappingPrism.values.defaultValue() == null ? null : mappingPrism.defaultValue();
boolean resultTypeIsDefined = mappingPrism.values.resultType() != null;
List<String> dependsOn = mappingPrism.dependsOn() != null ? mappingPrism.dependsOn() : Collections.<String>emptyList();
FormattingParameters formattingParam = new FormattingParameters(dateFormat, numberFormat, mappingPrism.mirror, mappingPrism.values.dateFormat(), element);
SelectionParameters selectionParams = new SelectionParameters(mappingPrism.qualifiedBy(), mappingPrism.qualifiedByName(), resultTypeIsDefined ? mappingPrism.resultType() : null, typeUtils);
return new Mapping(source, constant, expression, defaultExpression, mappingPrism.target(), defaultValue, mappingPrism.ignore(), mappingPrism.mirror, mappingPrism.values.source(), mappingPrism.values.target(), formattingParam, selectionParams, mappingPrism.values.dependsOn(), dependsOn);
}
Aggregations