Search in sources :

Example 1 with CollectionMappingStrategyGem

use of org.mapstruct.ap.internal.gem.CollectionMappingStrategyGem in project mapstruct by mapstruct.

the class MappingMethodOptions method applyIgnoreAll.

public void applyIgnoreAll(SourceMethod method, TypeFactory typeFactory, FormattingMessager messager) {
    CollectionMappingStrategyGem cms = method.getOptions().getMapper().getCollectionMappingStrategy();
    Type writeType = method.getResultType();
    if (!method.isUpdateMethod()) {
        writeType = typeFactory.effectiveResultTypeFor(writeType, method.getOptions().getBeanMapping().getBuilder());
    }
    Map<String, Accessor> writeAccessors = writeType.getPropertyWriteAccessors(cms);
    for (MappingOptions mapping : mappings) {
        String mappedTargetProperty = getFirstTargetPropertyName(mapping);
        if (!".".equals(mappedTargetProperty)) {
            // Remove the mapped target property from the write accessors
            writeAccessors.remove(mappedTargetProperty);
        } else {
            messager.printMessage(method.getExecutable(), getBeanMapping().getMirror(), Message.BEANMAPPING_IGNORE_BY_DEFAULT_WITH_MAPPING_TARGET_THIS);
            // Nothing more to do if this is reached
            return;
        }
    }
    // The writeAccessors now contains only the accessors that should be ignored
    for (String targetPropertyName : writeAccessors.keySet()) {
        MappingOptions mapping = MappingOptions.forIgnore(targetPropertyName);
        mappings.add(mapping);
    }
}
Also used : Type(org.mapstruct.ap.internal.model.common.Type) CollectionMappingStrategyGem(org.mapstruct.ap.internal.gem.CollectionMappingStrategyGem) Accessor(org.mapstruct.ap.internal.util.accessor.Accessor)

Example 2 with CollectionMappingStrategyGem

use of org.mapstruct.ap.internal.gem.CollectionMappingStrategyGem in project mapstruct by mapstruct.

the class CollectionAssignmentBuilder method build.

