Search in sources :

Example 1 with SelectionCriteria

use of org.mapstruct.ap.internal.model.source.selector.SelectionCriteria in project mapstruct by mapstruct.

the class MappingResolverImpl method getTargetAssignment.

@Override
public Assignment getTargetAssignment(Method mappingMethod, Type targetType, String targetPropertyName, FormattingParameters formattingParameters, SelectionParameters selectionParameters, SourceRHS sourceRHS, boolean preferUpdateMapping) {
    SelectionCriteria criteria = SelectionCriteria.forMappingMethods(selectionParameters, targetPropertyName, preferUpdateMapping);
    ResolvingAttempt attempt = new ResolvingAttempt(sourceModel, mappingMethod, formattingParameters, sourceRHS, criteria);
    return attempt.getTargetAssignment(sourceRHS.getSourceTypeForMatching(), targetType);
}
Also used : SelectionCriteria(org.mapstruct.ap.internal.model.source.selector.SelectionCriteria)

Example 2 with SelectionCriteria

use of org.mapstruct.ap.internal.model.source.selector.SelectionCriteria in project mapstruct by mapstruct.

the class ContainerMappingMethodBuilder method build.

@Override
public final M build() {
    Type sourceParameterType = first(method.getSourceParameters()).getType();
    Type resultType = method.getResultType();
    Type sourceElementType = getElementType(sourceParameterType);
    Type targetElementType = getElementType(resultType);
    String loopVariableName = Strings.getSafeVariableName(sourceElementType.getName(), method.getParameterNames());
    SourceRHS sourceRHS = new SourceRHS(loopVariableName, sourceElementType, new HashSet<>(), errorMessagePart);
    SelectionCriteria criteria = SelectionCriteria.forMappingMethods(selectionParameters, method.getOptions().getIterableMapping().getMappingControl(ctx.getElementUtils()), callingContextTargetPropertyName, false);
    Assignment assignment = ctx.getMappingResolver().getTargetAssignment(method, getDescription(), targetElementType, formattingParameters, criteria, sourceRHS, positionHint, () -> forge(sourceRHS, sourceElementType, targetElementType));
    if (assignment == null) {
        if (method instanceof ForgedMethod) {
            // leave messaging to calling property mapping
            return null;
        } else {
            reportCannotCreateMapping(method, String.format("%s \"%s\"", sourceRHS.getSourceErrorMessagePart(), sourceRHS.getSourceType().describe()), sourceRHS.getSourceType(), targetElementType, "");
        }
    } else {
        ctx.getMessager().note(2, Message.ITERABLEMAPPING_SELECT_ELEMENT_NOTE, assignment);
        if (method instanceof ForgedMethod) {
            ForgedMethod forgedMethod = (ForgedMethod) method;
            forgedMethod.addThrownTypes(assignment.getThrownTypes());
        }
    }
    assignment = getWrapper(assignment, method);
    // mapNullToDefault
    boolean mapNullToDefault = method.getOptions().getIterableMapping().getNullValueMappingStrategy().isReturnDefault();
    MethodReference factoryMethod = null;
    if (!method.isUpdateMethod()) {
        factoryMethod = ObjectFactoryMethodResolver.getFactoryMethod(method, null, ctx);
    }
    Set<String> existingVariables = new HashSet<>(method.getParameterNames());
    existingVariables.add(loopVariableName);
    List<LifecycleCallbackMethodReference> beforeMappingMethods = LifecycleMethodResolver.beforeMappingMethods(method, selectionParameters, ctx, existingVariables);
    List<LifecycleCallbackMethodReference> afterMappingMethods = LifecycleMethodResolver.afterMappingMethods(method, selectionParameters, ctx, existingVariables);
    return instantiateMappingMethod(method, existingVariables, assignment, factoryMethod, mapNullToDefault, loopVariableName, beforeMappingMethods, afterMappingMethods, selectionParameters);
}
Also used : Assignment(org.mapstruct.ap.internal.model.common.Assignment) Type(org.mapstruct.ap.internal.model.common.Type) SourceRHS(org.mapstruct.ap.internal.model.common.SourceRHS) SelectionCriteria(org.mapstruct.ap.internal.model.source.selector.SelectionCriteria) HashSet(java.util.HashSet)

Aggregations

SelectionCriteria (org.mapstruct.ap.internal.model.source.selector.SelectionCriteria)2 HashSet (java.util.HashSet)1 Assignment (org.mapstruct.ap.internal.model.common.Assignment)1 SourceRHS (org.mapstruct.ap.internal.model.common.SourceRHS)1 Type (org.mapstruct.ap.internal.model.common.Type)1