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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import org.mapstruct.ap.internal.util.Message;
import org.mapstruct.ap.internal.util.Strings;
import org.mapstruct.ap.internal.util.accessor.Accessor;
import org.mapstruct.ap.internal.util.accessor.ExecutableElementAccessor;

/**
* A {@link MappingMethod} implemented by a {@link Mapper} class which maps one bean type to another, optionally
Expand Down Expand Up @@ -590,7 +589,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 @@ -12,7 +12,6 @@
import java.util.Set;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.type.DeclaredType;

import org.mapstruct.ap.internal.model.assignment.AdderWrapper;
import org.mapstruct.ap.internal.model.assignment.ArrayCopyWrapper;
Expand Down Expand Up @@ -40,14 +39,13 @@
import org.mapstruct.ap.internal.prism.NullValueCheckStrategyPrism;
import org.mapstruct.ap.internal.prism.NullValueMappingStrategyPrism;
import org.mapstruct.ap.internal.prism.NullValuePropertyMappingStrategyPrism;
import org.mapstruct.ap.internal.util.AccessorNamingUtils;
import org.mapstruct.ap.internal.util.Executables;
import org.mapstruct.ap.internal.util.MapperConfiguration;
import org.mapstruct.ap.internal.util.Message;
import org.mapstruct.ap.internal.util.NativeTypes;
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 @@ -73,37 +71,11 @@ 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 Accessor targetReadAccessor;
protected String targetPropertyName;
Expand All @@ -125,7 +97,7 @@ public T targetProperty(PropertyEntry targetProp) {
this.targetReadAccessor = targetProp.getReadAccessor();
this.targetWriteAccessor = targetProp.getWriteAccessor();
this.targetType = targetProp.getType();
this.targetWriteAccessorType = TargetWriteAccessorType.of( ctx.getAccessorNaming(), targetWriteAccessor );
this.targetWriteAccessorType = targetWriteAccessor.getAccessorType();
return (T) this;
}

Expand All @@ -136,7 +108,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 @@ -148,25 +120,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();
if ( !method.isUpdateMethod() ) {
mappingType = mappingType.getEffectiveType();
}
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 +144,7 @@ public T existingVariableNames(Set<String> existingVariableNames) {
}

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

Expand Down Expand Up @@ -300,11 +254,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 +400,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 +483,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 +691,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 +717,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 +852,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 +968,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