Search in sources :

Example 1 with ParameterBinding

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

the class TypeSelector method getCandidateParameterBindingPermutations.

/**
 * @param availableParams parameter bindings available in the scope of the method call
 * @param methodParameters parameters of the method that is inspected
 * @return all parameter binding permutations for which proper type checks need to be conducted.
 */
private static List<List<ParameterBinding>> getCandidateParameterBindingPermutations(List<ParameterBinding> availableParams, List<Parameter> methodParameters) {
    if (methodParameters.size() > availableParams.size()) {
        return null;
    }
    List<List<ParameterBinding>> bindingPermutations = new ArrayList<>(1);
    bindingPermutations.add(new ArrayList<>(methodParameters.size()));
    for (Parameter methodParam : methodParameters) {
        List<ParameterBinding> candidateBindings = findCandidateBindingsForParameter(availableParams, methodParam);
        if (candidateBindings.isEmpty()) {
            return null;
        }
        if (candidateBindings.size() == 1) {
            // short-cut to avoid list-copies for the usual case where only one binding fits
            for (List<ParameterBinding> variant : bindingPermutations) {
                // add binding to each existing variant
                variant.add(first(candidateBindings));
            }
        } else {
            List<List<ParameterBinding>> newVariants = new ArrayList<>(bindingPermutations.size() * candidateBindings.size());
            for (List<ParameterBinding> variant : bindingPermutations) {
                // create a copy of each variant for each binding
                for (ParameterBinding binding : candidateBindings) {
                    List<ParameterBinding> extendedVariant = new ArrayList<>(methodParameters.size());
                    extendedVariant.addAll(variant);
                    extendedVariant.add(binding);
                    newVariants.add(extendedVariant);
                }
            }
            bindingPermutations = newVariants;
        }
    }
    return bindingPermutations;
}
Also used : ArrayList(java.util.ArrayList) Parameter(org.mapstruct.ap.internal.model.common.Parameter) ArrayList(java.util.ArrayList) List(java.util.List) ParameterBinding(org.mapstruct.ap.internal.model.common.ParameterBinding)

Example 2 with ParameterBinding

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

the class TypeSelector method addMappingTargetAndTargetTypeBindings.

/**
 * Adds default parameter bindings for the mapping-target and target-type if not already available.
 *
 * @param availableParams Already available params, new entries will be added to this list
 * @param targetType Target type
 */
private void addMappingTargetAndTargetTypeBindings(List<ParameterBinding> availableParams, Type targetType) {
    boolean mappingTargetAvailable = false;
    boolean targetTypeAvailable = false;
    // search available parameter bindings if mapping-target and/or target-type is available
    for (ParameterBinding pb : availableParams) {
        if (pb.isMappingTarget()) {
            mappingTargetAvailable = true;
        } else if (pb.isTargetType()) {
            targetTypeAvailable = true;
        }
    }
    if (!mappingTargetAvailable) {
        availableParams.add(ParameterBinding.forMappingTargetBinding(targetType));
    }
    if (!targetTypeAvailable) {
        availableParams.add(ParameterBinding.forTargetTypeBinding(typeFactory.classTypeOf(targetType)));
    }
}
Also used : ParameterBinding(org.mapstruct.ap.internal.model.common.ParameterBinding)

Example 3 with ParameterBinding

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

the class TypeSelector method getAvailableParameterBindingsFromSourceTypes.

private List<ParameterBinding> getAvailableParameterBindingsFromSourceTypes(List<Type> sourceTypes, Type targetType, Method mappingMethod) {
    List<ParameterBinding> availableParams = new ArrayList<>(sourceTypes.size() + 2);
    for (Type sourceType : sourceTypes) {
        availableParams.add(ParameterBinding.forSourceTypeBinding(sourceType));
    }
    for (Parameter param : mappingMethod.getParameters()) {
        if (param.isMappingContext()) {
            availableParams.add(ParameterBinding.fromParameter(param));
        }
    }
    addMappingTargetAndTargetTypeBindings(availableParams, targetType);
    return availableParams;
}
Also used : Type(org.mapstruct.ap.internal.model.common.Type) ArrayList(java.util.ArrayList) Parameter(org.mapstruct.ap.internal.model.common.Parameter) ParameterBinding(org.mapstruct.ap.internal.model.common.ParameterBinding)

Aggregations

ParameterBinding (org.mapstruct.ap.internal.model.common.ParameterBinding)3 ArrayList (java.util.ArrayList)2 Parameter (org.mapstruct.ap.internal.model.common.Parameter)2 List (java.util.List)1 Type (org.mapstruct.ap.internal.model.common.Type)1