#499 Extracting base class for shared mapping builder properties

This commit is contained in:
Gunnar Morling 2015-03-09 23:48:44 +01:00
parent c030535ebe
commit 0674a8780b
3 changed files with 56 additions and 135 deletions

View File

@ -186,6 +186,7 @@
<module name="InterfaceIsType"/> <module name="InterfaceIsType"/>
<module name="VisibilityModifier"> <module name="VisibilityModifier">
<property name="packageAllowed" value="true" /> <property name="packageAllowed" value="true" />
<property name="protectedAllowed" value="true" />
</module> </module>

View File

@ -80,7 +80,7 @@ public class BeanMappingMethod extends MappingMethod {
private List<TypeMirror> qualifiers; private List<TypeMirror> qualifiers;
private NullValueMappingStrategyPrism nullValueMappingStrategy; private NullValueMappingStrategyPrism nullValueMappingStrategy;
private TypeMirror resultTypeMirror; private TypeMirror resultTypeMirror;
private final Collection<String> existingVariableNames = new HashSet<String>(); private final Set<String> existingVariableNames = new HashSet<String>();
public Builder mappingContext(MappingBuilderContext mappingContext) { public Builder mappingContext(MappingBuilderContext mappingContext) {
this.ctx = mappingContext; this.ctx = mappingContext;
@ -283,7 +283,7 @@ public class BeanMappingMethod extends MappingMethod {
// as possible before we stop analysing // as possible before we stop analysing
propertyMapping = new PropertyMappingBuilder() propertyMapping = new PropertyMappingBuilder()
.mappingContext( ctx ) .mappingContext( ctx )
.souceMethod( method ) .sourceMethod( method )
.targetWriteAccessor( targetWriteAccessor ) .targetWriteAccessor( targetWriteAccessor )
.targetReadAccessor( getTargetPropertyReadAccessor( mapping.getTargetName() ) ) .targetReadAccessor( getTargetPropertyReadAccessor( mapping.getTargetName() ) )
.targetPropertyName( mapping.getTargetName() ) .targetPropertyName( mapping.getTargetName() )
@ -327,7 +327,7 @@ public class BeanMappingMethod extends MappingMethod {
propertyMapping = new JavaExpressionMappingBuilder() propertyMapping = new JavaExpressionMappingBuilder()
.mappingContext( ctx ) .mappingContext( ctx )
.souceMethod( method ) .sourceMethod( method )
.javaExpression( mapping.getJavaExpression() ) .javaExpression( mapping.getJavaExpression() )
.existingVariableNames( existingVariableNames ) .existingVariableNames( existingVariableNames )
.targetWriteAccessor( targetWriteAccessor ) .targetWriteAccessor( targetWriteAccessor )
@ -405,7 +405,7 @@ public class BeanMappingMethod extends MappingMethod {
newPropertyMapping = new PropertyMappingBuilder() newPropertyMapping = new PropertyMappingBuilder()
.mappingContext( ctx ) .mappingContext( ctx )
.souceMethod( method ) .sourceMethod( method )
.targetWriteAccessor( targetProperty.getValue() ) .targetWriteAccessor( targetProperty.getValue() )
.targetReadAccessor( getTargetPropertyReadAccessor( targetProperty.getKey() ) ) .targetReadAccessor( getTargetPropertyReadAccessor( targetProperty.getKey() ) )
.targetPropertyName( targetProperty.getKey() ) .targetPropertyName( targetProperty.getKey() )
@ -469,7 +469,7 @@ public class BeanMappingMethod extends MappingMethod {
PropertyMapping propertyMapping = new PropertyMappingBuilder() PropertyMapping propertyMapping = new PropertyMappingBuilder()
.mappingContext( ctx ) .mappingContext( ctx )
.souceMethod( method ) .sourceMethod( method )
.targetWriteAccessor( targetProperty.getValue() ) .targetWriteAccessor( targetProperty.getValue() )
.targetReadAccessor( getTargetPropertyReadAccessor( targetProperty.getKey() ) ) .targetReadAccessor( getTargetPropertyReadAccessor( targetProperty.getKey() ) )
.targetPropertyName( targetProperty.getKey() ) .targetPropertyName( targetProperty.getKey() )

View File

@ -19,7 +19,6 @@
package org.mapstruct.ap.model; package org.mapstruct.ap.model;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -68,45 +67,60 @@ public class PropertyMapping extends ModelElement {
private final Assignment assignment; private final Assignment assignment;
private final List<String> dependsOn; private final List<String> dependsOn;
public static class PropertyMappingBuilder { @SuppressWarnings("unchecked")
private static class MappingBuilderBase<T extends MappingBuilderBase<T>> {
protected MappingBuilderContext ctx;
protected SourceMethod method;
protected ExecutableElement targetWriteAccessor;
protected ExecutableElement targetReadAccessor;
protected String targetPropertyName;
protected List<String> dependsOn;
protected Set<String> existingVariableNames;
public T mappingContext(MappingBuilderContext mappingContext) {
this.ctx = mappingContext;
return (T) this;
}
public T sourceMethod(SourceMethod sourceMethod) {
this.method = sourceMethod;
return (T) this;
}
public T targetReadAccessor(ExecutableElement targetReadAccessor) {
this.targetReadAccessor = targetReadAccessor;
return (T) this;
}
public T targetWriteAccessor(ExecutableElement targetWriteAccessor) {
this.targetWriteAccessor = targetWriteAccessor;
return (T) this;
}
public T targetPropertyName(String targetPropertyName) {
this.targetPropertyName = targetPropertyName;
return (T) this;
}
public T dependsOn(List<String> dependsOn) {
this.dependsOn = dependsOn;
return (T) this;
}
public T existingVariableNames(Set<String> existingVariableNames) {
this.existingVariableNames = existingVariableNames;
return (T) this;
}
}
public static class PropertyMappingBuilder extends MappingBuilderBase<PropertyMappingBuilder> {
// initial properties // initial properties
private MappingBuilderContext ctx;
private SourceMethod method;
private ExecutableElement targetWriteAccessor;
private ExecutableElement targetReadAccessor;
private String targetPropertyName;
private String dateFormat; private String dateFormat;
private List<TypeMirror> qualifiers; private List<TypeMirror> qualifiers;
private TypeMirror resultType; private TypeMirror resultType;
private SourceReference sourceReference; private SourceReference sourceReference;
private Collection<String> existingVariableNames;
private List<String> dependsOn;
public PropertyMappingBuilder mappingContext(MappingBuilderContext mappingContext) {
this.ctx = mappingContext;
return this;
}
public PropertyMappingBuilder souceMethod(SourceMethod sourceMethod) {
this.method = sourceMethod;
return this;
}
public PropertyMappingBuilder targetReadAccessor(ExecutableElement targetReadAccessor) {
this.targetReadAccessor = targetReadAccessor;
return this;
}
public PropertyMappingBuilder targetWriteAccessor(ExecutableElement targetWriteAccessor) {
this.targetWriteAccessor = targetWriteAccessor;
return this;
}
public PropertyMappingBuilder targetPropertyName(String targetPropertyName) {
this.targetPropertyName = targetPropertyName;
return this;
}
public PropertyMappingBuilder sourceReference(SourceReference sourceReference) { public PropertyMappingBuilder sourceReference(SourceReference sourceReference) {
this.sourceReference = sourceReference; this.sourceReference = sourceReference;
@ -128,16 +142,6 @@ public class PropertyMapping extends ModelElement {
return this; return this;
} }
public PropertyMappingBuilder existingVariableNames(Collection<String> existingVariableNames) {
this.existingVariableNames = existingVariableNames;
return this;
}
public PropertyMappingBuilder dependsOn(List<String> dependsOn) {
this.dependsOn = dependsOn;
return this;
}
private enum TargetWriteAccessorType { private enum TargetWriteAccessorType {
GETTER, GETTER,
SETTER, SETTER,
@ -506,50 +510,18 @@ public class PropertyMapping extends ModelElement {
} }
} }
public static class ConstantMappingBuilder { public static class ConstantMappingBuilder extends MappingBuilderBase<ConstantMappingBuilder> {
private MappingBuilderContext ctx;
private SourceMethod method;
private String constantExpression; private String constantExpression;
private String targetPropertyName;
private ExecutableElement targetWriteAccessor;
private ExecutableElement targetReadAccessor;
private String dateFormat; private String dateFormat;
private List<TypeMirror> qualifiers; private List<TypeMirror> qualifiers;
private TypeMirror resultType; private TypeMirror resultType;
private Collection<String> existingVariableNames;
private List<String> dependsOn;
public ConstantMappingBuilder mappingContext(MappingBuilderContext mappingContext) {
this.ctx = mappingContext;
return this;
}
public ConstantMappingBuilder sourceMethod(SourceMethod sourceMethod) {
this.method = sourceMethod;
return this;
}
public ConstantMappingBuilder constantExpression(String constantExpression) { public ConstantMappingBuilder constantExpression(String constantExpression) {
this.constantExpression = constantExpression; this.constantExpression = constantExpression;
return this; return this;
} }
public ConstantMappingBuilder targetWriteAccessor(ExecutableElement targetAccessor) {
this.targetWriteAccessor = targetAccessor;
return this;
}
public ConstantMappingBuilder targetReadAccessor(ExecutableElement targetReadAccessor) {
this.targetReadAccessor = targetReadAccessor;
return this;
}
public ConstantMappingBuilder targetPropertyName(String targetPropertyName) {
this.targetPropertyName = targetPropertyName;
return this;
}
public ConstantMappingBuilder dateFormat(String dateFormat) { public ConstantMappingBuilder dateFormat(String dateFormat) {
this.dateFormat = dateFormat; this.dateFormat = dateFormat;
return this; return this;
@ -565,16 +537,6 @@ public class PropertyMapping extends ModelElement {
return this; return this;
} }
public ConstantMappingBuilder existingVariableNames(Collection<String> existingVariableNames) {
this.existingVariableNames = existingVariableNames;
return this;
}
public ConstantMappingBuilder dependsOn(List<String> dependsOn) {
this.dependsOn = dependsOn;
return this;
}
public PropertyMapping build() { public PropertyMapping build() {
// source // source
@ -656,57 +618,15 @@ public class PropertyMapping extends ModelElement {
} }
} }
public static class JavaExpressionMappingBuilder { public static class JavaExpressionMappingBuilder extends MappingBuilderBase<JavaExpressionMappingBuilder> {
private MappingBuilderContext ctx;
private SourceMethod method;
private String javaExpression; private String javaExpression;
private Collection<String> existingVariableNames;
private String targetPropertyName;
private List<String> dependsOn;
private ExecutableElement targetWriteAccessor;
private ExecutableElement targetReadAccessor;
public JavaExpressionMappingBuilder mappingContext(MappingBuilderContext mappingContext) {
this.ctx = mappingContext;
return this;
}
public JavaExpressionMappingBuilder souceMethod(SourceMethod sourceMethod) {
this.method = sourceMethod;
return this;
}
public JavaExpressionMappingBuilder javaExpression(String javaExpression) { public JavaExpressionMappingBuilder javaExpression(String javaExpression) {
this.javaExpression = javaExpression; this.javaExpression = javaExpression;
return this; return this;
} }
public JavaExpressionMappingBuilder targetWriteAccessor(ExecutableElement targetWriteAccessor) {
this.targetWriteAccessor = targetWriteAccessor;
return this;
}
public JavaExpressionMappingBuilder targetReadAccessor(ExecutableElement targetReadAccessor) {
this.targetReadAccessor = targetReadAccessor;
return this;
}
public JavaExpressionMappingBuilder existingVariableNames(Collection<String> existingVariableNames) {
this.existingVariableNames = existingVariableNames;
return this;
}
public JavaExpressionMappingBuilder targetPropertyName(String targetPropertyName) {
this.targetPropertyName = targetPropertyName;
return this;
}
public JavaExpressionMappingBuilder dependsOn(List<String> dependsOn) {
this.dependsOn = dependsOn;
return this;
}
public PropertyMapping build() { public PropertyMapping build() {
Assignment assignment = AssignmentFactory.createDirect( javaExpression ); Assignment assignment = AssignmentFactory.createDirect( javaExpression );