Search in sources :

Example 1 with ExistingInstanceSetterWrapperForCollectionsAndMaps

use of org.mapstruct.ap.internal.model.assignment.ExistingInstanceSetterWrapperForCollectionsAndMaps 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)1 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