Search in sources :

Example 1 with Method

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

the class MappingResolverImpl method getFactoryMethod.

@Override
public MethodReference getFactoryMethod(final Method mappingMethod, Type targetType, SelectionParameters selectionParameters) {
    List<SelectedMethod<Method>> matchingFactoryMethods = methodSelectors.getMatchingMethods(mappingMethod, sourceModel, java.util.Collections.<Type>emptyList(), targetType, SelectionCriteria.forFactoryMethods(selectionParameters));
    if (matchingFactoryMethods.isEmpty()) {
        return null;
    }
    if (matchingFactoryMethods.size() > 1) {
        messager.printMessage(mappingMethod.getExecutable(), Message.GENERAL_AMBIGIOUS_FACTORY_METHOD, targetType, Strings.join(matchingFactoryMethods, ", "));
        return null;
    }
    SelectedMethod<Method> matchingFactoryMethod = first(matchingFactoryMethods);
    MapperReference ref = findMapperReference(matchingFactoryMethod.getMethod());
    return MethodReference.forMapperReference(matchingFactoryMethod.getMethod(), ref, matchingFactoryMethod.getParameterBindings());
}
Also used : SelectedMethod(org.mapstruct.ap.internal.model.source.selector.SelectedMethod) MapperReference(org.mapstruct.ap.internal.model.MapperReference) BuiltInMethod(org.mapstruct.ap.internal.model.source.builtin.BuiltInMethod) Method(org.mapstruct.ap.internal.model.source.Method) HelperMethod(org.mapstruct.ap.internal.model.HelperMethod) VirtualMappingMethod(org.mapstruct.ap.internal.model.VirtualMappingMethod) SelectedMethod(org.mapstruct.ap.internal.model.source.selector.SelectedMethod)

Example 2 with Method

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

the class PresenceCheckMethodResolver method findMatchingPresenceCheckMethod.

private static SelectedMethod<SourceMethod> findMatchingPresenceCheckMethod(Method method, SelectionParameters selectionParameters, MappingBuilderContext ctx) {
    MethodSelectors selectors = new MethodSelectors(ctx.getTypeUtils(), ctx.getElementUtils(), ctx.getTypeFactory(), ctx.getMessager());
    Type booleanType = ctx.getTypeFactory().getType(Boolean.class);
    List<SelectedMethod<SourceMethod>> matchingMethods = selectors.getMatchingMethods(method, getAllAvailableMethods(method, ctx.getSourceModel()), Collections.emptyList(), booleanType, booleanType, SelectionCriteria.forPresenceCheckMethods(selectionParameters));
    if (matchingMethods.isEmpty()) {
        return null;
    }
    if (matchingMethods.size() > 1) {
        ctx.getMessager().printMessage(method.getExecutable(), Message.GENERAL_AMBIGUOUS_PRESENCE_CHECK_METHOD, selectionParameters.getSourceRHS().getSourceType().describe(), matchingMethods.stream().map(SelectedMethod::getMethod).map(Method::describe).collect(Collectors.joining(", ")));
        return null;
    }
    return matchingMethods.get(0);
}
Also used : Type(org.mapstruct.ap.internal.model.common.Type) SelectedMethod(org.mapstruct.ap.internal.model.source.selector.SelectedMethod) SelectedMethod(org.mapstruct.ap.internal.model.source.selector.SelectedMethod) SourceMethod(org.mapstruct.ap.internal.model.source.SourceMethod) Method(org.mapstruct.ap.internal.model.source.Method) MethodSelectors(org.mapstruct.ap.internal.model.source.selector.MethodSelectors)

Example 3 with Method

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

the class TypeSelector method getMatchingParameterBinding.

private <T extends Method> SelectedMethod<T> getMatchingParameterBinding(Type returnType, Method mappingMethod, SelectedMethod<T> selectedMethodInfo, List<List<ParameterBinding>> parameterAssignmentVariants) {
    List<List<ParameterBinding>> matchingParameterAssignmentVariants = new ArrayList<>(parameterAssignmentVariants);
    Method selectedMethod = selectedMethodInfo.getMethod();
    // remove all assignment variants that doesn't match the types from the method
    matchingParameterAssignmentVariants.removeIf(parameterAssignments -> !selectedMethod.matches(extractTypes(parameterAssignments), returnType));
    if (matchingParameterAssignmentVariants.isEmpty()) {
        // no matching variants found
        return null;
    } else if (matchingParameterAssignmentVariants.size() == 1) {
        // we found exactly one set of variants, use this
        selectedMethodInfo.setParameterBindings(first(matchingParameterAssignmentVariants));
        return selectedMethodInfo;
    }
    // more than one variant matches, try to find one where also the parameter names are matching
    // -> remove all variants where the binding var-name doesn't match the var-name of the parameter
    List<Parameter> methodParameters = selectedMethod.getParameters();
    matchingParameterAssignmentVariants.removeIf(parameterBindings -> parameterBindingNotMatchesParameterVariableNames(parameterBindings, methodParameters));
    if (matchingParameterAssignmentVariants.isEmpty()) {
        // we had some matching assignments before, but when checking the parameter names we can't find an
        // appropriate one, in this case the user must chose identical parameter names for the mapping and lifecycle
        // method
        messager.printMessage(selectedMethod.getExecutable(), Message.LIFECYCLEMETHOD_AMBIGUOUS_PARAMETERS, mappingMethod);
        return null;
    }
    // there should never be more then one assignment left after checking the parameter names as it is not possible
    // to use the same parameter name more then once
    // -> we can use the first variant that is left (that should also be the only one)
    selectedMethodInfo.setParameterBindings(first(matchingParameterAssignmentVariants));
    return selectedMethodInfo;
}
Also used : ArrayList(java.util.ArrayList) Parameter(org.mapstruct.ap.internal.model.common.Parameter) ArrayList(java.util.ArrayList) List(java.util.List) Method(org.mapstruct.ap.internal.model.source.Method)

Aggregations

Method (org.mapstruct.ap.internal.model.source.Method)3 SelectedMethod (org.mapstruct.ap.internal.model.source.selector.SelectedMethod)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 HelperMethod (org.mapstruct.ap.internal.model.HelperMethod)1 MapperReference (org.mapstruct.ap.internal.model.MapperReference)1 VirtualMappingMethod (org.mapstruct.ap.internal.model.VirtualMappingMethod)1 Parameter (org.mapstruct.ap.internal.model.common.Parameter)1 Type (org.mapstruct.ap.internal.model.common.Type)1 SourceMethod (org.mapstruct.ap.internal.model.source.SourceMethod)1 BuiltInMethod (org.mapstruct.ap.internal.model.source.builtin.BuiltInMethod)1 MethodSelectors (org.mapstruct.ap.internal.model.source.selector.MethodSelectors)1