This commit is contained in:
Gunnar Morling 2014-10-12 18:48:13 +02:00
parent b1e184811f
commit e94acd56a4

View File

@ -18,13 +18,8 @@
*/ */
package org.mapstruct.ap.model; package org.mapstruct.ap.model;
import static org.mapstruct.ap.model.assignment.Assignment.AssignmentType.DIRECT;
import static org.mapstruct.ap.model.assignment.Assignment.AssignmentType.TYPE_CONVERTED;
import static org.mapstruct.ap.model.assignment.Assignment.AssignmentType.TYPE_CONVERTED_MAPPED;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.ExecutableElement;
import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic; import javax.tools.Diagnostic;
@ -44,6 +39,10 @@ import org.mapstruct.ap.model.source.Mapping;
import org.mapstruct.ap.model.source.SourceMethod; import org.mapstruct.ap.model.source.SourceMethod;
import org.mapstruct.ap.util.Executables; import org.mapstruct.ap.util.Executables;
import static org.mapstruct.ap.model.assignment.Assignment.AssignmentType.DIRECT;
import static org.mapstruct.ap.model.assignment.Assignment.AssignmentType.TYPE_CONVERTED;
import static org.mapstruct.ap.model.assignment.Assignment.AssignmentType.TYPE_CONVERTED_MAPPED;
/** /**
* Represents the mapping between a source and target property, e.g. from * Represents the mapping between a source and target property, e.g. from
@ -75,22 +74,22 @@ public class PropertyMapping extends ModelElement {
return this; return this;
} }
public PropertyMappingBuilder souceMethod( SourceMethod sourceMethod ) { public PropertyMappingBuilder souceMethod(SourceMethod sourceMethod) {
this.method = sourceMethod; this.method = sourceMethod;
return this; return this;
} }
public PropertyMappingBuilder targetAccessor( ExecutableElement targetAccessor ) { public PropertyMappingBuilder targetAccessor(ExecutableElement targetAccessor) {
this.targetAccessor = targetAccessor; this.targetAccessor = targetAccessor;
return this; return this;
} }
public PropertyMappingBuilder targetPropertyName( String targetPropertyName ) { public PropertyMappingBuilder targetPropertyName(String targetPropertyName) {
this.targetPropertyName = targetPropertyName; this.targetPropertyName = targetPropertyName;
return this; return this;
} }
public PropertyMappingBuilder parameter( Parameter parameter ) { public PropertyMappingBuilder parameter(Parameter parameter) {
this.parameter = parameter; this.parameter = parameter;
return this; return this;
} }
@ -128,7 +127,7 @@ public class PropertyMapping extends ModelElement {
for ( Mapping sourceMapping : sourceMappings ) { for ( Mapping sourceMapping : sourceMappings ) {
boolean mapsToOtherTarget = !sourceMapping.getTargetName().equals( targetPropertyName ); boolean mapsToOtherTarget = !sourceMapping.getTargetName().equals( targetPropertyName );
if ( Executables.getPropertyName( sourceAccessor ).equals( sourcePropertyName ) if ( Executables.getPropertyName( sourceAccessor ).equals( sourcePropertyName )
&& !mapsToOtherTarget ) { && !mapsToOtherTarget ) {
return getPropertyMapping( sourceAccessor, dateFormat, qualifiers ); return getPropertyMapping( sourceAccessor, dateFormat, qualifiers );
} }
} }
@ -140,9 +139,9 @@ public class PropertyMapping extends ModelElement {
return null; return null;
} }
private PropertyMapping getPropertyMapping( ExecutableElement sourceAccessor, private PropertyMapping getPropertyMapping(ExecutableElement sourceAccessor,
String dateFormat, String dateFormat,
List<TypeMirror> qualifiers ) { List<TypeMirror> qualifiers) {
Type sourceType; Type sourceType;
Type targetType; Type targetType;
@ -173,14 +172,15 @@ public class PropertyMapping extends ModelElement {
String sourcePropertyName = Executables.getPropertyName( sourceAccessor ); String sourcePropertyName = Executables.getPropertyName( sourceAccessor );
String mappedElement = "property '" + sourcePropertyName + "'"; String mappedElement = "property '" + sourcePropertyName + "'";
Assignment assignment = ctx.getMappingResolver().getTargetAssignment( method, Assignment assignment = ctx.getMappingResolver().getTargetAssignment(
mappedElement, method,
sourceType, mappedElement,
targetType, sourceType,
targetPropertyName, targetType,
dateFormat, targetPropertyName,
qualifiers, dateFormat,
iteratorReference != null ? iteratorReference : sourceReference qualifiers,
iteratorReference != null ? iteratorReference : sourceReference
); );
if ( assignment == null ) { if ( assignment == null ) {
@ -199,7 +199,7 @@ public class PropertyMapping extends ModelElement {
Assignment newCollectionOrMap = null; Assignment newCollectionOrMap = null;
if ( assignment.getType() == DIRECT ) { if ( assignment.getType() == DIRECT ) {
newCollectionOrMap = newCollectionOrMap =
new NewCollectionOrMapWrapper( assignment, targetType.getImportTypes() ); new NewCollectionOrMapWrapper( assignment, targetType.getImportTypes() );
newCollectionOrMap = new SetterWrapper( newCollectionOrMap, method.getThrownTypes() ); newCollectionOrMap = new SetterWrapper( newCollectionOrMap, method.getThrownTypes() );
} }
@ -208,9 +208,9 @@ public class PropertyMapping extends ModelElement {
// target accessor is setter, so wrap the setter in setter map/ collection handling // target accessor is setter, so wrap the setter in setter map/ collection handling
assignment = new SetterCollectionOrMapWrapper( assignment = new SetterCollectionOrMapWrapper(
assignment, assignment,
targetAccessor.getSimpleName().toString(), targetAccessor.getSimpleName().toString(),
newCollectionOrMap newCollectionOrMap
); );
} }
else { else {
@ -232,10 +232,10 @@ public class PropertyMapping extends ModelElement {
if ( targetAccessorType == TargetAccessorType.SETTER ) { if ( targetAccessorType == TargetAccessorType.SETTER ) {
assignment = new SetterWrapper( assignment, method.getThrownTypes() ); assignment = new SetterWrapper( assignment, method.getThrownTypes() );
if ( !sourceType.isPrimitive() if ( !sourceType.isPrimitive()
&& ( assignment.getType() == TYPE_CONVERTED && ( assignment.getType() == TYPE_CONVERTED
|| assignment.getType() == TYPE_CONVERTED_MAPPED || assignment.getType() == TYPE_CONVERTED_MAPPED
|| assignment.getType() == DIRECT && targetType.isPrimitive() ) ) { || assignment.getType() == DIRECT && targetType.isPrimitive() ) ) {
// for primitive types null check is not possible at all, but a conversion needs // for primitive types null check is not possible at all, but a conversion needs
// a null check. // a null check.
assignment = new NullCheckWrapper( assignment ); assignment = new NullCheckWrapper( assignment );
} }
@ -244,10 +244,10 @@ public class PropertyMapping extends ModelElement {
// TargetAccessorType must be ADDER // TargetAccessorType must be ADDER
if ( sourceIsCollection ) { if ( sourceIsCollection ) {
assignment = new AdderWrapper( assignment = new AdderWrapper(
assignment, assignment,
method.getThrownTypes(), method.getThrownTypes(),
sourceReference, sourceReference,
sourceType sourceType
); );
} }
else { else {
@ -260,22 +260,22 @@ public class PropertyMapping extends ModelElement {
} }
else { else {
ctx.getMessager().printMessage( ctx.getMessager().printMessage(
Diagnostic.Kind.ERROR, Diagnostic.Kind.ERROR,
String.format( String.format(
"Can't map property \"%s %s\" to \"%s %s\".", "Can't map property \"%s %s\" to \"%s %s\".",
sourceType, sourceType,
sourcePropertyName, sourcePropertyName,
targetType, targetType,
targetPropertyName targetPropertyName
), ),
method.getExecutable() method.getExecutable()
); );
} }
return new PropertyMapping( return new PropertyMapping(
parameter.getName(), parameter.getName(),
targetAccessor.getSimpleName().toString(), targetAccessor.getSimpleName().toString(),
targetType, targetType,
assignment assignment
); );
} }
@ -286,13 +286,13 @@ public class PropertyMapping extends ModelElement {
if ( sourceType.isCollectionType() && targetType.isCollectionType() ) { if ( sourceType.isCollectionType() && targetType.isCollectionType() ) {
ForgedMethod methodToGenerate = new ForgedMethod( sourceType, targetType, element ); ForgedMethod methodToGenerate = new ForgedMethod( sourceType, targetType, element );
IterableMappingMethod.Builder builder = new IterableMappingMethod.Builder( ); IterableMappingMethod.Builder builder = new IterableMappingMethod.Builder();
IterableMappingMethod iterableMappingMethod = builder IterableMappingMethod iterableMappingMethod = builder
.mappingContext( ctx ) .mappingContext( ctx )
.method( methodToGenerate ) .method( methodToGenerate )
.build(); .build();
if ( !ctx.getMappingsToGenerate().contains( iterableMappingMethod ) ) { if ( !ctx.getMappingsToGenerate().contains( iterableMappingMethod ) ) {
ctx.getMappingsToGenerate().add( iterableMappingMethod ); ctx.getMappingsToGenerate().add( iterableMappingMethod );
@ -305,11 +305,11 @@ public class PropertyMapping extends ModelElement {
ForgedMethod methodToGenerate = new ForgedMethod( sourceType, targetType, element ); ForgedMethod methodToGenerate = new ForgedMethod( sourceType, targetType, element );
MapMappingMethod.Builder builder = new MapMappingMethod.Builder( ); MapMappingMethod.Builder builder = new MapMappingMethod.Builder();
MapMappingMethod mapMappingMethod = builder MapMappingMethod mapMappingMethod = builder
.mappingContext( ctx ) .mappingContext( ctx )
.method( methodToGenerate ) .method( methodToGenerate )
.build(); .build();
if ( !ctx.getMappingsToGenerate().contains( mapMappingMethod ) ) { if ( !ctx.getMappingsToGenerate().contains( mapMappingMethod ) ) {
ctx.getMappingsToGenerate().add( mapMappingMethod ); ctx.getMappingsToGenerate().add( mapMappingMethod );
@ -335,27 +335,27 @@ public class PropertyMapping extends ModelElement {
return this; return this;
} }
public ConstantMappingBuilder sourceMethod( SourceMethod sourceMethod ) { public ConstantMappingBuilder sourceMethod(SourceMethod sourceMethod) {
this.method = sourceMethod; this.method = sourceMethod;
return this; return this;
} }
public ConstantMappingBuilder constantExpression( String constantExpression ) { public ConstantMappingBuilder constantExpression(String constantExpression) {
this.constantExpression = constantExpression; this.constantExpression = constantExpression;
return this; return this;
} }
public ConstantMappingBuilder targetAccessor( ExecutableElement targetAccessor ) { public ConstantMappingBuilder targetAccessor(ExecutableElement targetAccessor) {
this.targetAccessor = targetAccessor; this.targetAccessor = targetAccessor;
return this; return this;
} }
public ConstantMappingBuilder dateFormat( String dateFormat ) { public ConstantMappingBuilder dateFormat(String dateFormat) {
this.dateFormat = dateFormat; this.dateFormat = dateFormat;
return this; return this;
} }
public ConstantMappingBuilder qualifiers( List<TypeMirror> qualifiers ) { public ConstantMappingBuilder qualifiers(List<TypeMirror> qualifiers) {
this.qualifiers = qualifiers; this.qualifiers = qualifiers;
return this; return this;
} }
@ -377,14 +377,15 @@ public class PropertyMapping extends ModelElement {
String targetPropertyName = Executables.getPropertyName( targetAccessor ); String targetPropertyName = Executables.getPropertyName( targetAccessor );
Assignment assignment = ctx.getMappingResolver().getTargetAssignment( method, Assignment assignment = ctx.getMappingResolver().getTargetAssignment(
mappedElement, method,
sourceType, mappedElement,
targetType, sourceType,
targetPropertyName, targetType,
dateFormat, targetPropertyName,
qualifiers, dateFormat,
constantExpression qualifiers,
constantExpression
); );
if ( assignment != null ) { if ( assignment != null ) {
@ -399,15 +400,15 @@ public class PropertyMapping extends ModelElement {
} }
else { else {
ctx.getMessager().printMessage( ctx.getMessager().printMessage(
Diagnostic.Kind.ERROR, Diagnostic.Kind.ERROR,
String.format( String.format(
"Can't map \"%s %s\" to \"%s %s\".", "Can't map \"%s %s\" to \"%s %s\".",
sourceType, sourceType,
constantExpression, constantExpression,
targetType, targetType,
targetPropertyName targetPropertyName
), ),
method.getExecutable() method.getExecutable()
); );
} }
@ -422,22 +423,22 @@ public class PropertyMapping extends ModelElement {
private String javaExpression; private String javaExpression;
private ExecutableElement targetAccessor; private ExecutableElement targetAccessor;
public JavaExpressionMappingBuilder mappingContext(MappingBuilderContext mappingContext) { public JavaExpressionMappingBuilder mappingContext(MappingBuilderContext mappingContext) {
this.ctx = mappingContext; this.ctx = mappingContext;
return this; return this;
} }
public JavaExpressionMappingBuilder souceMethod( SourceMethod sourceMethod ) { public JavaExpressionMappingBuilder souceMethod(SourceMethod sourceMethod) {
this.method = sourceMethod; this.method = sourceMethod;
return this; return this;
} }
public JavaExpressionMappingBuilder javaExpression( String javaExpression ) { public JavaExpressionMappingBuilder javaExpression(String javaExpression) {
this.javaExpression = javaExpression; this.javaExpression = javaExpression;
return this; return this;
} }
public JavaExpressionMappingBuilder targetAccessor( ExecutableElement targetAccessor ) { public JavaExpressionMappingBuilder targetAccessor(ExecutableElement targetAccessor) {
this.targetAccessor = targetAccessor; this.targetAccessor = targetAccessor;
return this; return this;
} }