#750 Refactoring, moving selection parameters to common SelectorParameters, repair

This commit is contained in:
sjaakd 2016-02-14 23:15:27 +01:00
parent 6a5e2a79e4
commit d83d8b4102
16 changed files with 250 additions and 361 deletions

View File

@ -34,7 +34,6 @@ import java.util.Set;
import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.ExecutableElement;
import javax.lang.model.type.DeclaredType; import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic; import javax.tools.Diagnostic;
import org.mapstruct.ap.internal.model.PropertyMapping.ConstantMappingBuilder; import org.mapstruct.ap.internal.model.PropertyMapping.ConstantMappingBuilder;
@ -45,6 +44,7 @@ import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.dependency.GraphAnalyzer; import org.mapstruct.ap.internal.model.dependency.GraphAnalyzer;
import org.mapstruct.ap.internal.model.dependency.GraphAnalyzer.GraphAnalyzerBuilder; import org.mapstruct.ap.internal.model.dependency.GraphAnalyzer.GraphAnalyzerBuilder;
import org.mapstruct.ap.internal.model.source.Mapping; import org.mapstruct.ap.internal.model.source.Mapping;
import org.mapstruct.ap.internal.model.source.SelectionParameters;
import org.mapstruct.ap.internal.model.source.SourceMethod; import org.mapstruct.ap.internal.model.source.SourceMethod;
import org.mapstruct.ap.internal.model.source.SourceReference; import org.mapstruct.ap.internal.model.source.SourceReference;
import org.mapstruct.ap.internal.option.ReportingPolicy; import org.mapstruct.ap.internal.option.ReportingPolicy;
@ -79,10 +79,8 @@ public class BeanMappingMethod extends MappingMethod {
private Set<String> targetProperties; private Set<String> targetProperties;
private final List<PropertyMapping> propertyMappings = new ArrayList<PropertyMapping>(); private final List<PropertyMapping> propertyMappings = new ArrayList<PropertyMapping>();
private final Set<Parameter> unprocessedSourceParameters = new HashSet<Parameter>(); private final Set<Parameter> unprocessedSourceParameters = new HashSet<Parameter>();
private List<TypeMirror> qualifiers;
private List<String> qualifyingNames;
private NullValueMappingStrategyPrism nullValueMappingStrategy; private NullValueMappingStrategyPrism nullValueMappingStrategy;
private TypeMirror resultTypeMirror; private SelectionParameters selectionParameters;
private final Set<String> existingVariableNames = new HashSet<String>(); private final Set<String> existingVariableNames = new HashSet<String>();
public Builder mappingContext(MappingBuilderContext mappingContext) { public Builder mappingContext(MappingBuilderContext mappingContext) {
@ -103,13 +101,8 @@ public class BeanMappingMethod extends MappingMethod {
return this; return this;
} }
public Builder qualifiers(List<TypeMirror> qualifiers) { public Builder selectionParameters(SelectionParameters selectionParameters) {
this.qualifiers = qualifiers; this.selectionParameters = selectionParameters;
return this;
}
public Builder qualifyingNames(List<String> qualifyingNames) {
this.qualifyingNames = qualifyingNames;
return this; return this;
} }
@ -118,11 +111,6 @@ public class BeanMappingMethod extends MappingMethod {
return this; return this;
} }
public Builder resultType(TypeMirror resultType) {
this.resultTypeMirror = resultType;
return this;
}
public BeanMappingMethod build() { public BeanMappingMethod build() {
// map properties with mapping // map properties with mapping
boolean mappingErrorOccured = handleDefinedSourceMappings(); boolean mappingErrorOccured = handleDefinedSourceMappings();
@ -150,16 +138,14 @@ public class BeanMappingMethod extends MappingMethod {
factoryMethod = ctx.getMappingResolver().getFactoryMethod( factoryMethod = ctx.getMappingResolver().getFactoryMethod(
method, method,
method.getResultType(), method.getResultType(),
qualifiers, selectionParameters );
qualifyingNames,
resultTypeMirror );
} }
// if there's no factory method, try the resultType in the @BeanMapping // if there's no factory method, try the resultType in the @BeanMapping
Type resultType = null; Type resultType = null;
if ( factoryMethod == null ) { if ( factoryMethod == null ) {
if ( resultTypeMirror != null ) { if ( selectionParameters != null && selectionParameters.getResultType() != null ) {
resultType = ctx.getTypeFactory().getType( resultTypeMirror ); resultType = ctx.getTypeFactory().getType( selectionParameters.getResultType() );
if ( !resultType.isAssignableTo( method.getResultType() ) ) { if ( !resultType.isAssignableTo( method.getResultType() ) ) {
ctx.getMessager().printMessage( ctx.getMessager().printMessage(
method.getExecutable(), method.getExecutable(),
@ -173,9 +159,9 @@ public class BeanMappingMethod extends MappingMethod {
sortPropertyMappingsByDependencies(); sortPropertyMappingsByDependencies();
List<LifecycleCallbackMethodReference> beforeMappingMethods = List<LifecycleCallbackMethodReference> beforeMappingMethods =
LifecycleCallbackFactory.beforeMappingMethods( method, qualifiers, ctx ); LifecycleCallbackFactory.beforeMappingMethods( method, selectionParameters, ctx );
List<LifecycleCallbackMethodReference> afterMappingMethods = List<LifecycleCallbackMethodReference> afterMappingMethods =
LifecycleCallbackFactory.afterMappingMethods( method, qualifiers, ctx ); LifecycleCallbackFactory.afterMappingMethods( method, selectionParameters, ctx );
return new BeanMappingMethod( return new BeanMappingMethod(
method, method,
@ -306,9 +292,7 @@ public class BeanMappingMethod extends MappingMethod {
.targetReadAccessor( getTargetPropertyReadAccessor( mapping.getTargetName() ) ) .targetReadAccessor( getTargetPropertyReadAccessor( mapping.getTargetName() ) )
.targetPropertyName( mapping.getTargetName() ) .targetPropertyName( mapping.getTargetName() )
.sourceReference( sourceRef ) .sourceReference( sourceRef )
.qualifiers( mapping.getQualifiers() ) .selectionParameters( mapping.getSelectionParameters() )
.qualifyingNames( mapping.getQualifyingNames() )
.resultType( mapping.getResultType() )
.dateFormat( mapping.getDateFormat() ) .dateFormat( mapping.getDateFormat() )
.existingVariableNames( existingVariableNames ) .existingVariableNames( existingVariableNames )
.dependsOn( mapping.getDependsOn() ) .dependsOn( mapping.getDependsOn() )
@ -334,9 +318,7 @@ public class BeanMappingMethod extends MappingMethod {
.targetReadAccessor( getTargetPropertyReadAccessor( mapping.getTargetName() ) ) .targetReadAccessor( getTargetPropertyReadAccessor( mapping.getTargetName() ) )
.targetPropertyName( mapping.getTargetName() ) .targetPropertyName( mapping.getTargetName() )
.dateFormat( mapping.getDateFormat() ) .dateFormat( mapping.getDateFormat() )
.qualifiers( mapping.getQualifiers() ) .selectionParameters( mapping.getSelectionParameters() )
.qualifyingNames( mapping.getQualifyingNames() )
.resultType( mapping.getResultType() )
.existingVariableNames( existingVariableNames ) .existingVariableNames( existingVariableNames )
.dependsOn( mapping.getDependsOn() ) .dependsOn( mapping.getDependsOn() )
.build(); .build();
@ -383,14 +365,14 @@ public class BeanMappingMethod extends MappingMethod {
* the set of remaining target properties. * the set of remaining target properties.
*/ */
private void applyPropertyNameBasedMapping() { private void applyPropertyNameBasedMapping() {
Iterator<Entry<String, ExecutableElement>> targetProperties = Iterator<Entry<String, ExecutableElement>> targetPropertiesIterator =
unprocessedTargetProperties.entrySet().iterator(); unprocessedTargetProperties.entrySet().iterator();
// usually there should be only one getter; only for Boolean there may be two: isFoo() and getFoo() // usually there should be only one getter; only for Boolean there may be two: isFoo() and getFoo()
List<ExecutableElement> candidates = new ArrayList<ExecutableElement>( 2 ); List<ExecutableElement> candidates = new ArrayList<ExecutableElement>( 2 );
while ( targetProperties.hasNext() ) { while ( targetPropertiesIterator.hasNext() ) {
Entry<String, ExecutableElement> targetProperty = targetProperties.next(); Entry<String, ExecutableElement> targetProperty = targetPropertiesIterator.next();
PropertyMapping propertyMapping = null; PropertyMapping propertyMapping = null;
@ -432,9 +414,7 @@ public class BeanMappingMethod extends MappingMethod {
.targetReadAccessor( getTargetPropertyReadAccessor( targetProperty.getKey() ) ) .targetReadAccessor( getTargetPropertyReadAccessor( targetProperty.getKey() ) )
.targetPropertyName( targetProperty.getKey() ) .targetPropertyName( targetProperty.getKey() )
.sourceReference( sourceRef ) .sourceReference( sourceRef )
.qualifiers( mapping != null ? mapping.getQualifiers() : null ) .selectionParameters( mapping != null ? mapping.getSelectionParameters() : null )
.qualifyingNames( mapping != null ? mapping.getQualifyingNames() : null )
.resultType( mapping != null ? mapping.getResultType() : null )
.dateFormat( mapping != null ? mapping.getDateFormat() : null ) .dateFormat( mapping != null ? mapping.getDateFormat() : null )
.defaultValue( mapping != null ? mapping.getDefaultValue() : null ) .defaultValue( mapping != null ? mapping.getDefaultValue() : null )
.existingVariableNames( existingVariableNames ) .existingVariableNames( existingVariableNames )
@ -464,7 +444,7 @@ public class BeanMappingMethod extends MappingMethod {
if ( propertyMapping != null ) { if ( propertyMapping != null ) {
propertyMappings.add( propertyMapping ); propertyMappings.add( propertyMapping );
targetProperties.remove(); targetPropertiesIterator.remove();
} }
} }
} }
@ -498,9 +478,7 @@ public class BeanMappingMethod extends MappingMethod {
.targetReadAccessor( getTargetPropertyReadAccessor( targetProperty.getKey() ) ) .targetReadAccessor( getTargetPropertyReadAccessor( targetProperty.getKey() ) )
.targetPropertyName( targetProperty.getKey() ) .targetPropertyName( targetProperty.getKey() )
.sourceReference( sourceRef ) .sourceReference( sourceRef )
.qualifiers( mapping != null ? mapping.getQualifiers() : null ) .selectionParameters( mapping != null ? mapping.getSelectionParameters() : null )
.qualifyingNames( mapping != null ? mapping.getQualifyingNames() : null )
.resultType( mapping != null ? mapping.getResultType() : null )
.dateFormat( mapping != null ? mapping.getDateFormat() : null ) .dateFormat( mapping != null ? mapping.getDateFormat() : null )
.existingVariableNames( existingVariableNames ) .existingVariableNames( existingVariableNames )
.dependsOn( mapping != null ? mapping.getDependsOn() : Collections.<String>emptyList() ) .dependsOn( mapping != null ? mapping.getDependsOn() : Collections.<String>emptyList() )

View File

@ -29,6 +29,7 @@ import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.source.EnumMapping; import org.mapstruct.ap.internal.model.source.EnumMapping;
import org.mapstruct.ap.internal.model.source.Mapping; import org.mapstruct.ap.internal.model.source.Mapping;
import org.mapstruct.ap.internal.model.source.Method; import org.mapstruct.ap.internal.model.source.Method;
import org.mapstruct.ap.internal.model.source.SelectionParameters;
import org.mapstruct.ap.internal.model.source.SourceMethod; import org.mapstruct.ap.internal.model.source.SourceMethod;
import org.mapstruct.ap.internal.prism.BeanMappingPrism; import org.mapstruct.ap.internal.prism.BeanMappingPrism;
import org.mapstruct.ap.internal.util.Message; import org.mapstruct.ap.internal.util.Message;
@ -96,23 +97,24 @@ public class EnumMappingMethod extends MappingMethod {
} }
} }
List<TypeMirror> qualifiers = getQualifiers( method ); SelectionParameters selectionParameters = getSelecionParameters( method );
List<LifecycleCallbackMethodReference> beforeMappingMethods = List<LifecycleCallbackMethodReference> beforeMappingMethods =
LifecycleCallbackFactory.beforeMappingMethods( method, qualifiers, ctx ); LifecycleCallbackFactory.beforeMappingMethods( method, selectionParameters, ctx );
List<LifecycleCallbackMethodReference> afterMappingMethods = List<LifecycleCallbackMethodReference> afterMappingMethods =
LifecycleCallbackFactory.afterMappingMethods( method, qualifiers, ctx ); LifecycleCallbackFactory.afterMappingMethods( method, selectionParameters, ctx );
return new EnumMappingMethod( method, enumMappings, beforeMappingMethods, afterMappingMethods ); return new EnumMappingMethod( method, enumMappings, beforeMappingMethods, afterMappingMethods );
} }
private static List<TypeMirror> getQualifiers(SourceMethod method) { private static SelectionParameters getSelecionParameters(SourceMethod method) {
BeanMappingPrism beanMappingPrism = BeanMappingPrism.getInstanceOn( method.getExecutable() ); BeanMappingPrism beanMappingPrism = BeanMappingPrism.getInstanceOn( method.getExecutable() );
if ( beanMappingPrism != null ) { if ( beanMappingPrism != null ) {
return beanMappingPrism.qualifiedBy(); List<TypeMirror> qualifiers = beanMappingPrism.qualifiedBy();
List<String> qualifyingNames = beanMappingPrism.qualifiedByName();
TypeMirror resultType = beanMappingPrism.resultType();
return new SelectionParameters( qualifiers, qualifyingNames, resultType );
} }
return null; return null;
} }

View File

@ -24,7 +24,6 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import org.mapstruct.ap.internal.model.assignment.Assignment; import org.mapstruct.ap.internal.model.assignment.Assignment;
import org.mapstruct.ap.internal.model.assignment.LocalVarWrapper; import org.mapstruct.ap.internal.model.assignment.LocalVarWrapper;
@ -33,6 +32,7 @@ import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.source.ForgedMethod; import org.mapstruct.ap.internal.model.source.ForgedMethod;
import org.mapstruct.ap.internal.model.source.Method; import org.mapstruct.ap.internal.model.source.Method;
import org.mapstruct.ap.internal.model.source.SelectionParameters;
import org.mapstruct.ap.internal.prism.NullValueMappingStrategyPrism; import org.mapstruct.ap.internal.prism.NullValueMappingStrategyPrism;
import org.mapstruct.ap.internal.util.Message; import org.mapstruct.ap.internal.util.Message;
import org.mapstruct.ap.internal.util.Strings; import org.mapstruct.ap.internal.util.Strings;
@ -56,9 +56,7 @@ public class IterableMappingMethod extends MappingMethod {
private Method method; private Method method;
private MappingBuilderContext ctx; private MappingBuilderContext ctx;
private String dateFormat; private String dateFormat;
private List<TypeMirror> qualifiers; private SelectionParameters selectionParameters;
private List<String> qualifyingNames;
private TypeMirror qualifyingElementTargetType;
private NullValueMappingStrategyPrism nullValueMappingStrategy; private NullValueMappingStrategyPrism nullValueMappingStrategy;
public Builder mappingContext(MappingBuilderContext mappingContext) { public Builder mappingContext(MappingBuilderContext mappingContext) {
@ -76,18 +74,8 @@ public class IterableMappingMethod extends MappingMethod {
return this; return this;
} }
public Builder qualifiers(List<TypeMirror> qualifiers) { public Builder selectionParameters(SelectionParameters selectionParameters) {
this.qualifiers = qualifiers; this.selectionParameters = selectionParameters;
return this;
}
public Builder qualifyingNames(List<String> qualifyingNames) {
this.qualifyingNames = qualifyingNames;
return this;
}
public Builder qualifyingElementTargetType(TypeMirror qualifyingElementTargetType) {
this.qualifyingElementTargetType = qualifyingElementTargetType;
return this; return this;
} }
@ -117,9 +105,7 @@ public class IterableMappingMethod extends MappingMethod {
targetElementType, targetElementType,
null, // there is no targetPropertyName null, // there is no targetPropertyName
dateFormat, dateFormat,
qualifiers, selectionParameters,
qualifyingNames,
qualifyingElementTargetType,
loopVariableName, loopVariableName,
false false
); );
@ -155,14 +141,13 @@ public class IterableMappingMethod extends MappingMethod {
MethodReference factoryMethod = null; MethodReference factoryMethod = null;
if ( !method.isUpdateMethod() ) { if ( !method.isUpdateMethod() ) {
factoryMethod = ctx.getMappingResolver().getFactoryMethod( method, method.getResultType(), null, null, factoryMethod = ctx.getMappingResolver().getFactoryMethod( method, method.getResultType(), null );
null );
} }
List<LifecycleCallbackMethodReference> beforeMappingMethods = List<LifecycleCallbackMethodReference> beforeMappingMethods =
LifecycleCallbackFactory.beforeMappingMethods( method, qualifiers, ctx ); LifecycleCallbackFactory.beforeMappingMethods( method, selectionParameters, ctx );
List<LifecycleCallbackMethodReference> afterMappingMethods = List<LifecycleCallbackMethodReference> afterMappingMethods =
LifecycleCallbackFactory.afterMappingMethods( method, qualifiers, ctx ); LifecycleCallbackFactory.afterMappingMethods( method, selectionParameters, ctx );
return new IterableMappingMethod( return new IterableMappingMethod(
method, method,

View File

@ -24,11 +24,11 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import org.mapstruct.ap.internal.model.common.Parameter; import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.source.Method; import org.mapstruct.ap.internal.model.source.Method;
import org.mapstruct.ap.internal.model.source.SelectionParameters;
import org.mapstruct.ap.internal.model.source.SourceMethod; import org.mapstruct.ap.internal.model.source.SourceMethod;
import org.mapstruct.ap.internal.model.source.selector.QualifierSelector; import org.mapstruct.ap.internal.model.source.selector.QualifierSelector;
import org.mapstruct.ap.internal.model.source.selector.SelectionCriteria; import org.mapstruct.ap.internal.model.source.selector.SelectionCriteria;
@ -45,57 +45,57 @@ public final class LifecycleCallbackFactory {
/** /**
* @param method the method to obtain the beforeMapping methods for * @param method the method to obtain the beforeMapping methods for
* @param qualifiers method qualifiers * @param selectionParameters method selectionParameters
* @param ctx the builder context * @param ctx the builder context
* @return all applicable {@code @BeforeMapping} methods for the given method * @return all applicable {@code @BeforeMapping} methods for the given method
*/ */
public static List<LifecycleCallbackMethodReference> beforeMappingMethods( public static List<LifecycleCallbackMethodReference> beforeMappingMethods(
Method method, List<TypeMirror> qualifiers, MappingBuilderContext ctx) { Method method, SelectionParameters selectionParameters, MappingBuilderContext ctx) {
return collectLifecycleCallbackMethods( return collectLifecycleCallbackMethods(
method, method,
qualifiers, selectionParameters,
filterBeforeMappingMethods( ctx.getSourceModel() ), filterBeforeMappingMethods( ctx.getSourceModel() ),
ctx ); ctx );
} }
/** /**
* @param method the method to obtain the afterMapping methods for * @param method the method to obtain the afterMapping methods for
* @param qualifiers method qualifiers * @param selectionParameters method selectionParameters
* @param ctx the builder context * @param ctx the builder context
* @return all applicable {@code @AfterMapping} methods for the given method * @return all applicable {@code @AfterMapping} methods for the given method
*/ */
public static List<LifecycleCallbackMethodReference> afterMappingMethods( public static List<LifecycleCallbackMethodReference> afterMappingMethods(
Method method, List<TypeMirror> qualifiers, MappingBuilderContext ctx) { Method method, SelectionParameters selectionParameters, MappingBuilderContext ctx) {
return collectLifecycleCallbackMethods( return collectLifecycleCallbackMethods(
method, method,
qualifiers, selectionParameters,
filterAfterMappingMethods( ctx.getSourceModel() ), filterAfterMappingMethods( ctx.getSourceModel() ),
ctx ); ctx );
} }
private static List<LifecycleCallbackMethodReference> collectLifecycleCallbackMethods( private static List<LifecycleCallbackMethodReference> collectLifecycleCallbackMethods(
Method method, List<TypeMirror> qualifiers, List<SourceMethod> callbackMethods, MappingBuilderContext ctx) { Method method, SelectionParameters selectionParameters, List<SourceMethod> callbackMethods,
MappingBuilderContext ctx) {
Map<SourceMethod, List<Parameter>> parameterAssignmentsForSourceMethod = Map<SourceMethod, List<Parameter>> parameterAssignmentsForSourceMethod
new HashMap<SourceMethod, List<Parameter>>(); = new HashMap<SourceMethod, List<Parameter>>();
List<SourceMethod> candidates = List<SourceMethod> candidates =
filterCandidatesByType( method, callbackMethods, parameterAssignmentsForSourceMethod, ctx ); filterCandidatesByType( method, callbackMethods, parameterAssignmentsForSourceMethod, ctx );
candidates = filterCandidatesByQualifiers( method, qualifiers, candidates, ctx ); candidates = filterCandidatesByQualifiers( method, selectionParameters, candidates, ctx );
return toLifecycleCallbackMethodRefs( candidates, parameterAssignmentsForSourceMethod, ctx ); return toLifecycleCallbackMethodRefs( candidates, parameterAssignmentsForSourceMethod, ctx );
} }
private static List<SourceMethod> filterCandidatesByQualifiers(Method method, List<TypeMirror> qualifiers, private static List<SourceMethod> filterCandidatesByQualifiers(Method method,
SelectionParameters selectionParameters,
List<SourceMethod> candidates, List<SourceMethod> candidates,
MappingBuilderContext ctx) { MappingBuilderContext ctx) {
QualifierSelector selector = new QualifierSelector( ctx.getTypeUtils(), ctx.getElementUtils() ); QualifierSelector selector = new QualifierSelector( ctx.getTypeUtils(), ctx.getElementUtils() );
return selector.getMatchingMethods( method, candidates, null, null, new SelectionCriteria( return selector.getMatchingMethods( method, candidates, null, null, new SelectionCriteria(
qualifiers, selectionParameters,
null, /* todo */
null,
null, null,
false ) ); false ) );
} }

View File

@ -23,14 +23,13 @@ import static org.mapstruct.ap.internal.util.Collections.first;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import javax.lang.model.type.TypeMirror;
import org.mapstruct.ap.internal.model.assignment.Assignment; import org.mapstruct.ap.internal.model.assignment.Assignment;
import org.mapstruct.ap.internal.model.assignment.LocalVarWrapper; import org.mapstruct.ap.internal.model.assignment.LocalVarWrapper;
import org.mapstruct.ap.internal.model.common.Parameter; import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.source.ForgedMethod; import org.mapstruct.ap.internal.model.source.ForgedMethod;
import org.mapstruct.ap.internal.model.source.Method; import org.mapstruct.ap.internal.model.source.Method;
import org.mapstruct.ap.internal.model.source.SelectionParameters;
import org.mapstruct.ap.internal.prism.NullValueMappingStrategyPrism; import org.mapstruct.ap.internal.prism.NullValueMappingStrategyPrism;
import org.mapstruct.ap.internal.util.Message; import org.mapstruct.ap.internal.util.Message;
import org.mapstruct.ap.internal.util.Strings; import org.mapstruct.ap.internal.util.Strings;
@ -53,15 +52,11 @@ public class MapMappingMethod extends MappingMethod {
private String keyDateFormat; private String keyDateFormat;
private String valueDateFormat; private String valueDateFormat;
private List<TypeMirror> keyQualifiers;
private List<String> keyQualifyingNames;
private List<TypeMirror> valueQualifiers;
private List<String> valueQualifyingNames;
private TypeMirror keyQualifyingTargetType;
private TypeMirror valueQualifyingTargetType;
private Method method; private Method method;
private MappingBuilderContext ctx; private MappingBuilderContext ctx;
private NullValueMappingStrategyPrism nullValueMappingStrategy; private NullValueMappingStrategyPrism nullValueMappingStrategy;
private SelectionParameters keySelectionParameters;
private SelectionParameters valueSelectionParameters;
public Builder mappingContext(MappingBuilderContext mappingContext) { public Builder mappingContext(MappingBuilderContext mappingContext) {
this.ctx = mappingContext; this.ctx = mappingContext;
@ -73,6 +68,16 @@ public class MapMappingMethod extends MappingMethod {
return this; return this;
} }
public Builder keySelectionParameters(SelectionParameters keySelectionParameters) {
this.keySelectionParameters = keySelectionParameters;
return this;
}
public Builder valueSelectionParameters(SelectionParameters valueSelectionParameters) {
this.valueSelectionParameters = valueSelectionParameters;
return this;
}
public Builder keyDateFormat(String keyDateFormat) { public Builder keyDateFormat(String keyDateFormat) {
this.keyDateFormat = keyDateFormat; this.keyDateFormat = keyDateFormat;
return this; return this;
@ -83,43 +88,12 @@ public class MapMappingMethod extends MappingMethod {
return this; return this;
} }
public Builder keyQualifiers(List<TypeMirror> keyQualifiers) {
this.keyQualifiers = keyQualifiers;
return this;
}
public Builder keyQualifyingNames(List<String> keyQualifyingNames) {
this.keyQualifyingNames = keyQualifyingNames;
return this;
}
public Builder valueQualifiers(List<TypeMirror> valueQualifiers) {
this.valueQualifiers = valueQualifiers;
return this;
}
public Builder valueQualifyingNames(List<String> valueQualifyingNames) {
this.valueQualifyingNames = valueQualifyingNames;
return this;
}
public Builder keyQualifyingTargetType(TypeMirror keyQualifyingTargetType) {
this.keyQualifyingTargetType = keyQualifyingTargetType;
return this;
}
public Builder valueQualifyingTargetType(TypeMirror valueQualifyingTargetType) {
this.valueQualifyingTargetType = valueQualifyingTargetType;
return this;
}
public Builder nullValueMappingStrategy(NullValueMappingStrategyPrism nullValueMappingStrategy) { public Builder nullValueMappingStrategy(NullValueMappingStrategyPrism nullValueMappingStrategy) {
this.nullValueMappingStrategy = nullValueMappingStrategy; this.nullValueMappingStrategy = nullValueMappingStrategy;
return this; return this;
} }
public MapMappingMethod build() { public MapMappingMethod build() {
List<Type> sourceTypeParams = first( method.getSourceParameters() ).getType().getTypeParameters(); List<Type> sourceTypeParams = first( method.getSourceParameters() ).getType().getTypeParameters();
@ -136,9 +110,7 @@ public class MapMappingMethod extends MappingMethod {
keyTargetType, keyTargetType,
null, // there is no targetPropertyName null, // there is no targetPropertyName
keyDateFormat, keyDateFormat,
keyQualifiers, keySelectionParameters,
keyQualifyingNames,
keyQualifyingTargetType,
"entry.getKey()", "entry.getKey()",
false false
); );
@ -165,9 +137,7 @@ public class MapMappingMethod extends MappingMethod {
valueTargetType, valueTargetType,
null, // there is no targetPropertyName null, // there is no targetPropertyName
valueDateFormat, valueDateFormat,
valueQualifiers, valueSelectionParameters,
valueQualifyingNames,
valueQualifyingTargetType,
"entry.getValue()", "entry.getValue()",
false false
); );
@ -201,8 +171,7 @@ public class MapMappingMethod extends MappingMethod {
MethodReference factoryMethod = null; MethodReference factoryMethod = null;
if ( !method.isUpdateMethod() ) { if ( !method.isUpdateMethod() ) {
factoryMethod = ctx.getMappingResolver().getFactoryMethod( method, method.getResultType(), null, null, factoryMethod = ctx.getMappingResolver().getFactoryMethod( method, method.getResultType(), null );
null );
} }
keyAssignment = new LocalVarWrapper( keyAssignment, method.getThrownTypes() ); keyAssignment = new LocalVarWrapper( keyAssignment, method.getThrownTypes() );

View File

@ -23,7 +23,6 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import javax.lang.model.element.TypeElement; import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements; import javax.lang.model.util.Elements;
import javax.lang.model.util.Types; import javax.lang.model.util.Types;
@ -31,6 +30,7 @@ import org.mapstruct.ap.internal.model.assignment.Assignment;
import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.common.TypeFactory; import org.mapstruct.ap.internal.model.common.TypeFactory;
import org.mapstruct.ap.internal.model.source.Method; import org.mapstruct.ap.internal.model.source.Method;
import org.mapstruct.ap.internal.model.source.SelectionParameters;
import org.mapstruct.ap.internal.model.source.SourceMethod; import org.mapstruct.ap.internal.model.source.SourceMethod;
import org.mapstruct.ap.internal.option.Options; import org.mapstruct.ap.internal.option.Options;
import org.mapstruct.ap.internal.util.FormattingMessager; import org.mapstruct.ap.internal.util.FormattingMessager;
@ -79,10 +79,7 @@ public class MappingBuilderContext {
* @param targetType return type to match * @param targetType return type to match
* @param targetPropertyName name of the target property * @param targetPropertyName name of the target property
* @param dateFormat used for formatting dates in build in methods that need context information * @param dateFormat used for formatting dates in build in methods that need context information
* @param qualifiers used for further select the appropriate mapping method based on class and name * @param selectionParameters parameters used in the selection process
* @param qualifyingNames see qualifiers, used in combination with with @Named
* @param resultType used for further select the appropriate mapping method based on resultType (bean mapping)
* targetType (Iterable- and MapMapping)
* @param sourceReference call to source type as string * @param sourceReference call to source type as string
* @param preferUpdateMethods selection should prefer update methods when present. * @param preferUpdateMethods selection should prefer update methods when present.
* *
@ -94,10 +91,9 @@ public class MappingBuilderContext {
* <li>null, no assignment found</li> * <li>null, no assignment found</li>
* </ol> * </ol>
*/ */
@SuppressWarnings("checkstyle:parameternumber")
Assignment getTargetAssignment(Method mappingMethod, String mappedElement, Type sourceType, Type targetType, Assignment getTargetAssignment(Method mappingMethod, String mappedElement, Type sourceType, Type targetType,
String targetPropertyName, String dateFormat, List<TypeMirror> qualifiers, String targetPropertyName, String dateFormat,
List<String> qualifyingNames, TypeMirror resultType, String sourceReference, SelectionParameters selectionParameters, String sourceReference,
boolean preferUpdateMethods); boolean preferUpdateMethods);
/** /**
@ -105,16 +101,12 @@ public class MappingBuilderContext {
* *
* @param mappingMethod target mapping method * @param mappingMethod target mapping method
* @param target return type to match * @param target return type to match
* @param qualifiers used for further select the appropriate mapping method based on class and name * @param selectionParameters parameters used in the selection process
* @param qualifyingNames see qualifiers, used in combination with with @Named
* @param resultType used for further select the appropriate mapping method based on resultType (bean mapping)
* targetType (Iterable- and MapMapping) *
* *
* @return a method reference to the factory method, or null if no suitable, or ambiguous method found * @return a method reference to the factory method, or null if no suitable, or ambiguous method found
* *
*/ */
MethodReference getFactoryMethod(Method mappingMethod, Type target, List<TypeMirror> qualifiers, MethodReference getFactoryMethod(Method mappingMethod, Type target, SelectionParameters selectionParameters);
List<String> qualifyingNames, TypeMirror resultType);
Set<VirtualMappingMethod> getUsedVirtualMappings(); Set<VirtualMappingMethod> getUsedVirtualMappings();
} }

View File

@ -58,6 +58,8 @@ public abstract class MappingMethod extends ModelElement {
* *
* @param method method * @param method method
* @param existingVariableNames existingVariableNames * @param existingVariableNames existingVariableNames
* @param beforeMappingReferences
* @param afterMappingReferences
*/ */
protected MappingMethod(Method method, Collection<String> existingVariableNames, protected MappingMethod(Method method, Collection<String> existingVariableNames,
List<LifecycleCallbackMethodReference> beforeMappingReferences, List<LifecycleCallbackMethodReference> beforeMappingReferences,

View File

@ -48,6 +48,7 @@ import org.mapstruct.ap.internal.model.common.ModelElement;
import org.mapstruct.ap.internal.model.common.Parameter; import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.source.ForgedMethod; import org.mapstruct.ap.internal.model.source.ForgedMethod;
import org.mapstruct.ap.internal.model.source.SelectionParameters;
import org.mapstruct.ap.internal.model.source.SourceMethod; import org.mapstruct.ap.internal.model.source.SourceMethod;
import org.mapstruct.ap.internal.model.source.SourceReference; import org.mapstruct.ap.internal.model.source.SourceReference;
import org.mapstruct.ap.internal.model.source.SourceReference.PropertyEntry; import org.mapstruct.ap.internal.model.source.SourceReference.PropertyEntry;
@ -172,24 +173,20 @@ public class PropertyMapping extends ModelElement {
private List<String> qualifyingNames; private List<String> qualifyingNames;
private TypeMirror resultType; private TypeMirror resultType;
private SourceReference sourceReference; private SourceReference sourceReference;
private SelectionParameters selectionParameters;
public PropertyMappingBuilder sourceReference(SourceReference sourceReference) { public PropertyMappingBuilder sourceReference(SourceReference sourceReference) {
this.sourceReference = sourceReference; this.sourceReference = sourceReference;
return this; return this;
} }
public PropertyMappingBuilder qualifiers(List<TypeMirror> qualifiers) { public PropertyMappingBuilder selectionParameters(SelectionParameters selectionParameters) {
this.qualifiers = qualifiers; if ( selectionParameters != null ) {
return this; this.qualifiers = selectionParameters.getQualifiers();
this.qualifyingNames = selectionParameters.getQualifyingNames();
this.resultType = selectionParameters.getResultType();
} }
this.selectionParameters = selectionParameters;
public PropertyMappingBuilder qualifyingNames(List<String> qualifyingNames) {
this.qualifyingNames = qualifyingNames;
return this;
}
public PropertyMappingBuilder resultType(TypeMirror resultType) {
this.resultType = resultType;
return this; return this;
} }
@ -234,9 +231,7 @@ public class PropertyMapping extends ModelElement {
targetType, targetType,
targetPropertyName, targetPropertyName,
dateFormat, dateFormat,
qualifiers, selectionParameters,
qualifyingNames,
resultType,
sourceRefStr, sourceRefStr,
preferUpdateMethods preferUpdateMethods
); );
@ -295,8 +290,7 @@ public class PropertyMapping extends ModelElement {
PropertyMapping build = new ConstantMappingBuilder() PropertyMapping build = new ConstantMappingBuilder()
.constantExpression( '"' + defaultValue + '"' ) .constantExpression( '"' + defaultValue + '"' )
.dateFormat( dateFormat ) .dateFormat( dateFormat )
.qualifiers( qualifiers ) .selectionParameters( selectionParameters )
.resultType( resultType )
.dependsOn( dependsOn ) .dependsOn( dependsOn )
.existingVariableNames( existingVariableNames ) .existingVariableNames( existingVariableNames )
.mappingContext( ctx ) .mappingContext( ctx )
@ -322,8 +316,7 @@ public class PropertyMapping extends ModelElement {
Message.PROPERTYMAPPING_NO_READ_ACCESSOR_FOR_TARGET_TYPE, Message.PROPERTYMAPPING_NO_READ_ACCESSOR_FOR_TARGET_TYPE,
targetPropertyName ); targetPropertyName );
} }
Assignment factoryMethod = Assignment factoryMethod = ctx.getMappingResolver().getFactoryMethod( method, targetType, null );
ctx.getMappingResolver().getFactoryMethod( method, targetType, null, null, null );
result = new UpdateWrapper( result, method.getThrownTypes(), factoryMethod, result = new UpdateWrapper( result, method.getThrownTypes(), factoryMethod,
targetType ); targetType );
} }
@ -403,8 +396,7 @@ public class PropertyMapping extends ModelElement {
Message.PROPERTYMAPPING_NO_READ_ACCESSOR_FOR_TARGET_TYPE, Message.PROPERTYMAPPING_NO_READ_ACCESSOR_FOR_TARGET_TYPE,
targetPropertyName ); targetPropertyName );
} }
Assignment factoryMethod Assignment factoryMethod = ctx.getMappingResolver().getFactoryMethod( method, targetType, null );
= ctx.getMappingResolver().getFactoryMethod( method, targetType, null, null, null );
result = new UpdateWrapper( result, method.getThrownTypes(), factoryMethod, result = new UpdateWrapper( result, method.getThrownTypes(), factoryMethod,
targetType ); targetType );
} }
@ -611,9 +603,7 @@ public class PropertyMapping extends ModelElement {
private String constantExpression; private String constantExpression;
private String dateFormat; private String dateFormat;
private List<TypeMirror> qualifiers; private SelectionParameters selectionParameters;
private List<String> qualifyingNames;
private TypeMirror resultType;
public ConstantMappingBuilder constantExpression(String constantExpression) { public ConstantMappingBuilder constantExpression(String constantExpression) {
this.constantExpression = constantExpression; this.constantExpression = constantExpression;
@ -625,18 +615,8 @@ public class PropertyMapping extends ModelElement {
return this; return this;
} }
public ConstantMappingBuilder qualifiers(List<TypeMirror> qualifiers) { public ConstantMappingBuilder selectionParameters(SelectionParameters selectionParameters) {
this.qualifiers = qualifiers; this.selectionParameters = selectionParameters;
return this;
}
public ConstantMappingBuilder qualifyingNames(List<String> qualifyingNames) {
this.qualifyingNames = qualifyingNames;
return this;
}
public ConstantMappingBuilder resultType(TypeMirror resultType) {
this.resultType = resultType;
return this; return this;
} }
@ -652,9 +632,7 @@ public class PropertyMapping extends ModelElement {
targetType, targetType,
targetPropertyName, targetPropertyName,
dateFormat, dateFormat,
qualifiers, selectionParameters,
qualifyingNames,
resultType,
constantExpression, constantExpression,
method.getMappingTargetParameter() != null method.getMappingTargetParameter() != null
); );
@ -671,7 +649,7 @@ public class PropertyMapping extends ModelElement {
targetPropertyName ); targetPropertyName );
} }
Assignment factoryMethod = Assignment factoryMethod =
ctx.getMappingResolver().getFactoryMethod( method, targetType, null, null, null ); ctx.getMappingResolver().getFactoryMethod( method, targetType, null );
assignment = new UpdateWrapper( assignment, method.getThrownTypes(), factoryMethod, assignment = new UpdateWrapper( assignment, method.getThrownTypes(), factoryMethod,
targetType ); targetType );
} }

View File

@ -18,18 +18,14 @@
*/ */
package org.mapstruct.ap.internal.model.source; package org.mapstruct.ap.internal.model.source;
import java.util.List;
import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.ExecutableElement;
import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import org.mapstruct.ap.internal.prism.BeanMappingPrism; import org.mapstruct.ap.internal.prism.BeanMappingPrism;
import org.mapstruct.ap.internal.prism.NullValueMappingStrategyPrism; import org.mapstruct.ap.internal.prism.NullValueMappingStrategyPrism;
import org.mapstruct.ap.internal.util.FormattingMessager; import org.mapstruct.ap.internal.util.FormattingMessager;
import org.mapstruct.ap.internal.util.Message; import org.mapstruct.ap.internal.util.Message;
/** /**
* Represents an bean mapping as configured via {@code @BeanMapping}. * Represents an bean mapping as configured via {@code @BeanMapping}.
* *
@ -37,9 +33,7 @@ import org.mapstruct.ap.internal.util.Message;
*/ */
public class BeanMapping { public class BeanMapping {
private final List<TypeMirror> qualifiers; private final SelectionParameters selectionParameters;
private final List<String> qualifyingNames;
private final TypeMirror resultType;
private final NullValueMappingStrategyPrism nullValueMappingStrategy; private final NullValueMappingStrategyPrism nullValueMappingStrategy;
public static BeanMapping fromPrism(BeanMappingPrism beanMapping, ExecutableElement method, public static BeanMapping fromPrism(BeanMappingPrism beanMapping, ExecutableElement method,
@ -62,33 +56,21 @@ public class BeanMapping {
messager.printMessage( method, Message.BEANMAPPING_NO_ELEMENTS ); messager.printMessage( method, Message.BEANMAPPING_NO_ELEMENTS );
} }
return new BeanMapping( SelectionParameters cmp = new SelectionParameters(
beanMapping.qualifiedBy(), beanMapping.qualifiedBy(),
beanMapping.qualifiedByName(), beanMapping.qualifiedByName(),
resultTypeIsDefined ? beanMapping.resultType() : null, resultTypeIsDefined ? beanMapping.resultType() : null );
nullValueMappingStrategy
); return new BeanMapping(cmp, nullValueMappingStrategy );
} }
private BeanMapping(List<TypeMirror> qualifiers, List<String> qualyfyingNames, TypeMirror mirror, private BeanMapping( SelectionParameters selectionParameters, NullValueMappingStrategyPrism nvms ) {
NullValueMappingStrategyPrism nvms) { this.selectionParameters = selectionParameters;
this.qualifiers = qualifiers;
this.qualifyingNames = qualyfyingNames;
this.resultType = mirror;
this.nullValueMappingStrategy = nvms; this.nullValueMappingStrategy = nvms;
} }
public List<TypeMirror> getQualifiers() { public SelectionParameters getSelectionParameters() {
return qualifiers; return selectionParameters;
}
public List<String> getQualifyingNames() {
return qualifyingNames;
}
public TypeMirror getResultType() {
return resultType;
} }
public NullValueMappingStrategyPrism getNullValueMappingStrategy() { public NullValueMappingStrategyPrism getNullValueMappingStrategy() {

View File

@ -18,13 +18,10 @@
*/ */
package org.mapstruct.ap.internal.model.source; package org.mapstruct.ap.internal.model.source;
import java.util.List;
import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.ExecutableElement;
import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import org.mapstruct.ap.internal.prism.IterableMappingPrism; import org.mapstruct.ap.internal.prism.IterableMappingPrism;
import org.mapstruct.ap.internal.prism.NullValueMappingStrategyPrism; import org.mapstruct.ap.internal.prism.NullValueMappingStrategyPrism;
@ -39,9 +36,7 @@ import org.mapstruct.ap.internal.util.Message;
public class IterableMapping { public class IterableMapping {
private final String dateFormat; private final String dateFormat;
private final List<TypeMirror> qualifiers; private final SelectionParameters selectionParameters;
private final List<String> qualifyingNames;
private final TypeMirror qualifyingElementTargetType;
private final AnnotationMirror mirror; private final AnnotationMirror mirror;
private final AnnotationValue dateFormatAnnotationValue; private final AnnotationValue dateFormatAnnotationValue;
private final NullValueMappingStrategyPrism nullValueMappingStrategy; private final NullValueMappingStrategyPrism nullValueMappingStrategy;
@ -68,24 +63,24 @@ public class IterableMapping {
messager.printMessage( method, Message.ITERABLEMAPPING_NO_ELEMENTS ); messager.printMessage( method, Message.ITERABLEMAPPING_NO_ELEMENTS );
} }
return new IterableMapping(iterableMapping.dateFormat(), SelectionParameters selection = new SelectionParameters(
iterableMapping.qualifiedBy(), iterableMapping.qualifiedBy(),
iterableMapping.qualifiedByName(), iterableMapping.qualifiedByName(),
elementTargetTypeIsDefined ? iterableMapping.elementTargetType() : null, elementTargetTypeIsDefined ? iterableMapping.elementTargetType() : null );
return new IterableMapping(iterableMapping.dateFormat(),
selection,
iterableMapping.mirror, iterableMapping.mirror,
iterableMapping.values.dateFormat(), iterableMapping.values.dateFormat(),
nullValueMappingStrategy nullValueMappingStrategy
); );
} }
private IterableMapping(String dateFormat, List<TypeMirror> qualifiers, List<String> qualifyingNames, private IterableMapping(String dateFormat, SelectionParameters selectionParameters, AnnotationMirror mirror,
TypeMirror resultType, AnnotationMirror mirror, AnnotationValue dateFormatAnnotationValue, AnnotationValue dateFormatAnnotationValue, NullValueMappingStrategyPrism nvms) {
NullValueMappingStrategyPrism nvms) {
this.dateFormat = dateFormat; this.dateFormat = dateFormat;
this.qualifiers = qualifiers; this.selectionParameters = selectionParameters;
this.qualifyingNames = qualifyingNames;
this.qualifyingElementTargetType = resultType;
this.mirror = mirror; this.mirror = mirror;
this.dateFormatAnnotationValue = dateFormatAnnotationValue; this.dateFormatAnnotationValue = dateFormatAnnotationValue;
this.nullValueMappingStrategy = nvms; this.nullValueMappingStrategy = nvms;
@ -95,16 +90,8 @@ public class IterableMapping {
return dateFormat; return dateFormat;
} }
public List<TypeMirror> getQualifiers() { public SelectionParameters getSelectionParameters() {
return qualifiers; return selectionParameters;
}
public List<String> getQualifyingNames() {
return qualifyingNames;
}
public TypeMirror getQualifyingElementTargetType() {
return qualifyingElementTargetType;
} }
public AnnotationMirror getMirror() { public AnnotationMirror getMirror() {

View File

@ -18,12 +18,9 @@
*/ */
package org.mapstruct.ap.internal.model.source; package org.mapstruct.ap.internal.model.source;
import java.util.List;
import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.ExecutableElement;
import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import org.mapstruct.ap.internal.prism.MapMappingPrism; import org.mapstruct.ap.internal.prism.MapMappingPrism;
import org.mapstruct.ap.internal.prism.NullValueMappingStrategyPrism; import org.mapstruct.ap.internal.prism.NullValueMappingStrategyPrism;
@ -38,14 +35,10 @@ import org.mapstruct.ap.internal.util.Message;
public class MapMapping { public class MapMapping {
private final String keyFormat; private final String keyFormat;
private final List<TypeMirror> keyQualifiers; private final SelectionParameters keySelectionParameters;
private final List<String> keyQualifyingNames;
private final String valueFormat; private final String valueFormat;
private final List<TypeMirror> valueQualifiers; private final SelectionParameters valueSelectionParameters;
private final List<String> valueQualifyingNames;
private final AnnotationMirror mirror; private final AnnotationMirror mirror;
private final TypeMirror keyQualifyingTargetType;
private final TypeMirror valueQualifyingTargetType;
private final NullValueMappingStrategyPrism nullValueMappingStrategy; private final NullValueMappingStrategyPrism nullValueMappingStrategy;
public static MapMapping fromPrism(MapMappingPrism mapMapping, ExecutableElement method, public static MapMapping fromPrism(MapMappingPrism mapMapping, ExecutableElement method,
@ -75,33 +68,32 @@ public class MapMapping {
messager.printMessage( method, Message.MAPMAPPING_NO_ELEMENTS ); messager.printMessage( method, Message.MAPMAPPING_NO_ELEMENTS );
} }
SelectionParameters keySelection = new SelectionParameters(
mapMapping.keyQualifiedBy(),
mapMapping.keyQualifiedByName(),
keyTargetTypeIsDefined ? mapMapping.keyTargetType() : null);
SelectionParameters valueSelection = new SelectionParameters(
mapMapping.valueQualifiedBy(),
mapMapping.valueQualifiedByName(),
valueTargetTypeIsDefined ? mapMapping.valueTargetType() : null);
return new MapMapping( return new MapMapping(
mapMapping.keyDateFormat(), mapMapping.keyDateFormat(),
mapMapping.keyQualifiedBy(), keySelection,
mapMapping.keyQualifiedByName(),
keyTargetTypeIsDefined ? mapMapping.keyTargetType() : null,
mapMapping.valueDateFormat(), mapMapping.valueDateFormat(),
mapMapping.valueQualifiedBy(), valueSelection,
mapMapping.valueQualifiedByName(),
valueTargetTypeIsDefined ? mapMapping.valueTargetType() : null,
mapMapping.mirror, mapMapping.mirror,
nullValueMappingStrategy nullValueMappingStrategy
); );
} }
private MapMapping(String keyFormat, List<TypeMirror> keyQualifiers, List<String> keyQualifyingNames, private MapMapping(String keyFormat, SelectionParameters keySelectionParameters, String valueFormat,
TypeMirror keyResultType, String valueFormat, List<TypeMirror> valueQualifiers, SelectionParameters valueSelectionParameters, AnnotationMirror mirror, NullValueMappingStrategyPrism nvms ) {
List<String> valueQualifyingNames, TypeMirror valueResultType, AnnotationMirror mirror,
NullValueMappingStrategyPrism nvms ) {
this.keyFormat = keyFormat; this.keyFormat = keyFormat;
this.keyQualifiers = keyQualifiers; this.keySelectionParameters = keySelectionParameters;
this.keyQualifyingNames = keyQualifyingNames;
this.keyQualifyingTargetType = keyResultType;
this.valueFormat = valueFormat; this.valueFormat = valueFormat;
this.valueQualifiers = valueQualifiers; this.valueSelectionParameters = valueSelectionParameters;
this.valueQualifyingNames = valueQualifyingNames;
this.valueQualifyingTargetType = valueResultType;
this.mirror = mirror; this.mirror = mirror;
this.nullValueMappingStrategy = nvms; this.nullValueMappingStrategy = nvms;
} }
@ -110,32 +102,16 @@ public class MapMapping {
return keyFormat; return keyFormat;
} }
public List<TypeMirror> getKeyQualifiers() { public SelectionParameters getKeySelectionParameters() {
return keyQualifiers; return keySelectionParameters;
}
public List<String> getKeyQualifyingNames() {
return keyQualifyingNames;
}
public TypeMirror getKeyQualifyingTargetType() {
return keyQualifyingTargetType;
} }
public String getValueFormat() { public String getValueFormat() {
return valueFormat; return valueFormat;
} }
public List<TypeMirror> getValueQualifiers() { public SelectionParameters getValueSelectionParameters() {
return valueQualifiers; return valueSelectionParameters;
}
public List<String> getValueQualifyingNames() {
return valueQualifyingNames;
}
public TypeMirror getValueQualifyingTargetType() {
return valueQualifyingTargetType;
} }
public AnnotationMirror getMirror() { public AnnotationMirror getMirror() {

View File

@ -56,9 +56,8 @@ public class Mapping {
private final String targetName; private final String targetName;
private final String dateFormat; private final String dateFormat;
private final String defaultValue; private final String defaultValue;
private final List<TypeMirror> qualifiers; private final SelectionParameters selectionParameters;
private final List<String> qualifyingNames;
private final TypeMirror resultType;
private final boolean isIgnored; private final boolean isIgnored;
private final List<String> dependsOn; private final List<String> dependsOn;
@ -136,10 +135,14 @@ public class Mapping {
String defaultValue = mappingPrism.values.defaultValue() == null ? null : mappingPrism.defaultValue(); String defaultValue = mappingPrism.values.defaultValue() == null ? null : mappingPrism.defaultValue();
boolean resultTypeIsDefined = mappingPrism.values.resultType() != null; boolean resultTypeIsDefined = mappingPrism.values.resultType() != null;
TypeMirror resultType = resultTypeIsDefined ? mappingPrism.resultType() : null;
List<String> dependsOn = List<String> dependsOn =
mappingPrism.dependsOn() != null ? mappingPrism.dependsOn() : Collections.<String>emptyList(); mappingPrism.dependsOn() != null ? mappingPrism.dependsOn() : Collections.<String>emptyList();
SelectionParameters selectionParams = new SelectionParameters(
mappingPrism.qualifiedBy(),
mappingPrism.qualifiedByName(),
resultTypeIsDefined ? mappingPrism.resultType() : null);
return new Mapping( return new Mapping(
source, source,
constant, constant,
@ -147,39 +150,34 @@ public class Mapping {
mappingPrism.target(), mappingPrism.target(),
dateFormat, dateFormat,
defaultValue, defaultValue,
mappingPrism.qualifiedBy(),
mappingPrism.qualifiedByName(),
mappingPrism.ignore(), mappingPrism.ignore(),
mappingPrism.mirror, mappingPrism.mirror,
mappingPrism.values.source(), mappingPrism.values.source(),
mappingPrism.values.target(), mappingPrism.values.target(),
selectionParams,
mappingPrism.values.dependsOn(), mappingPrism.values.dependsOn(),
resultType,
dependsOn dependsOn
); );
} }
@SuppressWarnings("checkstyle:parameternumber") @SuppressWarnings("checkstyle:parameternumber")
private Mapping(String sourceName, String constant, String javaExpression, String targetName, private Mapping(String sourceName, String constant, String javaExpression, String targetName,
String dateFormat, String defaultValue, List<TypeMirror> qualifiers, String dateFormat, String defaultValue, boolean isIgnored, AnnotationMirror mirror,
List<String> qualifyingNames, boolean isIgnored, AnnotationMirror mirror,
AnnotationValue sourceAnnotationValue, AnnotationValue targetAnnotationValue, AnnotationValue sourceAnnotationValue, AnnotationValue targetAnnotationValue,
AnnotationValue dependsOnAnnotationValue, SelectionParameters selectionParameters, AnnotationValue dependsOnAnnotationValue,
TypeMirror resultType, List<String> dependsOn) { List<String> dependsOn) {
this.sourceName = sourceName; this.sourceName = sourceName;
this.constant = constant; this.constant = constant;
this.javaExpression = javaExpression; this.javaExpression = javaExpression;
this.targetName = targetName; this.targetName = targetName;
this.dateFormat = dateFormat; this.dateFormat = dateFormat;
this.defaultValue = defaultValue; this.defaultValue = defaultValue;
this.qualifiers = qualifiers;
this.qualifyingNames = qualifyingNames;
this.isIgnored = isIgnored; this.isIgnored = isIgnored;
this.mirror = mirror; this.mirror = mirror;
this.sourceAnnotationValue = sourceAnnotationValue; this.sourceAnnotationValue = sourceAnnotationValue;
this.targetAnnotationValue = targetAnnotationValue; this.targetAnnotationValue = targetAnnotationValue;
this.selectionParameters = selectionParameters;
this.dependsOnAnnotationValue = dependsOnAnnotationValue; this.dependsOnAnnotationValue = dependsOnAnnotationValue;
this.resultType = resultType;
this.dependsOn = dependsOn; this.dependsOn = dependsOn;
} }
@ -249,12 +247,8 @@ public class Mapping {
return defaultValue; return defaultValue;
} }
public List<TypeMirror> getQualifiers() { public SelectionParameters getSelectionParameters() {
return qualifiers; return selectionParameters;
}
public List<String> getQualifyingNames() {
return qualifyingNames;
} }
public boolean isIgnored() { public boolean isIgnored() {
@ -281,10 +275,6 @@ public class Mapping {
return sourceReference; return sourceReference;
} }
public TypeMirror getResultType() {
return resultType;
}
public List<String> getDependsOn() { public List<String> getDependsOn() {
return dependsOn; return dependsOn;
} }
@ -331,14 +321,12 @@ public class Mapping {
sourceName != null ? sourceName : targetName, sourceName != null ? sourceName : targetName,
dateFormat, dateFormat,
null, null,
qualifiers,
qualifyingNames,
isIgnored, isIgnored,
mirror, mirror,
sourceAnnotationValue, sourceAnnotationValue,
targetAnnotationValue, targetAnnotationValue,
selectionParameters,
dependsOnAnnotationValue, dependsOnAnnotationValue,
null,
Collections.<String>emptyList() Collections.<String>emptyList()
); );
@ -360,14 +348,12 @@ public class Mapping {
targetName, targetName,
dateFormat, dateFormat,
defaultValue, defaultValue,
qualifiers,
qualifyingNames,
isIgnored, isIgnored,
mirror, mirror,
sourceAnnotationValue, sourceAnnotationValue,
targetAnnotationValue, targetAnnotationValue,
selectionParameters,
dependsOnAnnotationValue, dependsOnAnnotationValue,
resultType,
dependsOn dependsOn
); );

View File

@ -0,0 +1,68 @@
/**
* Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.ap.internal.model.source;
import java.util.List;
import javax.lang.model.type.TypeMirror;
/**
* Holding parameters common to the selection process, common to IterableMapping, BeanMapping, PropertyMapping and
* MapMapping
*
* @author Sjaak Derksen
*/
public class SelectionParameters {
private final List<TypeMirror> qualifiers;
private final List<String> qualifyingNames;
private final TypeMirror resultType;
public SelectionParameters(List<TypeMirror> qualifiers, List<String> qualifyingNames, TypeMirror resultType ) {
this.qualifiers = qualifiers;
this.qualifyingNames = qualifyingNames;
this.resultType = resultType;
}
/**
*
* @return qualifiers used for further select the appropriate mapping method based on class and name
*/
public List<TypeMirror> getQualifiers() {
return qualifiers;
}
/**
*
* @return qualifyingNames see qualifiers, used in combination with with @Named
*/
public List<String> getQualifyingNames() {
return qualifyingNames;
}
/**
*
* @return resultType used for further select the appropriate mapping method based on resultType (bean mapping)
* targetType (Iterable- and MapMapping)
*/
public TypeMirror getResultType() {
return resultType;
}
}

View File

@ -18,9 +18,11 @@
*/ */
package org.mapstruct.ap.internal.model.source.selector; package org.mapstruct.ap.internal.model.source.selector;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeMirror;
import org.mapstruct.ap.internal.model.source.SelectionParameters;
/** /**
* This class groups the selection criteria in one class * This class groups the selection criteria in one class
@ -29,18 +31,23 @@ import javax.lang.model.type.TypeMirror;
*/ */
public class SelectionCriteria { public class SelectionCriteria {
private final List<TypeMirror> qualifiers; private final List<TypeMirror> qualifiers = new ArrayList<TypeMirror>();
private final List<String> qualifiedByNames; private final List<String> qualifiedByNames = new ArrayList<String>();
private final String targetPropertyName; private final String targetPropertyName;
private final TypeMirror qualifyingResultType; private final TypeMirror qualifyingResultType;
private boolean preferUpdateMapping; private boolean preferUpdateMapping;
public SelectionCriteria(List<TypeMirror> qualifiers, List<String> qualifiedByNames, String targetPropertyName, public SelectionCriteria( SelectionParameters selectionParameters, String targetPropertyName,
TypeMirror qualifyingResultType, boolean preferUpdateMapping ) { boolean preferUpdateMapping ) {
this.qualifiers = qualifiers; if ( selectionParameters != null ) {
this.qualifiedByNames = qualifiedByNames; qualifiers.addAll( selectionParameters.getQualifiers() );
qualifiedByNames.addAll( selectionParameters.getQualifyingNames() );
qualifyingResultType = selectionParameters.getResultType();
}
else {
this.qualifyingResultType = null;
}
this.targetPropertyName = targetPropertyName; this.targetPropertyName = targetPropertyName;
this.qualifyingResultType = qualifyingResultType;
this.preferUpdateMapping = preferUpdateMapping; this.preferUpdateMapping = preferUpdateMapping;
} }

View File

@ -50,6 +50,7 @@ import org.mapstruct.ap.internal.model.MappingMethod;
import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.common.TypeFactory; import org.mapstruct.ap.internal.model.common.TypeFactory;
import org.mapstruct.ap.internal.model.source.MappingOptions; import org.mapstruct.ap.internal.model.source.MappingOptions;
import org.mapstruct.ap.internal.model.source.SelectionParameters;
import org.mapstruct.ap.internal.model.source.SourceMethod; import org.mapstruct.ap.internal.model.source.SourceMethod;
import org.mapstruct.ap.internal.option.Options; import org.mapstruct.ap.internal.option.Options;
import org.mapstruct.ap.internal.prism.DecoratedWithPrism; import org.mapstruct.ap.internal.prism.DecoratedWithPrism;
@ -268,16 +269,12 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
IterableMappingMethod.Builder builder = new IterableMappingMethod.Builder(); IterableMappingMethod.Builder builder = new IterableMappingMethod.Builder();
String dateFormat = null; String dateFormat = null;
List<TypeMirror> qualifiers = null; SelectionParameters selectionParameters = null;
List<String> qualifyingNames = null;
TypeMirror qualifyingElementTargetType = null;
NullValueMappingStrategyPrism nullValueMappingStrategy = null; NullValueMappingStrategyPrism nullValueMappingStrategy = null;
if ( mappingOptions.getIterableMapping() != null ) { if ( mappingOptions.getIterableMapping() != null ) {
dateFormat = mappingOptions.getIterableMapping().getDateFormat(); dateFormat = mappingOptions.getIterableMapping().getDateFormat();
qualifiers = mappingOptions.getIterableMapping().getQualifiers(); selectionParameters = mappingOptions.getIterableMapping().getSelectionParameters();
qualifyingNames = mappingOptions.getIterableMapping().getQualifyingNames();
qualifyingElementTargetType = mappingOptions.getIterableMapping().getQualifyingElementTargetType();
nullValueMappingStrategy = mappingOptions.getIterableMapping().getNullValueMappingStrategy(); nullValueMappingStrategy = mappingOptions.getIterableMapping().getNullValueMappingStrategy();
} }
@ -285,9 +282,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
.mappingContext( mappingContext ) .mappingContext( mappingContext )
.method( method ) .method( method )
.dateFormat( dateFormat ) .dateFormat( dateFormat )
.qualifiers( qualifiers ) .selectionParameters( selectionParameters )
.qualifyingNames( qualifyingNames )
.qualifyingElementTargetType( qualifyingElementTargetType )
.nullValueMappingStrategy( nullValueMappingStrategy ) .nullValueMappingStrategy( nullValueMappingStrategy )
.build(); .build();
@ -300,23 +295,15 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
String keyDateFormat = null; String keyDateFormat = null;
String valueDateFormat = null; String valueDateFormat = null;
List<TypeMirror> keyQualifiers = null; SelectionParameters keySelectionParameters = null;
List<String> keyQualifyingNames = null; SelectionParameters valueSelectionParameters = null;
List<TypeMirror> valueQualifiers = null;
List<String> valueQualifyingNames = null;
TypeMirror keyQualifyingTargetType = null;
TypeMirror valueQualifyingTargetType = null;
NullValueMappingStrategyPrism nullValueMappingStrategy = null; NullValueMappingStrategyPrism nullValueMappingStrategy = null;
if ( mappingOptions.getMapMapping() != null ) { if ( mappingOptions.getMapMapping() != null ) {
keyDateFormat = mappingOptions.getMapMapping().getKeyFormat(); keyDateFormat = mappingOptions.getMapMapping().getKeyFormat();
keySelectionParameters = mappingOptions.getMapMapping().getKeySelectionParameters();
valueSelectionParameters = mappingOptions.getMapMapping().getValueSelectionParameters();
valueDateFormat = mappingOptions.getMapMapping().getValueFormat(); valueDateFormat = mappingOptions.getMapMapping().getValueFormat();
keyQualifiers = mappingOptions.getMapMapping().getKeyQualifiers();
keyQualifyingNames = mappingOptions.getMapMapping().getKeyQualifyingNames();
valueQualifiers = mappingOptions.getMapMapping().getValueQualifiers();
valueQualifyingNames = mappingOptions.getMapMapping().getValueQualifyingNames();
keyQualifyingTargetType = mappingOptions.getMapMapping().getKeyQualifyingTargetType();
valueQualifyingTargetType = mappingOptions.getMapMapping().getValueQualifyingTargetType();
nullValueMappingStrategy = mappingOptions.getMapMapping().getNullValueMappingStrategy(); nullValueMappingStrategy = mappingOptions.getMapMapping().getNullValueMappingStrategy();
} }
@ -324,13 +311,9 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
.mappingContext( mappingContext ) .mappingContext( mappingContext )
.method( method ) .method( method )
.keyDateFormat( keyDateFormat ) .keyDateFormat( keyDateFormat )
.keySelectionParameters( keySelectionParameters )
.valueDateFormat( valueDateFormat ) .valueDateFormat( valueDateFormat )
.keyQualifiers( keyQualifiers ) .valueSelectionParameters( valueSelectionParameters )
.keyQualifyingNames( keyQualifyingNames )
.valueQualifiers( valueQualifiers )
.valueQualifyingNames( valueQualifyingNames )
.keyQualifyingTargetType( keyQualifyingTargetType )
.valueQualifyingTargetType( valueQualifyingTargetType )
.nullValueMappingStrategy( nullValueMappingStrategy ) .nullValueMappingStrategy( nullValueMappingStrategy )
.build(); .build();
@ -352,24 +335,18 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
else { else {
NullValueMappingStrategyPrism nullValueMappingStrategy = null; NullValueMappingStrategyPrism nullValueMappingStrategy = null;
TypeMirror resultType = null; SelectionParameters selectionParameters = null;
List<TypeMirror> qualifiers = null;
List<String> qualifyingNames = null;
if ( mappingOptions.getBeanMapping() != null ) { if ( mappingOptions.getBeanMapping() != null ) {
nullValueMappingStrategy = mappingOptions.getBeanMapping().getNullValueMappingStrategy(); nullValueMappingStrategy = mappingOptions.getBeanMapping().getNullValueMappingStrategy();
resultType = mappingOptions.getBeanMapping().getResultType(); selectionParameters = mappingOptions.getBeanMapping().getSelectionParameters();
qualifiers = mappingOptions.getBeanMapping().getQualifiers();
qualifyingNames = mappingOptions.getBeanMapping().getQualifyingNames();
} }
BeanMappingMethod.Builder builder = new BeanMappingMethod.Builder(); BeanMappingMethod.Builder builder = new BeanMappingMethod.Builder();
BeanMappingMethod beanMappingMethod = builder BeanMappingMethod beanMappingMethod = builder
.mappingContext( mappingContext ) .mappingContext( mappingContext )
.souceMethod( method ) .souceMethod( method )
.nullValueMappingStrategy( nullValueMappingStrategy ) .nullValueMappingStrategy( nullValueMappingStrategy )
.qualifiers( qualifiers ) .selectionParameters( selectionParameters )
.qualifyingNames( qualifyingNames )
.resultType( resultType )
.build(); .build();
if ( beanMappingMethod != null ) { if ( beanMappingMethod != null ) {

View File

@ -46,6 +46,7 @@ import org.mapstruct.ap.internal.model.common.DefaultConversionContext;
import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.common.TypeFactory; import org.mapstruct.ap.internal.model.common.TypeFactory;
import org.mapstruct.ap.internal.model.source.Method; import org.mapstruct.ap.internal.model.source.Method;
import org.mapstruct.ap.internal.model.source.SelectionParameters;
import org.mapstruct.ap.internal.model.source.SourceMethod; import org.mapstruct.ap.internal.model.source.SourceMethod;
import org.mapstruct.ap.internal.model.source.builtin.BuiltInMappingMethods; import org.mapstruct.ap.internal.model.source.builtin.BuiltInMappingMethods;
import org.mapstruct.ap.internal.model.source.builtin.BuiltInMethod; import org.mapstruct.ap.internal.model.source.builtin.BuiltInMethod;
@ -102,13 +103,12 @@ public class MappingResolverImpl implements MappingResolver {
} }
@Override @Override
@SuppressWarnings("checkstyle:parameternumber")
public Assignment getTargetAssignment(Method mappingMethod, String mappedElement, Type sourceType, public Assignment getTargetAssignment(Method mappingMethod, String mappedElement, Type sourceType,
Type targetType, String targetPropertyName, String dateFormat, List<TypeMirror> qualifiers, Type targetType, String targetPropertyName, String dateFormat, SelectionParameters selectionParameters,
List<String> qualifyingNames, TypeMirror resultType, String sourceReference, boolean preferUpdateMapping) { String sourceReference, boolean preferUpdateMapping) {
SelectionCriteria criteria = SelectionCriteria criteria =
new SelectionCriteria(qualifiers, qualifyingNames, targetPropertyName, resultType, preferUpdateMapping ); new SelectionCriteria( selectionParameters, targetPropertyName, preferUpdateMapping );
ResolvingAttempt attempt = new ResolvingAttempt( ResolvingAttempt attempt = new ResolvingAttempt(
sourceModel, sourceModel,
@ -128,10 +128,10 @@ public class MappingResolverImpl implements MappingResolver {
} }
@Override @Override
public MethodReference getFactoryMethod( Method mappingMethod, Type targetType, List<TypeMirror> qualifiers, public MethodReference getFactoryMethod( Method mappingMethod, Type targetType,
List<String> qualifyingNames, TypeMirror resultType ) { SelectionParameters selectionParameters ) {
SelectionCriteria criteria = new SelectionCriteria( qualifiers, qualifyingNames, null, resultType, false ); SelectionCriteria criteria = new SelectionCriteria( selectionParameters, null, false );
ResolvingAttempt attempt = new ResolvingAttempt( ResolvingAttempt attempt = new ResolvingAttempt(
sourceModel, sourceModel,