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