Search in sources :

Example 6 with Parameter

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;
}
Also used : ArrayList(java.util.ArrayList) Parameter(org.mapstruct.ap.internal.model.common.Parameter) List(java.util.List) ArrayList(java.util.ArrayList) ParameterBinding(org.mapstruct.ap.internal.model.common.ParameterBinding)

Example 7 with Parameter

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;
}
Also used : ArrayType(javax.lang.model.type.ArrayType) Type(org.mapstruct.ap.internal.model.common.Type) DeclaredType(javax.lang.model.type.DeclaredType) PrimitiveType(javax.lang.model.type.PrimitiveType) WildcardType(javax.lang.model.type.WildcardType) TypeVariable(javax.lang.model.type.TypeVariable) TypeMirror(javax.lang.model.type.TypeMirror) HashMap(java.util.HashMap) Parameter(org.mapstruct.ap.internal.model.common.Parameter) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with Parameter

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);
}
Also used : PropertyEntry.forSourceReference(org.mapstruct.ap.internal.model.source.PropertyEntry.forSourceReference) ArrayList(java.util.ArrayList) Parameter(org.mapstruct.ap.internal.model.common.Parameter)

Example 9 with Parameter

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;
}
Also used : ArrayList(java.util.ArrayList) Parameter(org.mapstruct.ap.internal.model.common.Parameter) SourceMethod(org.mapstruct.ap.internal.model.source.SourceMethod)

Example 10 with Parameter

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;
}
Also used : Type(org.mapstruct.ap.internal.model.common.Type) Parameter(org.mapstruct.ap.internal.model.common.Parameter)

Aggregations

Parameter (org.mapstruct.ap.internal.model.common.Parameter)16 ArrayList (java.util.ArrayList)8 Type (org.mapstruct.ap.internal.model.common.Type)8 DeclaredType (javax.lang.model.type.DeclaredType)4 ExecutableType (javax.lang.model.type.ExecutableType)4 HashSet (java.util.HashSet)3 SourceMethod (org.mapstruct.ap.internal.model.source.SourceMethod)3 ParameterBinding (org.mapstruct.ap.internal.model.common.ParameterBinding)2 ParameterProvidedMethods (org.mapstruct.ap.internal.model.source.ParameterProvidedMethods)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 TypeElement (javax.lang.model.element.TypeElement)1 ArrayType (javax.lang.model.type.ArrayType)1 PrimitiveType (javax.lang.model.type.PrimitiveType)1 TypeMirror (javax.lang.model.type.TypeMirror)1 TypeVariable (javax.lang.model.type.TypeVariable)1 WildcardType (javax.lang.model.type.WildcardType)1 PropertyEntry.forSourceReference (org.mapstruct.ap.internal.model.source.PropertyEntry.forSourceReference)1