use of org.mapstruct.ap.internal.model.common.Parameter 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<List<ParameterBinding>>(1);
bindingPermutations.add(new ArrayList<ParameterBinding>(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<List<ParameterBinding>>(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<ParameterBinding>(methodParameters.size());
extendedVariant.addAll(variant);
extendedVariant.add(binding);
newVariants.add(extendedVariant);
}
}
bindingPermutations = newVariants;
}
}
return bindingPermutations;
}
use of org.mapstruct.ap.internal.model.common.Parameter in project mapstruct by mapstruct.
the class MethodMatcher method matches.
/**
* Whether the given source and target types are matched by this matcher's candidate method.
*
* @param sourceTypes the source types
* @param resultType the target type
* @return {@code true} when both, source type and target types match the signature of this matcher's method;
* {@code false} otherwise.
*/
boolean matches(List<Type> sourceTypes, Type resultType) {
// check & collect generic types.
Map<TypeVariable, TypeMirror> genericTypesMap = new HashMap<TypeVariable, TypeMirror>();
if (candidateMethod.getParameters().size() == sourceTypes.size()) {
int i = 0;
for (Parameter candidateParam : candidateMethod.getParameters()) {
Type sourceType = sourceTypes.get(i++);
if (sourceType == null || !matchSourceType(sourceType, candidateParam.getType(), genericTypesMap)) {
return false;
}
}
} else {
return false;
}
// check if the method matches the proper result type to construct
Parameter targetTypeParameter = candidateMethod.getTargetTypeParameter();
if (targetTypeParameter != null) {
Type returnClassType = typeFactory.classTypeOf(resultType);
if (!matchSourceType(returnClassType, targetTypeParameter.getType(), genericTypesMap)) {
return false;
}
}
// check result type
if (!matchResultType(resultType, genericTypesMap)) {
return false;
}
// check if all type parameters are indeed mapped
if (candidateMethod.getExecutable().getTypeParameters().size() != genericTypesMap.size()) {
return false;
}
// check if all entries are in the bounds
for (Map.Entry<TypeVariable, TypeMirror> entry : genericTypesMap.entrySet()) {
if (!isWithinBounds(entry.getValue(), getTypeParamFromCandidate(entry.getKey()))) {
// checks if the found Type is in bounds of the TypeParameters bounds.
return false;
}
}
return true;
}
use of org.mapstruct.ap.internal.model.common.Parameter in project mapstruct by mapstruct.
the class SourceReference method copyForInheritanceTo.
/**
* Creates a copy of this reference, which is adapted to the given method
*
* @param method the method to create the copy for
* @return the copy
*/
public SourceReference copyForInheritanceTo(SourceMethod method) {
List<Parameter> replacementParamCandidates = new ArrayList<Parameter>();
for (Parameter sourceParam : method.getSourceParameters()) {
if (sourceParam.getType().isAssignableTo(parameter.getType())) {
replacementParamCandidates.add(sourceParam);
}
}
Parameter replacement = parameter;
if (replacementParamCandidates.size() == 1) {
replacement = first(replacementParamCandidates);
}
return new SourceReference(replacement, propertyEntries, isValid);
}
use of org.mapstruct.ap.internal.model.common.Parameter in project mapstruct by mapstruct.
the class LifecycleCallbackFactory method toLifecycleCallbackMethodRefs.
private static List<LifecycleCallbackMethodReference> toLifecycleCallbackMethodRefs(Method method, List<SelectedMethod<SourceMethod>> candidates, MappingBuilderContext ctx, Set<String> existingVariableNames) {
List<LifecycleCallbackMethodReference> result = new ArrayList<LifecycleCallbackMethodReference>();
for (SelectedMethod<SourceMethod> candidate : candidates) {
Parameter providingParameter = method.getContextProvidedMethods().getParameterForProvidedMethod(candidate.getMethod());
if (providingParameter != null) {
result.add(LifecycleCallbackMethodReference.forParameterProvidedMethod(candidate, providingParameter, method, existingVariableNames));
} else {
MapperReference mapperReference = findMapperReference(ctx.getMapperReferences(), candidate.getMethod());
result.add(LifecycleCallbackMethodReference.forMethodReference(candidate, mapperReference, method, existingVariableNames));
}
}
return result;
}
use of org.mapstruct.ap.internal.model.common.Parameter in project mapstruct by mapstruct.
the class ForgedMethod method matches.
@Override
public boolean matches(List<Type> sourceTypes, Type targetType) {
if (!targetType.equals(returnType)) {
return false;
}
if (parameters.size() != sourceTypes.size()) {
return false;
}
Iterator<Type> srcTypeIt = sourceTypes.iterator();
Iterator<Parameter> paramIt = parameters.iterator();
while (srcTypeIt.hasNext() && paramIt.hasNext()) {
Type sourceType = srcTypeIt.next();
Parameter param = paramIt.next();
if (!sourceType.equals(param.getType())) {
return false;
}
}
return true;
}
Aggregations