/
MappingBuilderContext.java
273 lines (241 loc) · 10 KB
/
MappingBuilderContext.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.internal.model;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.TypeElement;
import org.mapstruct.ap.internal.model.common.Assignment;
import org.mapstruct.ap.internal.model.common.FormattingParameters;
import org.mapstruct.ap.internal.model.common.SourceRHS;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.common.TypeFactory;
import org.mapstruct.ap.internal.model.source.Method;
import org.mapstruct.ap.internal.model.source.SourceMethod;
import org.mapstruct.ap.internal.model.source.selector.SelectionCriteria;
import org.mapstruct.ap.internal.option.Options;
import org.mapstruct.ap.internal.util.AccessorNamingUtils;
import org.mapstruct.ap.internal.util.ElementUtils;
import org.mapstruct.ap.internal.util.FormattingMessager;
import org.mapstruct.ap.internal.util.Services;
import org.mapstruct.ap.internal.util.TypeUtils;
import org.mapstruct.ap.spi.EnumMappingStrategy;
import org.mapstruct.ap.spi.EnumTransformationStrategy;
import org.mapstruct.ap.spi.MappingExclusionProvider;
/**
* This class provides the context for the builders.
* <p>
* The context provides:
* <ul>
* <li>Input for the building process, such as the source model (mapping methods found) and mapper references.</li>
* <li>Required factory, utility, reporting methods for building the mappings.</li>
* <li>Means to harbor results produced by the builders, such as forged- and supported mapping methods that should be
* generated in a later stage.</li>
* </ul>
*
* @author Sjaak Derksen
*/
public class MappingBuilderContext {
private static final MappingExclusionProvider SUB_MAPPING_EXCLUSION_PROVIDER = Services.get(
MappingExclusionProvider.class,
new DefaultMappingExclusionProvider()
);
/**
* Resolves the most suitable way for mapping an element (property, iterable element etc.) from source to target.
* There are 2 basic types of mappings:
* <ul>
* <li>conversions</li>
* <li>methods</li>
* </ul>
* conversions are essentially one line mappings, such as String to Integer and Integer to Long methods come in some
* varieties:
* <ul>
* <li>referenced mapping methods, these are methods implemented (or referenced) by the user. Sometimes indicated
* with the 'uses' in the mapping annotations or part of the abstract mapper class</li>
* <li>generated mapping methods (by means of MapStruct)</li>
* <li>built in methods</li>
* </ul>
*
* @author Sjaak Derksen
*/
public interface MappingResolver {
/**
* returns a parameter assignment
*
* @param mappingMethod target mapping method
* @param description the description source
* @param targetType return type to match
* @param formattingParameters used for formatting dates and numbers
* @param criteria parameters criteria in the selection process
* @param sourceRHS source information
* @param positionHint the mirror for reporting problems
* @param forger the supplier of the callback method to forge a method
*
* @return an assignment to a method parameter, which can either be:
* <ol>
* <li>MethodReference</li>
* <li>TypeConversion</li>
* <li>SourceRHS Assignment (empty TargetAssignment)</li>
* <li>null, no assignment found</li>
* </ol>
*/
Assignment getTargetAssignment(Method mappingMethod, ForgedMethodHistory description, Type targetType,
FormattingParameters formattingParameters,
SelectionCriteria criteria, SourceRHS sourceRHS,
AnnotationMirror positionHint,
Supplier<Assignment> forger);
Set<SupportingMappingMethod> getUsedSupportedMappings();
Set<Field> getUsedSupportedFields();
}
private final TypeFactory typeFactory;
private final ElementUtils elementUtils;
private final TypeUtils typeUtils;
private final FormattingMessager messager;
private final AccessorNamingUtils accessorNaming;
private final EnumMappingStrategy enumMappingStrategy;
private final Map<String, EnumTransformationStrategy> enumTransformationStrategies;
private final Options options;
private final TypeElement mapperTypeElement;
private final List<SourceMethod> sourceModel;
private final List<MapperReference> mapperReferences;
private final MappingResolver mappingResolver;
private final List<MappingMethod> mappingsToGenerate = new ArrayList<>();
private final Map<ForgedMethod, ForgedMethod> forgedMethodsUnderCreation =
new HashMap<>();
//CHECKSTYLE:OFF
public MappingBuilderContext(TypeFactory typeFactory,
ElementUtils elementUtils,
TypeUtils typeUtils,
FormattingMessager messager,
AccessorNamingUtils accessorNaming,
EnumMappingStrategy enumMappingStrategy,
Map<String, EnumTransformationStrategy> enumTransformationStrategies,
Options options,
MappingResolver mappingResolver,
TypeElement mapper,
List<SourceMethod> sourceModel,
List<MapperReference> mapperReferences) {
this.typeFactory = typeFactory;
this.elementUtils = elementUtils;
this.typeUtils = typeUtils;
this.messager = messager;
this.accessorNaming = accessorNaming;
this.enumMappingStrategy = enumMappingStrategy;
this.enumTransformationStrategies = enumTransformationStrategies;
this.options = options;
this.mappingResolver = mappingResolver;
this.mapperTypeElement = mapper;
this.sourceModel = sourceModel;
this.mapperReferences = mapperReferences;
}
//CHECKSTYLE:ON
/**
* Returns a map which is used to track which forged methods are under creation.
* Used for cutting the possible infinite recursion of forged method creation.
*
* Map is used instead of set because not all fields of ForgedMethods are used in equals/hashCode and we are
* interested only in the first created ForgedMethod
*
* @return map of forged methods
*/
public Map<ForgedMethod, ForgedMethod> getForgedMethodsUnderCreation() {
return forgedMethodsUnderCreation;
}
public TypeElement getMapperTypeElement() {
return mapperTypeElement;
}
public List<SourceMethod> getSourceModel() {
return sourceModel;
}
public List<MapperReference> getMapperReferences() {
return mapperReferences;
}
public TypeFactory getTypeFactory() {
return typeFactory;
}
public ElementUtils getElementUtils() {
return elementUtils;
}
public TypeUtils getTypeUtils() {
return typeUtils;
}
public FormattingMessager getMessager() {
return messager;
}
public AccessorNamingUtils getAccessorNaming() {
return accessorNaming;
}
public EnumMappingStrategy getEnumMappingStrategy() {
return enumMappingStrategy;
}
public Map<String, EnumTransformationStrategy> getEnumTransformationStrategies() {
return enumTransformationStrategies;
}
public Options getOptions() {
return options;
}
public MappingResolver getMappingResolver() {
return mappingResolver;
}
public List<MappingMethod> getMappingsToGenerate() {
return mappingsToGenerate;
}
public List<String> getReservedNames() {
Set<String> nameSet = new HashSet<>();
for ( MappingMethod method : mappingsToGenerate ) {
nameSet.add( method.getName() );
}
// add existing names
for ( SourceMethod method : sourceModel) {
if ( method.isAbstract() ) {
nameSet.add( method.getName() );
}
}
return new ArrayList<>( nameSet );
}
public MappingMethod getExistingMappingMethod(MappingMethod newMappingMethod) {
MappingMethod existingMappingMethod = null;
for ( MappingMethod mappingMethod : mappingsToGenerate ) {
if ( newMappingMethod.equals( mappingMethod ) ) {
existingMappingMethod = mappingMethod;
break;
}
}
return existingMappingMethod;
}
public Set<SupportingMappingMethod> getUsedSupportedMappings() {
return mappingResolver.getUsedSupportedMappings();
}
public Set<Field> getUsedSupportedFields() {
return mappingResolver.getUsedSupportedFields();
}
/**
* @param sourceType from which an automatic sub-mapping needs to be generated
* @param targetType to which an automatic sub-mapping needs to be generated
*
* @return {@code true} if MapStruct is allowed to try and generate an automatic sub-mapping between the
* source and target {@link Type}
*/
public boolean canGenerateAutoSubMappingBetween(Type sourceType, Type targetType) {
return canGenerateAutoSubMappingFor( sourceType ) && canGenerateAutoSubMappingFor( targetType );
}
/**
* @param type that MapStruct wants to use to genrate an autoamtic sub-mapping for/from
*
* @return {@code true} if the type is not excluded from the {@link MappingExclusionProvider}
*/
private boolean canGenerateAutoSubMappingFor(Type type) {
return type.getTypeElement() != null && !SUB_MAPPING_EXCLUSION_PROVIDER.isExcluded( type.getTypeElement() );
}
public boolean isErroneous() {
return messager.isErroneous();
}
}