public Assignment build() {
    Assignment result = assignment;
    CollectionMappingStrategyGem cms = method.getOptions().getMapper().getCollectionMappingStrategy();
    boolean targetImmutable = cms == CollectionMappingStrategyGem.TARGET_IMMUTABLE || targetReadAccessor == null;
    if (targetAccessorType == AccessorType.SETTER || targetAccessorType.isFieldAssignment()) {
        if (result.isCallingUpdateMethod() && !targetImmutable) {
            // call to an update method
            if (targetReadAccessor == null) {
                ctx.getMessager().printMessage(method.getExecutable(), Message.PROPERTYMAPPING_NO_READ_ACCESSOR_FOR_TARGET_TYPE, targetPropertyName);
            }
            Assignment factoryMethod = ObjectFactoryMethodResolver.getFactoryMethod(method, targetType, SelectionParameters.forSourceRHS(sourceRHS), ctx);
            result = new UpdateWrapper(result, method.getThrownTypes(), factoryMethod, targetAccessorType.isFieldAssignment(), targetType, true, nvpms == SET_TO_NULL && !targetType.isPrimitive(), nvpms == SET_TO_DEFAULT);
        } else if (method.isUpdateMethod() && !targetImmutable) {
            result = new ExistingInstanceSetterWrapperForCollectionsAndMaps(result, method.getThrownTypes(), targetType, nvcs, nvpms, ctx.getTypeFactory(), targetAccessorType.isFieldAssignment());
        } else if (setterWrapperNeedsSourceNullCheck(result) && canBeMappedOrDirectlyAssigned(result)) {
            result = new SetterWrapperForCollectionsAndMapsWithNullCheck(result, method.getThrownTypes(), targetType, ctx.getTypeFactory(), targetAccessorType.isFieldAssignment());
        } else if (canBeMappedOrDirectlyAssigned(result)) {
            // TODO init default value
            // target accessor is setter, so wrap the setter in setter map/ collection handling
            result = new SetterWrapperForCollectionsAndMaps(result, method.getThrownTypes(), targetType, targetAccessorType.isFieldAssignment());
        } else if (hasNoArgsConstructor()) {
            result = new NewInstanceSetterWrapperForCollectionsAndMaps(result, method.getThrownTypes(), targetType, ctx.getTypeFactory(), targetAccessorType.isFieldAssignment());
        } else {
            ctx.getMessager().printMessage(method.getExecutable(), Message.PROPERTYMAPPING_NO_SUITABLE_COLLECTION_OR_MAP_CONSTRUCTOR, targetType);
        }
    } else {
        if (targetImmutable) {
            ctx.getMessager().printMessage(method.getExecutable(), Message.PROPERTYMAPPING_NO_WRITE_ACCESSOR_FOR_TARGET_TYPE, targetPropertyName);
        }
        // target accessor is getter, so wrap the setter in getter map/ collection handling
        result = new GetterWrapperForCollectionsAndMaps(result, method.getThrownTypes(), targetType, targetAccessorType.isFieldAssignment());
    }
    return result;
}
Also used : Assignment(org.mapstruct.ap.internal.model.common.Assignment) SetterWrapperForCollectionsAndMaps(org.mapstruct.ap.internal.model.assignment.SetterWrapperForCollectionsAndMaps) ExistingInstanceSetterWrapperForCollectionsAndMaps(org.mapstruct.ap.internal.model.assignment.ExistingInstanceSetterWrapperForCollectionsAndMaps) NewInstanceSetterWrapperForCollectionsAndMaps(org.mapstruct.ap.internal.model.assignment.NewInstanceSetterWrapperForCollectionsAndMaps) UpdateWrapper(org.mapstruct.ap.internal.model.assignment.UpdateWrapper) GetterWrapperForCollectionsAndMaps(org.mapstruct.ap.internal.model.assignment.GetterWrapperForCollectionsAndMaps) SetterWrapperForCollectionsAndMapsWithNullCheck(org.mapstruct.ap.internal.model.assignment.SetterWrapperForCollectionsAndMapsWithNullCheck) ExistingInstanceSetterWrapperForCollectionsAndMaps(org.mapstruct.ap.internal.model.assignment.ExistingInstanceSetterWrapperForCollectionsAndMaps) NewInstanceSetterWrapperForCollectionsAndMaps(org.mapstruct.ap.internal.model.assignment.NewInstanceSetterWrapperForCollectionsAndMaps) CollectionMappingStrategyGem(org.mapstruct.ap.internal.gem.CollectionMappingStrategyGem)

Aggregations

CollectionMappingStrategyGem (org.mapstruct.ap.internal.gem.CollectionMappingStrategyGem)2 ExistingInstanceSetterWrapperForCollectionsAndMaps (org.mapstruct.ap.internal.model.assignment.ExistingInstanceSetterWrapperForCollectionsAndMaps)1 GetterWrapperForCollectionsAndMaps (org.mapstruct.ap.internal.model.assignment.GetterWrapperForCollectionsAndMaps)1 NewInstanceSetterWrapperForCollectionsAndMaps (org.mapstruct.ap.internal.model.assignment.NewInstanceSetterWrapperForCollectionsAndMaps)1 SetterWrapperForCollectionsAndMaps (org.mapstruct.ap.internal.model.assignment.SetterWrapperForCollectionsAndMaps)1 SetterWrapperForCollectionsAndMapsWithNullCheck (org.mapstruct.ap.internal.model.assignment.SetterWrapperForCollectionsAndMapsWithNullCheck)1 UpdateWrapper (org.mapstruct.ap.internal.model.assignment.UpdateWrapper)1 Assignment (org.mapstruct.ap.internal.model.common.Assignment)1 Type (org.mapstruct.ap.internal.model.common.Type)1 Accessor (org.mapstruct.ap.internal.util.accessor.Accessor)1