Skip to content

Commit

Permalink
mapstruct#1742 refactor accessors (make consistent) to have accessed …
Browse files Browse the repository at this point in the history
…type always available
  • Loading branch information
sjaakd committed May 2, 2019
1 parent f8a713e commit 2215f17
Show file tree
Hide file tree
Showing 18 changed files with 354 additions and 258 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ private void applyPropertyNameBasedMapping() {
Accessor sourceReadAccessor =
sourceParameter.getType().getPropertyReadAccessors().get( targetPropertyName );

ExecutableElementAccessor sourcePresenceChecker =
Accessor sourcePresenceChecker =
sourceParameter.getType().getPropertyPresenceCheckers().get( targetPropertyName );

if ( sourceReadAccessor != null ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.mapstruct.ap.internal.prism.NullValuePropertyMappingStrategyPrism;
import org.mapstruct.ap.internal.util.Message;
import org.mapstruct.ap.internal.util.accessor.Accessor;
import org.mapstruct.ap.internal.util.accessor.AccessorType;

import static org.mapstruct.ap.internal.prism.NullValuePropertyMappingStrategyPrism.SET_TO_DEFAULT;
import static org.mapstruct.ap.internal.prism.NullValuePropertyMappingStrategyPrism.SET_TO_NULL;
Expand Down Expand Up @@ -57,7 +58,7 @@ public class CollectionAssignmentBuilder {
private Accessor targetReadAccessor;
private Type targetType;
private String targetPropertyName;
private PropertyMapping.TargetWriteAccessorType targetAccessorType;
private AccessorType targetAccessorType;
private Assignment assignment;
private SourceRHS sourceRHS;
private NullValueCheckStrategyPrism nvcs;
Expand Down Expand Up @@ -88,7 +89,7 @@ public CollectionAssignmentBuilder targetPropertyName(String targetPropertyName)
return this;
}

public CollectionAssignmentBuilder targetAccessorType(PropertyMapping.TargetWriteAccessorType targetAccessorType) {
public CollectionAssignmentBuilder targetAccessorType(AccessorType targetAccessorType) {
this.targetAccessorType = targetAccessorType;
return this;
}
Expand Down Expand Up @@ -129,8 +130,7 @@ public Assignment build() {
CollectionMappingStrategyPrism cms = method.getMapperConfiguration().getCollectionMappingStrategy();
boolean targetImmutable = cms == CollectionMappingStrategyPrism.TARGET_IMMUTABLE || targetReadAccessor == null;

if ( targetAccessorType == PropertyMapping.TargetWriteAccessorType.SETTER ||
targetAccessorType == PropertyMapping.TargetWriteAccessorType.FIELD ) {
if ( targetAccessorType == AccessorType.SETTER || targetAccessorType == AccessorType.FIELD ) {

if ( result.isCallingUpdateMethod() && !targetImmutable ) {

Expand All @@ -149,7 +149,7 @@ public Assignment build() {
result,
method.getThrownTypes(),
factoryMethod,
PropertyMapping.TargetWriteAccessorType.isFieldAssignment( targetAccessorType ),
targetAccessorType == AccessorType.FIELD,
targetType,
true,
nvpms == SET_TO_NULL && !targetType.isPrimitive(),
Expand All @@ -165,7 +165,7 @@ else if ( method.isUpdateMethod() && !targetImmutable ) {
nvcs,
nvpms,
ctx.getTypeFactory(),
PropertyMapping.TargetWriteAccessorType.isFieldAssignment( targetAccessorType )
targetAccessorType == AccessorType.FIELD
);
}
else if ( result.getType() == Assignment.AssignmentType.DIRECT ||
Expand All @@ -176,7 +176,7 @@ else if ( result.getType() == Assignment.AssignmentType.DIRECT ||
method.getThrownTypes(),
targetType,
ctx.getTypeFactory(),
PropertyMapping.TargetWriteAccessorType.isFieldAssignment( targetAccessorType )
targetAccessorType == AccessorType.FIELD
);
}
else {
Expand All @@ -185,7 +185,7 @@ else if ( result.getType() == Assignment.AssignmentType.DIRECT ||
result,
method.getThrownTypes(),
targetType,
PropertyMapping.TargetWriteAccessorType.isFieldAssignment( targetAccessorType )
targetAccessorType == AccessorType.FIELD
);
}
}
Expand All @@ -203,7 +203,7 @@ else if ( result.getType() == Assignment.AssignmentType.DIRECT ||
result,
method.getThrownTypes(),
targetType,
PropertyMapping.TargetWriteAccessorType.isFieldAssignment( targetAccessorType )
targetAccessorType == AccessorType.FIELD
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.mapstruct.ap.internal.util.Strings;
import org.mapstruct.ap.internal.util.ValueProvider;
import org.mapstruct.ap.internal.util.accessor.Accessor;
import org.mapstruct.ap.internal.util.accessor.AccessorType;

import static org.mapstruct.ap.internal.model.common.Assignment.AssignmentType.DIRECT;
import static org.mapstruct.ap.internal.prism.NullValuePropertyMappingStrategyPrism.SET_TO_DEFAULT;
Expand All @@ -74,37 +75,12 @@ public class PropertyMapping extends ModelElement {
private final List<String> dependsOn;
private final Assignment defaultValueAssignment;

public enum TargetWriteAccessorType {
FIELD,
GETTER,
SETTER,
ADDER;

public static TargetWriteAccessorType of(AccessorNamingUtils accessorNaming, Accessor accessor) {
if ( accessorNaming.isSetterMethod( accessor ) ) {
return TargetWriteAccessorType.SETTER;
}
else if ( accessorNaming.isAdderMethod( accessor ) ) {
return TargetWriteAccessorType.ADDER;
}
else if ( accessorNaming.isGetterMethod( accessor ) ) {
return TargetWriteAccessorType.GETTER;
}
else {
return TargetWriteAccessorType.FIELD;
}
}

public static boolean isFieldAssignment(TargetWriteAccessorType accessorType) {
return accessorType == FIELD;
}
}

@SuppressWarnings("unchecked")
private static class MappingBuilderBase<T extends MappingBuilderBase<T>> extends AbstractBaseBuilder<T> {

protected Accessor targetWriteAccessor;
protected TargetWriteAccessorType targetWriteAccessorType;
protected AccessorType targetWriteAccessorType;
protected Type targetType;
protected BuilderType targetBuilderType;
protected Accessor targetReadAccessor;
Expand All @@ -128,7 +104,7 @@ public T targetProperty(PropertyEntry targetProp) {
this.targetWriteAccessor = targetProp.getWriteAccessor();
this.targetType = targetProp.getType();
this.targetBuilderType = targetProp.getBuilderType();
this.targetWriteAccessorType = TargetWriteAccessorType.of( ctx.getAccessorNaming(), targetWriteAccessor );
this.targetWriteAccessorType = targetWriteAccessor.getAccessorType();
return (T) this;
}

Expand All @@ -139,7 +115,7 @@ public T targetReadAccessor(Accessor targetReadAccessor) {

public T targetWriteAccessor(Accessor targetWriteAccessor) {
this.targetWriteAccessor = targetWriteAccessor;
this.targetWriteAccessorType = TargetWriteAccessorType.of( ctx.getAccessorNaming(), targetWriteAccessor );
this.targetWriteAccessorType = targetWriteAccessor.getAccessorType();
this.targetType = determineTargetType();

return (T) this;
Expand All @@ -151,22 +127,7 @@ T mirror(AnnotationMirror mirror) {
}

private Type determineTargetType() {
// This is a bean mapping method, so we know the result is a declared type
Type mappingType = method.getResultType();
DeclaredType resultType = (DeclaredType) mappingType.getTypeMirror();

switch ( targetWriteAccessorType ) {
case ADDER:
case SETTER:
return ctx.getTypeFactory()
.getSingleParameter( resultType, targetWriteAccessor )
.getType();
case GETTER:
case FIELD:
default:
return ctx.getTypeFactory()
.getReturnType( resultType, targetWriteAccessor );
}
return ctx.getTypeFactory().getType( targetWriteAccessor.getAccessedType() );
}

public T targetPropertyName(String targetPropertyName) {
Expand All @@ -190,7 +151,7 @@ public T existingVariableNames(Set<String> existingVariableNames) {
}

protected boolean isFieldAssignment() {
return targetWriteAccessorType == TargetWriteAccessorType.FIELD;
return targetWriteAccessorType == AccessorType.FIELD;
}
}

Expand Down Expand Up @@ -300,11 +261,11 @@ public PropertyMapping build() {
ctx.getMessager().note( 2, Message.PROPERTYMAPPING_MAPPING_NOTE, rightHandSide, targetWriteAccessor );

rightHandSide.setUseElementAsSourceTypeForMatching(
targetWriteAccessorType == TargetWriteAccessorType.ADDER );
targetWriteAccessorType == AccessorType.ADDER );

// all the tricky cases will be excluded for the time being.
boolean preferUpdateMethods;
if ( targetWriteAccessorType == TargetWriteAccessorType.ADDER ) {
if ( targetWriteAccessorType == AccessorType.ADDER ) {
preferUpdateMethods = false;
}
else {
Expand Down Expand Up @@ -446,13 +407,12 @@ private Assignment getDefaultValueAssignment( Assignment rhs ) {
return null;
}

private Assignment assignToPlain(Type targetType, TargetWriteAccessorType targetAccessorType,
private Assignment assignToPlain(Type targetType, AccessorType targetAccessorType,
Assignment rightHandSide) {

Assignment result;

if ( targetAccessorType == TargetWriteAccessorType.SETTER ||
targetAccessorType == TargetWriteAccessorType.FIELD ) {
if ( targetAccessorType == AccessorType.SETTER || targetAccessorType == AccessorType.FIELD ) {
result = assignToPlainViaSetter( targetType, rightHandSide );
}
else {
Expand Down Expand Up @@ -530,7 +490,7 @@ else if ( result.getSourceType().isStreamType() ) {
return result;
}

private Assignment assignToCollection(Type targetType, TargetWriteAccessorType targetAccessorType,
private Assignment assignToCollection(Type targetType, AccessorType targetAccessorType,
Assignment rhs) {
return new CollectionAssignmentBuilder()
.mappingBuilderContext( ctx )
Expand Down Expand Up @@ -738,7 +698,7 @@ private Assignment forgeMapMapping(Type sourceType, Type targetType, SourceRHS s

private Assignment forgeMapping(SourceRHS sourceRHS) {
Type sourceType;
if ( targetWriteAccessorType == TargetWriteAccessorType.ADDER ) {
if ( targetWriteAccessorType == AccessorType.ADDER ) {
sourceType = sourceRHS.getSourceTypeForMatching();
}
else {
Expand All @@ -764,7 +724,7 @@ private Assignment forgeMapping(SourceRHS sourceRHS) {
// because we are forging a Mapping for a method with multiple source parameters.
// If the target type is enum, then we can't create an update method
if ( !targetType.isEnumType() && ( method.isUpdateMethod() || forceUpdateMethod )
&& targetWriteAccessorType != TargetWriteAccessorType.ADDER) {
&& targetWriteAccessorType != AccessorType.ADDER) {
parameters.add( Parameter.forForgedMappingTarget( targetType ) );
returnType = ctx.getTypeFactory().createVoidType();
}
Expand Down Expand Up @@ -899,8 +859,8 @@ public PropertyMapping build() {

if ( assignment != null ) {

if ( ctx.getAccessorNaming().isSetterMethod( targetWriteAccessor ) ||
Executables.isFieldAccessor( targetWriteAccessor ) ) {
if ( targetWriteAccessor.getAccessorType() == AccessorType.SETTER ||
targetWriteAccessor.getAccessorType() == AccessorType.FIELD ) {

// target accessor is setter, so decorate assignment as setter
if ( assignment.isCallingUpdateMethod() ) {
Expand Down Expand Up @@ -1015,8 +975,8 @@ public JavaExpressionMappingBuilder javaExpression(String javaExpression) {
public PropertyMapping build() {
Assignment assignment = new SourceRHS( javaExpression, null, existingVariableNames, "" );

if ( ctx.getAccessorNaming().isSetterMethod( targetWriteAccessor ) ||
Executables.isFieldAccessor( targetWriteAccessor ) ) {
if ( targetWriteAccessor.getAccessorType() == AccessorType.SETTER ||
targetWriteAccessor.getAccessorType() == AccessorType.FIELD ) {
// setter, so wrap in setter
assignment = new SetterWrapper( assignment, method.getThrownTypes(), isFieldAssignment() );
}
Expand Down
Loading

0 comments on commit 2215f17

Please sign in to comment.