Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package org.mapstruct.ap.internal.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
Expand All @@ -19,7 +18,7 @@
import org.mapstruct.ap.internal.model.source.SourceMethod;
import org.mapstruct.ap.internal.model.source.selector.MethodSelectors;
import org.mapstruct.ap.internal.model.source.selector.SelectedMethod;
import org.mapstruct.ap.internal.model.source.selector.SelectionCriteria;
import org.mapstruct.ap.internal.model.source.selector.SelectionContext;

/**
* Factory for creating lists of appropriate {@link LifecycleCallbackMethodReference}s
Expand Down Expand Up @@ -134,15 +133,12 @@ private static List<LifecycleCallbackMethodReference> collectLifecycleCallbackMe
MappingBuilderContext ctx, Set<String> existingVariableNames) {

MethodSelectors selectors =
new MethodSelectors( ctx.getTypeUtils(), ctx.getElementUtils(), ctx.getTypeFactory(), ctx.getMessager() );
new MethodSelectors( ctx.getTypeUtils(), ctx.getElementUtils(), ctx.getMessager() );

List<SelectedMethod<SourceMethod>> matchingMethods = selectors.getMatchingMethods(
method,
callbackMethods,
Collections.emptyList(),
targetType,
method.getResultType(),
SelectionCriteria.forLifecycleMethods( selectionParameters ) );
SelectionContext.forLifecycleMethods( method, targetType, selectionParameters, ctx.getTypeFactory() )
);

return toLifecycleCallbackMethodRefs(
method,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.mapstruct.ap.internal.model.source.SourceMethod;
import org.mapstruct.ap.internal.model.source.selector.MethodSelectors;
import org.mapstruct.ap.internal.model.source.selector.SelectedMethod;
import org.mapstruct.ap.internal.model.source.selector.SelectionCriteria;
import org.mapstruct.ap.internal.model.source.selector.SelectionContext;
import org.mapstruct.ap.internal.util.Message;

/**
Expand Down Expand Up @@ -126,15 +126,11 @@ public static List<SelectedMethod<SourceMethod>> getMatchingFactoryMethods( Meth
MappingBuilderContext ctx) {

MethodSelectors selectors =
new MethodSelectors( ctx.getTypeUtils(), ctx.getElementUtils(), ctx.getTypeFactory(), ctx.getMessager() );
new MethodSelectors( ctx.getTypeUtils(), ctx.getElementUtils(), ctx.getMessager() );

return selectors.getMatchingMethods(
method,
getAllAvailableMethods( method, ctx.getSourceModel() ),
java.util.Collections.emptyList(),
alternativeTarget,
alternativeTarget,
SelectionCriteria.forFactoryMethods( selectionParameters )
SelectionContext.forFactoryMethods( method, alternativeTarget, selectionParameters, ctx.getTypeFactory() )
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@
package org.mapstruct.ap.internal.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.common.PresenceCheck;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.source.Method;
import org.mapstruct.ap.internal.model.source.ParameterProvidedMethods;
import org.mapstruct.ap.internal.model.source.SelectionParameters;
import org.mapstruct.ap.internal.model.source.SourceMethod;
import org.mapstruct.ap.internal.model.source.selector.MethodSelectors;
import org.mapstruct.ap.internal.model.source.selector.SelectedMethod;
import org.mapstruct.ap.internal.model.source.selector.SelectionCriteria;
import org.mapstruct.ap.internal.model.source.selector.SelectionContext;
import org.mapstruct.ap.internal.util.Message;

/**
Expand Down Expand Up @@ -60,18 +58,12 @@ private static SelectedMethod<SourceMethod> findMatchingPresenceCheckMethod(
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 )
SelectionContext.forPresenceCheckMethods( method, selectionParameters, ctx.getTypeFactory() )
);

if ( matchingMethods.isEmpty() ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.ArrayList;
import java.util.List;

import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.source.Method;

/**
Expand All @@ -29,13 +28,9 @@
public class CreateOrUpdateSelector implements MethodSelector {

@Override
public <T extends Method> List<SelectedMethod<T>> getMatchingMethods(Method mappingMethod,
List<SelectedMethod<T>> methods,
List<Type> sourceTypes,
Type mappingTargetType,
Type returnType,
SelectionCriteria criteria) {

public <T extends Method> List<SelectedMethod<T>> getMatchingMethods(List<SelectedMethod<T>> methods,
SelectionContext context) {
SelectionCriteria criteria = context.getSelectionCriteria();
if ( criteria.isLifecycleCallbackRequired() || criteria.isObjectFactoryRequired()
|| criteria.isPresenceCheckRequired() ) {
return methods;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.ArrayList;
import java.util.List;

import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.source.Method;

/**
Expand All @@ -21,11 +20,9 @@
public class FactoryParameterSelector implements MethodSelector {

@Override
public <T extends Method> List<SelectedMethod<T>> getMatchingMethods(Method mappingMethod,
List<SelectedMethod<T>> methods,
List<Type> sourceTypes,
Type mappingTargetType, Type returnType,
SelectionCriteria criteria) {
public <T extends Method> List<SelectedMethod<T>> getMatchingMethods(List<SelectedMethod<T>> methods,
SelectionContext context) {
SelectionCriteria criteria = context.getSelectionCriteria();
if ( !criteria.isObjectFactoryRequired() || methods.size() <= 1 ) {
return methods;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,22 @@
public class InheritanceSelector implements MethodSelector {

@Override
public <T extends Method> List<SelectedMethod<T>> getMatchingMethods(Method mappingMethod,
List<SelectedMethod<T>> methods,
List<Type> sourceTypes,
Type mappingTargetType, Type returnType,
SelectionCriteria criteria) {
public <T extends Method> List<SelectedMethod<T>> getMatchingMethods(List<SelectedMethod<T>> methods,
SelectionContext context) {

if ( sourceTypes.size() != 1 ) {
Type sourceType = context.getSourceType();
if ( sourceType == null ) {
return methods;
}

Type singleSourceType = first( sourceTypes );

List<SelectedMethod<T>> candidatesWithBestMatchingSourceType = new ArrayList<>();
int bestMatchingSourceTypeDistance = Integer.MAX_VALUE;

// find the methods with the minimum distance regarding getParameter getParameter type
for ( SelectedMethod<T> method : methods ) {
Parameter singleSourceParam = first( method.getMethod().getSourceParameters() );

int sourceTypeDistance = singleSourceType.distanceTo( singleSourceParam.getType() );
int sourceTypeDistance = sourceType.distanceTo( singleSourceParam.getType() );
bestMatchingSourceTypeDistance =
addToCandidateListIfMinimal(
candidatesWithBestMatchingSourceType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.ArrayList;
import java.util.List;

import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.source.Method;

/**
Expand All @@ -20,12 +19,9 @@
public class MethodFamilySelector implements MethodSelector {

@Override
public <T extends Method> List<SelectedMethod<T>> getMatchingMethods(Method mappingMethod,
List<SelectedMethod<T>> methods,
List<Type> sourceTypes,
Type mappingTargetType,
Type returnType,
SelectionCriteria criteria) {
public <T extends Method> List<SelectedMethod<T>> getMatchingMethods(List<SelectedMethod<T>> methods,
SelectionContext context) {
SelectionCriteria criteria = context.getSelectionCriteria();

List<SelectedMethod<T>> result = new ArrayList<>( methods.size() );
for ( SelectedMethod<T> method : methods ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import java.util.List;

import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.source.Method;

/**
Expand All @@ -23,18 +22,10 @@ interface MethodSelector {
* Selects those methods which match the given types and other criteria
*
* @param <T> either SourceMethod or BuiltInMethod
* @param mappingMethod mapping method, defined in Mapper for which this selection is carried out
* @param candidates list of available methods
* @param sourceTypes parameter type(s) that should be matched
* @param mappingTargetType mappingTargetType that should be matched
* @param returnType return type that should be matched
* @param criteria criteria used in the selection process
* @param context the context for the matching
* @return list of methods that passes the matching process
*/
<T extends Method> List<SelectedMethod<T>> getMatchingMethods(Method mappingMethod,
List<SelectedMethod<T>> candidates,
List<Type> sourceTypes,
Type mappingTargetType,
Type returnType,
SelectionCriteria criteria);
<T extends Method> List<SelectedMethod<T>> getMatchingMethods(List<SelectedMethod<T>> candidates,
SelectionContext context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import java.util.Arrays;
import java.util.List;

import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.common.TypeFactory;
import org.mapstruct.ap.internal.model.source.Method;
import org.mapstruct.ap.internal.util.ElementUtils;
import org.mapstruct.ap.internal.util.FormattingMessager;
Expand All @@ -25,11 +23,11 @@ public class MethodSelectors {

private final List<MethodSelector> selectors;

public MethodSelectors(TypeUtils typeUtils, ElementUtils elementUtils, TypeFactory typeFactory,
public MethodSelectors(TypeUtils typeUtils, ElementUtils elementUtils,
FormattingMessager messager) {
selectors = Arrays.asList(
new MethodFamilySelector(),
new TypeSelector( typeFactory, messager ),
new TypeSelector( messager ),
new QualifierSelector( typeUtils, elementUtils ),
new TargetTypeSelector( typeUtils ),
new JavaxXmlElementDeclSelector( typeUtils ),
Expand All @@ -46,34 +44,20 @@ public MethodSelectors(TypeUtils typeUtils, ElementUtils elementUtils, TypeFacto
* Selects those methods which match the given types and other criteria
*
* @param <T> either SourceMethod or BuiltInMethod
* @param mappingMethod mapping method, defined in Mapper for which this selection is carried out
* @param methods list of available methods
* @param sourceTypes parameter type(s) that should be matched
* @param mappingTargetType the mapping target type that should be matched
* @param returnType return type that should be matched
* @param criteria criteria used in the selection process
* @param context the selection context that should be used in the matching process
* @return list of methods that passes the matching process
*/
public <T extends Method> List<SelectedMethod<T>> getMatchingMethods(Method mappingMethod,
List<T> methods,
List<Type> sourceTypes,
Type mappingTargetType,
Type returnType,
SelectionCriteria criteria) {
public <T extends Method> List<SelectedMethod<T>> getMatchingMethods(List<T> methods,
SelectionContext context) {

List<SelectedMethod<T>> candidates = new ArrayList<>( methods.size() );
for ( T method : methods ) {
candidates.add( new SelectedMethod<>( method ) );
}

for ( MethodSelector selector : selectors ) {
candidates = selector.getMatchingMethods(
mappingMethod,
candidates,
sourceTypes,
mappingTargetType,
returnType,
criteria );
candidates = selector.getMatchingMethods( candidates, context );
}
return candidates;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
public class MostSpecificResultTypeSelector implements MethodSelector {

@Override
public <T extends Method> List<SelectedMethod<T>> getMatchingMethods(Method mappingMethod,
List<SelectedMethod<T>> candidates,
List<Type> sourceTypes, Type mappingTargetType,
Type returnType, SelectionCriteria criteria) {
public <T extends Method> List<SelectedMethod<T>> getMatchingMethods(List<SelectedMethod<T>> candidates,
SelectionContext context) {
SelectionCriteria criteria = context.getSelectionCriteria();
Type mappingTargetType = context.getMappingTargetType();
if ( candidates.size() < 2 || !criteria.isForMapping() || criteria.getQualifyingResultType() != null) {
return candidates;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,9 @@ public QualifierSelector(TypeUtils typeUtils, ElementUtils elementUtils ) {
}

@Override
public <T extends Method> List<SelectedMethod<T>> getMatchingMethods(Method mappingMethod,
List<SelectedMethod<T>> methods,
List<Type> sourceTypes,
Type mappingTargetType,
Type returnType,
SelectionCriteria criteria) {
public <T extends Method> List<SelectedMethod<T>> getMatchingMethods(List<SelectedMethod<T>> methods,
SelectionContext context) {
SelectionCriteria criteria = context.getSelectionCriteria();

int numberOfQualifiersToMatch = 0;

Expand Down
Loading