Search in sources :

Example 6 with FormattingParameters

use of org.mapstruct.ap.internal.model.common.FormattingParameters in project mapstruct by mapstruct.

the class IterableMappingOptions method fromGem.

public static IterableMappingOptions fromGem(IterableMappingGem iterableMapping, MapperOptions mapperOptions, ExecutableElement method, FormattingMessager messager, TypeUtils typeUtils) {
    if (iterableMapping == null || !isConsistent(iterableMapping, method, messager)) {
        IterableMappingOptions options = new IterableMappingOptions(null, null, null, mapperOptions);
        return options;
    }
    SelectionParameters selection = new SelectionParameters(iterableMapping.qualifiedBy().get(), iterableMapping.qualifiedByName().get(), iterableMapping.elementTargetType().getValue(), typeUtils);
    FormattingParameters formatting = new FormattingParameters(iterableMapping.dateFormat().get(), iterableMapping.numberFormat().get(), iterableMapping.mirror(), iterableMapping.dateFormat().getAnnotationValue(), method);
    IterableMappingOptions options = new IterableMappingOptions(formatting, selection, iterableMapping, mapperOptions);
    return options;
}
Also used : FormattingParameters(org.mapstruct.ap.internal.model.common.FormattingParameters)

Example 7 with FormattingParameters

use of org.mapstruct.ap.internal.model.common.FormattingParameters in project mapstruct by mapstruct.

the class MappingOptions method addInstance.

