Skip to content

Commit 81a4cb3

Browse files
author
sjaakd
committed
mapstruct#981 Refactoring, move SourceRHS init to start of property build.
1 parent 0ec9729 commit 81a4cb3

22 files changed

+187
-178
lines changed

processor/src/main/java/org/mapstruct/ap/internal/model/IterableMappingMethod.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,11 @@ public IterableMappingMethod build() {
105105

106106
Assignment assignment = ctx.getMappingResolver().getTargetAssignment(
107107
method,
108-
"collection element",
109108
targetElementType,
110109
null, // there is no targetPropertyName
111110
formattingParameters,
112111
selectionParameters,
113-
new SourceRHS( loopVariableName, sourceElementType, new HashSet<String>() ),
112+
new SourceRHS( loopVariableName, sourceElementType, new HashSet<String>(), "collection element" ),
114113
false
115114
);
116115

processor/src/main/java/org/mapstruct/ap/internal/model/MapMappingMethod.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,11 @@ public MapMappingMethod build() {
108108

109109
Assignment keyAssignment = ctx.getMappingResolver().getTargetAssignment(
110110
method,
111-
"map key",
112111
keyTargetType,
113112
null, // there is no targetPropertyName
114113
keyFormattingParameters,
115114
keySelectionParameters,
116-
new SourceRHS( "entry.getKey()", keySourceType, new HashSet<String>() ),
115+
new SourceRHS( "entry.getKey()", keySourceType, new HashSet<String>(), "map key" ),
117116
false
118117
);
119118

@@ -134,12 +133,11 @@ public MapMappingMethod build() {
134133

135134
Assignment valueAssignment = ctx.getMappingResolver().getTargetAssignment(
136135
method,
137-
"map value",
138136
valueTargetType,
139137
null, // there is no targetPropertyName
140138
valueFormattingParameters,
141139
valueSelectionParameters,
142-
new SourceRHS( "entry.getValue()", valueSourceType, new HashSet<String>() ),
140+
new SourceRHS( "entry.getValue()", valueSourceType, new HashSet<String>(), "map value" ),
143141
false
144142
);
145143

processor/src/main/java/org/mapstruct/ap/internal/model/MappingBuilderContext.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ public interface MappingResolver {
7575
* returns a parameter assignment
7676
*
7777
* @param mappingMethod target mapping method
78-
* @param mappedElement used for error messages
7978
* @param targetType return type to match
8079
* @param targetPropertyName name of the target property
8180
* @param formattingParameters used for formatting dates and numbers
@@ -91,9 +90,8 @@ public interface MappingResolver {
9190
* <li>null, no assignment found</li>
9291
* </ol>
9392
*/
94-
@SuppressWarnings("checkstyle:parameternumber")
95-
Assignment getTargetAssignment(Method mappingMethod, String mappedElement, Type targetType,
96-
String targetPropertyName, FormattingParameters formattingParameters,
93+
Assignment getTargetAssignment(Method mappingMethod, Type targetType, String targetPropertyName,
94+
FormattingParameters formattingParameters,
9795
SelectionParameters selectionParameters, SourceRHS sourceRHS,
9896
boolean preferUpdateMethods);
9997

processor/src/main/java/org/mapstruct/ap/internal/model/MethodReference.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ public String getSourceReference() {
127127
return assignment.getSourceReference();
128128
}
129129

130+
@Override
131+
public String getSourcePresenceCheckerReference() {
132+
return assignment.getSourcePresenceCheckerReference();
133+
}
134+
130135
@Override
131136
public Type getSourceType() {
132137
return assignment.getSourceType();

processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java

Lines changed: 68 additions & 92 deletions
Large diffs are not rendered by default.

processor/src/main/java/org/mapstruct/ap/internal/model/SourceRHS.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
/**
3131
* SourceRHS Assignment. Right Hand Side (RHS), source part of the assignment.
3232
*
33+
* This class contains all information on the source side of an assignment needed for common use in the mapping.
34+
*
3335
* @author Sjaak Derksen
3436
*/
3537
public class SourceRHS extends ModelElement implements Assignment {
@@ -38,18 +40,36 @@ public class SourceRHS extends ModelElement implements Assignment {
3840
private final Type sourceType;
3941
private String sourceLocalVarName;
4042
private final Set<String> existingVariableNames;
43+
private final String sourceErrorMessagePart;
44+
private final String sourcePresenceCheckerReference;
45+
private boolean useElementAsSourceTypeForMatching = false;
46+
private final String sourceParameterName;
47+
48+
public SourceRHS(String sourceReference, Type sourceType, Set<String> existingVariableNames,
49+
String sourceErrorMessagePart ) {
50+
this( null, sourceReference, null, sourceType, existingVariableNames, sourceErrorMessagePart );
51+
}
4152

42-
public SourceRHS(String sourceReference, Type sourceType, Set<String> existingVariableNames ) {
53+
public SourceRHS(String sourceParameterName, String sourceReference, String sourcePresenceCheckerReference,
54+
Type sourceType, Set<String> existingVariableNames, String sourceErrorMessagePart ) {
4355
this.sourceReference = sourceReference;
4456
this.sourceType = sourceType;
4557
this.existingVariableNames = existingVariableNames;
58+
this.sourceErrorMessagePart = sourceErrorMessagePart;
59+
this.sourcePresenceCheckerReference = sourcePresenceCheckerReference;
60+
this.sourceParameterName = sourceParameterName;
4661
}
4762

4863
@Override
4964
public String getSourceReference() {
5065
return sourceReference;
5166
}
5267

68+
@Override
69+
public String getSourcePresenceCheckerReference() {
70+
return sourcePresenceCheckerReference;
71+
}
72+
5373
@Override
5474
public Type getSourceType() {
5575
return sourceType;
@@ -99,4 +119,32 @@ public boolean isUpdateMethod() {
99119
public String toString() {
100120
return sourceReference;
101121
}
122+
123+
public String getSourceErrorMessagePart() {
124+
return sourceErrorMessagePart;
125+
}
126+
127+
/**
128+
* The source type that is to be used when resolving the mapping from source to target.
129+
*
130+
* @return the source type to be used in the matching process.
131+
*/
132+
public Type getSourceTypeForMatching() {
133+
return useElementAsSourceTypeForMatching && sourceType.isCollectionType() ?
134+
sourceType.getTypeParameters().get( 0 ) : sourceType;
135+
}
136+
137+
/**
138+
* For collection type, use element as source type to find a suitable mapping method.
139+
*
140+
* @param useElementAsSourceTypeForMatching
141+
*/
142+
public void setUseElementAsSourceTypeForMatching(boolean useElementAsSourceTypeForMatching) {
143+
this.useElementAsSourceTypeForMatching = useElementAsSourceTypeForMatching;
144+
}
145+
146+
public String getSourceParameterName() {
147+
return sourceParameterName;
148+
}
149+
102150
}

processor/src/main/java/org/mapstruct/ap/internal/model/TypeConversion.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ public String getSourceReference() {
8686
return assignment.getSourceReference();
8787
}
8888

89+
@Override
90+
public String getSourcePresenceCheckerReference() {
91+
return assignment.getSourcePresenceCheckerReference();
92+
}
93+
8994
@Override
9095
public Type getSourceType() {
9196
return assignment.getSourceType();

processor/src/main/java/org/mapstruct/ap/internal/model/assignment/AdderWrapper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public class AdderWrapper extends AssignmentWrapper {
3838
public AdderWrapper( Assignment decoratedAssignment, List<Type> thrownTypesToExclude, boolean fieldAssignment ) {
3939
super( decoratedAssignment, fieldAssignment );
4040
this.thrownTypesToExclude = thrownTypesToExclude;
41-
this.sourceIteratorName =
42-
decoratedAssignment.createLocalVarName( decoratedAssignment.getSourceType().getName() );
41+
String desiredName = decoratedAssignment.getSourceType().getTypeParameters().get( 0 ).getName();
42+
this.sourceIteratorName = decoratedAssignment.createLocalVarName( desiredName );
4343
decoratedAssignment.setSourceLocalVarName( sourceIteratorName );
4444
}
4545

@@ -61,7 +61,7 @@ public List<Type> getThrownTypes() {
6161
public Set<Type> getImportTypes() {
6262
Set<Type> imported = new HashSet<Type>();
6363
imported.addAll( super.getImportTypes() );
64-
imported.add( getSourceType() );
64+
imported.add( getSourceType().getTypeParameters().get( 0 ) );
6565
return imported;
6666
}
6767

processor/src/main/java/org/mapstruct/ap/internal/model/assignment/Assignment.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ enum AssignmentType {
7777
*/
7878
String getSourceReference();
7979

80+
/**
81+
* the source presence checker reference
82+
*
83+
* @return source reference
84+
*/
85+
String getSourcePresenceCheckerReference();
86+
8087
/**
8188
* the source type used in the matching process
8289
*

processor/src/main/java/org/mapstruct/ap/internal/model/assignment/AssignmentWrapper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public String getSourceReference() {
6363
return decoratedAssignment.getSourceReference();
6464
}
6565

66+
@Override
67+
public String getSourcePresenceCheckerReference() {
68+
return decoratedAssignment.getSourcePresenceCheckerReference();
69+
}
70+
6671
@Override
6772
public Type getSourceType() {
6873
return decoratedAssignment.getSourceType();

0 commit comments

Comments
 (0)