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;
}
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)));
}
}
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;
}
Aggregations