14
14
import io .beanmapper .core .converter .collections .CollectionMapConverter ;
15
15
import io .beanmapper .core .converter .collections .CollectionSetConverter ;
16
16
import io .beanmapper .core .converter .impl .*;
17
- import io .beanmapper .core .rule .BeanMapperRule ;
18
- import io .beanmapper .core .rule .ConstantMapperRule ;
17
+ import io .beanmapper .core .rule .MappableFields ;
19
18
import io .beanmapper .core .unproxy .BeanUnproxy ;
20
19
import io .beanmapper .core .unproxy .DefaultBeanUnproxy ;
21
20
import io .beanmapper .core .unproxy .SkippingBeanUnproxy ;
@@ -135,7 +134,7 @@ public <S, T> T map(S source, Class<T> targetClass, BeanInitializer beanInitiali
135
134
BeanMatch beanMatch = getBeanMatch (source .getClass (), targetClass );
136
135
T target = beanInitializer .instantiate (targetClass , getConstructorArguments (source , beanMatch ));
137
136
138
- return processFields (source , target , beanMatch , new ConstantMapperRule ( true ) );
137
+ return processFields (source , target , beanMatch );
139
138
}
140
139
141
140
/**
@@ -182,22 +181,21 @@ public <S, T> Collection<T> map(Collection<S> sourceItems, Class<T> targetClass,
182
181
*/
183
182
public <S , T > T map (S source , T target ) {
184
183
BeanMatch beanMatch = getBeanMatch (source .getClass (), target .getClass ());
185
- return processFields (source , target , beanMatch , new ConstantMapperRule ( true ) );
184
+ return processFields (source , target , beanMatch );
186
185
}
187
186
188
187
/**
189
188
* Copies the values from the source object to an existing target instance
190
189
* @param source source instance of the properties
191
190
* @param target target instance for the properties
192
- * @param rule the matching rule
193
191
* @param <S> the instance from which the properties get copied.
194
192
* @param <T> the instance to which the properties get copied
195
193
* @return the original target instance containing all applicable properties
196
194
* @throws BeanMappingException
197
195
*/
198
- public <S , T > T map (S source , T target , BeanMapperRule rule ) {
196
+ public <S , T > T map (S source , T target , MappableFields fieldsToMap ) {
199
197
BeanMatch beanMatch = getBeanMatch (source .getClass (), target .getClass ());
200
- return processFields (source , target , beanMatch , rule );
198
+ return processFields (source , target , fieldsToMap . compressBeanMatch ( beanMatch ) );
201
199
}
202
200
203
201
private <S > ConstructorArguments getConstructorArguments (S source , BeanMatch beanMatch ) {
@@ -249,7 +247,7 @@ private <T, S> BeanMatch getBeanMatch(Class<S> sourceClazz, Class<T> targetClazz
249
247
* @return A filled target object.
250
248
* @throws BeanMappingException
251
249
*/
252
- private <S , T > T processFields (S source , T target , BeanMatch beanMatch , BeanMapperRule rule ) {
250
+ private <S , T > T processFields (S source , T target , BeanMatch beanMatch ) {
253
251
for (String fieldName : beanMatch .getTargetNode ().keySet ()) {
254
252
BeanField sourceField = beanMatch .getSourceNode ().get (fieldName );
255
253
if (sourceField == null ) {
@@ -261,7 +259,7 @@ private <S, T> T processFields(S source, T target, BeanMatch beanMatch, BeanMapp
261
259
// No target field found -> check for alias
262
260
targetField = beanMatch .getAliases ().get (fieldName );
263
261
}
264
- processField (new BeanFieldMatch (source , target , sourceField , targetField , fieldName , beanMatch ), rule );
262
+ processField (new BeanFieldMatch (source , target , sourceField , targetField , fieldName , beanMatch ));
265
263
}
266
264
return target ;
267
265
}
@@ -271,17 +269,17 @@ private <S, T> T processFields(S source, T target, BeanMatch beanMatch, BeanMapp
271
269
* @param beanFieldMatch contains the fields belonging to the source/target field match
272
270
* @throws BeanMappingException
273
271
*/
274
- private void processField (BeanFieldMatch beanFieldMatch , BeanMapperRule rule ) {
272
+ private void processField (BeanFieldMatch beanFieldMatch ) {
275
273
if (!beanFieldMatch .hasMatchingSource ()) {
276
274
dealWithNonMatchingNode (beanFieldMatch );
277
275
return ;
278
276
}
279
277
if (!isConverterFor (beanFieldMatch .getSourceClass (), beanFieldMatch .getTargetClass ()) &&
280
- ! beanFieldMatch .hasSimilarClasses ( ) &&
281
- isMappableClass (beanFieldMatch .getTargetClass ()) &&
278
+ (! beanFieldMatch . hasSimilarClasses () || ( beanFieldMatch . hasSimilarClasses () && beanFieldMatch .getTargetObject () != null ) ) &&
279
+ isMappableClass (beanFieldMatch .getTargetClass ()) &&
282
280
beanFieldMatch .getSourceObject () != null ) {
283
281
284
- dealWithMappableNestedClass (beanFieldMatch , rule );
282
+ dealWithMappableNestedClass (beanFieldMatch );
285
283
return ;
286
284
}
287
285
if (beanFieldMatch .isMappable ()) {
@@ -326,17 +324,20 @@ private void copySourceToTarget(BeanFieldMatch beanFieldMatch) {
326
324
* If the field is a class which can itself be mapped to another class, it must be treated
327
325
* as such. The matching process is called recursively to deal with this pair.
328
326
* @param beanFieldMatch contains the fields belonging to the source/target field match
329
- * @param rule the matching rule
330
327
* @throws BeanMappingException
331
328
*/
332
- private void dealWithMappableNestedClass (BeanFieldMatch beanFieldMatch , BeanMapperRule rule ) {
329
+ private void dealWithMappableNestedClass (BeanFieldMatch beanFieldMatch ) {
333
330
Object encapsulatedSource = beanFieldMatch .getSourceObject ();
334
331
Object target ;
335
332
if (encapsulatedSource != null ) {
336
333
if (beanFieldMatch .getTargetObject () == null ){
337
334
target = map (encapsulatedSource , beanFieldMatch .getTargetClass ());
338
335
}else {
339
- target = map (encapsulatedSource , beanFieldMatch .getTarget (), rule );
336
+ if (beanFieldMatch .getBeanMatch ().getMappableFields () == null ) {
337
+ target = map (encapsulatedSource , beanFieldMatch .getTargetObject ());
338
+ } else {
339
+ target = map (encapsulatedSource , beanFieldMatch .getTargetObject (), beanFieldMatch .getBeanMatch ().getMappableFields ().splitForField (beanFieldMatch .getSourceFieldName ()));
340
+ }
340
341
}
341
342
beanFieldMatch .writeObject (target );
342
343
}
0 commit comments