public static void addInstance(MappingGem mapping, ExecutableElement method, BeanMappingOptions beanMappingOptions, FormattingMessager messager, TypeUtils typeUtils, Set<MappingOptions> mappings) {
    if (!isConsistent(mapping, method, messager)) {
        return;
    }
    String source = mapping.source().getValue();
    String constant = mapping.constant().getValue();
    String expression = getExpression(mapping, method, messager);
    String defaultExpression = getDefaultExpression(mapping, method, messager);
    String conditionExpression = getConditionExpression(mapping, method, messager);
    String dateFormat = mapping.dateFormat().getValue();
    String numberFormat = mapping.numberFormat().getValue();
    String defaultValue = mapping.defaultValue().getValue();
    Set<String> dependsOn = mapping.dependsOn().hasValue() ? new LinkedHashSet(mapping.dependsOn().getValue()) : Collections.emptySet();
    FormattingParameters formattingParam = new FormattingParameters(dateFormat, numberFormat, mapping.mirror(), mapping.dateFormat().getAnnotationValue(), method);
    SelectionParameters selectionParams = new SelectionParameters(mapping.qualifiedBy().get(), mapping.qualifiedByName().get(), mapping.conditionQualifiedBy().get(), mapping.conditionQualifiedByName().get(), mapping.resultType().getValue(), typeUtils);
    MappingOptions options = new MappingOptions(mapping.target().getValue(), method, mapping.target().getAnnotationValue(), source, mapping.source().getAnnotationValue(), constant, expression, defaultExpression, conditionExpression, defaultValue, mapping.ignore().get(), formattingParam, selectionParams, dependsOn, mapping, null, beanMappingOptions);
    if (mappings.contains(options)) {
        messager.printMessage(method, Message.PROPERTYMAPPING_DUPLICATE_TARGETS, mapping.target().get());
    } else {
        mappings.add(options);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) FormattingParameters(org.mapstruct.ap.internal.model.common.FormattingParameters)

Example 8 with FormattingParameters

use of org.mapstruct.ap.internal.model.common.FormattingParameters in project mapstruct by mapstruct.

the class MapMappingOptions method fromGem.

public static MapMappingOptions fromGem(MapMappingGem mapMapping, MapperOptions mapperOptions, ExecutableElement method, FormattingMessager messager, TypeUtils typeUtils) {
    if (mapMapping == null || !isConsistent(mapMapping, method, messager)) {
        MapMappingOptions options = new MapMappingOptions(null, null, null, null, null, mapperOptions);
        return options;
    }
    SelectionParameters keySelection = new SelectionParameters(mapMapping.keyQualifiedBy().get(), mapMapping.keyQualifiedByName().get(), mapMapping.keyTargetType().getValue(), typeUtils);
    SelectionParameters valueSelection = new SelectionParameters(mapMapping.valueQualifiedBy().get(), mapMapping.valueQualifiedByName().get(), mapMapping.valueTargetType().getValue(), typeUtils);
    FormattingParameters keyFormatting = new FormattingParameters(mapMapping.keyDateFormat().get(), mapMapping.keyNumberFormat().get(), mapMapping.mirror(), mapMapping.keyDateFormat().getAnnotationValue(), method);
    FormattingParameters valueFormatting = new FormattingParameters(mapMapping.valueDateFormat().get(), mapMapping.valueNumberFormat().get(), mapMapping.mirror(), mapMapping.valueDateFormat().getAnnotationValue(), method);
    MapMappingOptions options = new MapMappingOptions(keyFormatting, keySelection, valueFormatting, valueSelection, mapMapping, mapperOptions);
    return options;
}
Also used : FormattingParameters(org.mapstruct.ap.internal.model.common.FormattingParameters)

Example 9 with FormattingParameters

use of org.mapstruct.ap.internal.model.common.FormattingParameters in project mapstruct by mapstruct.

the class MapperCreationProcessor method getMappingMethods.

private List<MappingMethod> getMappingMethods(MapperOptions mapperAnnotation, List<SourceMethod> methods) {
    List<MappingMethod> mappingMethods = new ArrayList<>();
    for (SourceMethod method : methods) {
        if (!method.overridesMethod()) {
            continue;
        }
        mergeInheritedOptions(method, mapperAnnotation, methods, new ArrayList<>(), null);
        MappingMethodOptions mappingOptions = method.getOptions();
        boolean hasFactoryMethod = false;
        if (method.isIterableMapping()) {
            this.messager.note(1, Message.ITERABLEMAPPING_CREATE_NOTE, method);
            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;
            NullValueMappingStrategyGem 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();
            }
            this.messager.note(1, Message.MAPMAPPING_CREATE_NOTE, method);
            MapMappingMethod mapMappingMethod = builder.mappingContext(mappingContext).method(method).keyFormattingParameters(keyFormattingParameters).keySelectionParameters(keySelectionParameters).valueFormattingParameters(valueFormattingParameters).valueSelectionParameters(valueSelectionParameters).build();
            hasFactoryMethod = mapMappingMethod.getFactoryMethod() != null;
            mappingMethods.add(mapMappingMethod);
        } else if (method.isValueMapping()) {
            // prefer value mappings over enum mapping
            this.messager.note(1, Message.VALUEMAPPING_CREATE_NOTE, method);
            ValueMappingMethod valueMappingMethod = new ValueMappingMethod.Builder().mappingContext(mappingContext).method(method).valueMappings(mappingOptions.getValueMappings()).enumMapping(mappingOptions.getEnumMappingOptions()).build();
            if (valueMappingMethod != null) {
                mappingMethods.add(valueMappingMethod);
            }
        } else if (method.isRemovedEnumMapping()) {
            messager.printMessage(method.getExecutable(), Message.ENUMMAPPING_REMOVED);
        } else if (method.isStreamMapping()) {
            this.messager.note(1, Message.STREAMMAPPING_CREATE_NOTE, method);
            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 {
            this.messager.note(1, Message.BEANMAPPING_CREATE_NOTE, method);
            BuilderGem builder = method.getOptions().getBeanMapping().getBuilder();
            Type userDefinedReturnType = getUserDesiredReturnType(method);
            Type builderBaseType = userDefinedReturnType != null ? userDefinedReturnType : method.getReturnType();
            BeanMappingMethod.Builder beanMappingBuilder = new BeanMappingMethod.Builder();
            BeanMappingMethod beanMappingMethod = beanMappingBuilder.mappingContext(mappingContext).sourceMethod(method).userDefinedReturnType(userDefinedReturnType).returnTypeBuilder(typeFactory.builderTypeFor(builderBaseType, builder)).build();
            // 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;
            if (beanMappingMethod != null) {
                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;
}
Also used : MappingMethodOptions(org.mapstruct.ap.internal.model.source.MappingMethodOptions) MapMappingMethod(org.mapstruct.ap.internal.model.MapMappingMethod) ValueMappingMethod(org.mapstruct.ap.internal.model.ValueMappingMethod) ContainerMappingMethodBuilder(org.mapstruct.ap.internal.model.ContainerMappingMethodBuilder) MapMappingMethod(org.mapstruct.ap.internal.model.MapMappingMethod) IterableMappingMethod(org.mapstruct.ap.internal.model.IterableMappingMethod) ValueMappingMethod(org.mapstruct.ap.internal.model.ValueMappingMethod) MappingMethod(org.mapstruct.ap.internal.model.MappingMethod) StreamMappingMethod(org.mapstruct.ap.internal.model.StreamMappingMethod) ContainerMappingMethod(org.mapstruct.ap.internal.model.ContainerMappingMethod) BeanMappingMethod(org.mapstruct.ap.internal.model.BeanMappingMethod) ArrayList(java.util.ArrayList) StreamMappingMethod(org.mapstruct.ap.internal.model.StreamMappingMethod) BuilderGem(org.mapstruct.ap.internal.gem.BuilderGem) BeanMappingMethod(org.mapstruct.ap.internal.model.BeanMappingMethod) IterableMappingMethod(org.mapstruct.ap.internal.model.IterableMappingMethod) Type(org.mapstruct.ap.internal.model.common.Type) FormattingParameters(org.mapstruct.ap.internal.model.common.FormattingParameters) NullValueMappingStrategyGem(org.mapstruct.ap.internal.gem.NullValueMappingStrategyGem) SelectionParameters(org.mapstruct.ap.internal.model.source.SelectionParameters) SourceMethod(org.mapstruct.ap.internal.model.source.SourceMethod)

Example 10 with FormattingParameters

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, MappingMethodOptions mappingMethodOptions, ContainerMappingMethodBuilder<?, M> builder) {
    FormattingParameters formattingParameters = null;
    SelectionParameters selectionParameters = null;
    if (mappingMethodOptions.getIterableMapping() != null) {
        formattingParameters = mappingMethodOptions.getIterableMapping().getFormattingParameters();
        selectionParameters = mappingMethodOptions.getIterableMapping().getSelectionParameters();
    }
    return builder.mappingContext(mappingContext).method(method).formattingParameters(formattingParameters).selectionParameters(selectionParameters).build();
}
Also used : FormattingParameters(org.mapstruct.ap.internal.model.common.FormattingParameters) SelectionParameters(org.mapstruct.ap.internal.model.source.SelectionParameters)

Aggregations

FormattingParameters (org.mapstruct.ap.internal.model.common.FormattingParameters)10 SelectionParameters (org.mapstruct.ap.internal.model.source.SelectionParameters)4 NullValueMappingStrategyPrism (org.mapstruct.ap.internal.prism.NullValueMappingStrategyPrism)4 ArrayList (java.util.ArrayList)2 BeanMappingMethod (org.mapstruct.ap.internal.model.BeanMappingMethod)2 ContainerMappingMethod (org.mapstruct.ap.internal.model.ContainerMappingMethod)2 ContainerMappingMethodBuilder (org.mapstruct.ap.internal.model.ContainerMappingMethodBuilder)2 IterableMappingMethod (org.mapstruct.ap.internal.model.IterableMappingMethod)2 MapMappingMethod (org.mapstruct.ap.internal.model.MapMappingMethod)2 MappingMethod (org.mapstruct.ap.internal.model.MappingMethod)2 StreamMappingMethod (org.mapstruct.ap.internal.model.StreamMappingMethod)2 ValueMappingMethod (org.mapstruct.ap.internal.model.ValueMappingMethod)2 SourceMethod (org.mapstruct.ap.internal.model.source.SourceMethod)2 LinkedHashSet (java.util.LinkedHashSet)1 BuilderGem (org.mapstruct.ap.internal.gem.BuilderGem)1 NullValueMappingStrategyGem (org.mapstruct.ap.internal.gem.NullValueMappingStrategyGem)1 EnumMappingMethod (org.mapstruct.ap.internal.model.EnumMappingMethod)1 Type (org.mapstruct.ap.internal.model.common.Type)1 MappingMethodOptions (org.mapstruct.ap.internal.model.source.MappingMethodOptions)1 MappingOptions (org.mapstruct.ap.internal.model.source.MappingOptions)1