2020
2121import java .util .Arrays ;
2222import java .util .Collection ;
23+ import java .util .Collections ;
2324import java .util .List ;
2425import java .util .Set ;
26+
2527import javax .lang .model .element .ExecutableElement ;
2628import javax .lang .model .type .TypeMirror ;
2729
3133import org .mapstruct .ap .model .assignment .GetterWrapperForCollectionsAndMaps ;
3234import org .mapstruct .ap .model .assignment .NewCollectionOrMapWrapper ;
3335import org .mapstruct .ap .model .assignment .NullCheckWrapper ;
34- import org .mapstruct .ap .model .assignment .SetterWrapperForCollectionsAndMaps ;
3536import org .mapstruct .ap .model .assignment .SetterWrapper ;
37+ import org .mapstruct .ap .model .assignment .SetterWrapperForCollectionsAndMaps ;
3638import org .mapstruct .ap .model .common .ModelElement ;
3739import org .mapstruct .ap .model .common .Parameter ;
3840import org .mapstruct .ap .model .common .Type ;
4143import org .mapstruct .ap .model .source .SourceReference ;
4244import org .mapstruct .ap .model .source .SourceReference .PropertyEntry ;
4345import org .mapstruct .ap .util .Executables ;
46+ import org .mapstruct .ap .util .Message ;
4447import org .mapstruct .ap .util .Strings ;
4548
4649import static org .mapstruct .ap .model .assignment .Assignment .AssignmentType .DIRECT ;
4750import static org .mapstruct .ap .model .assignment .Assignment .AssignmentType .TYPE_CONVERTED ;
4851import static org .mapstruct .ap .model .assignment .Assignment .AssignmentType .TYPE_CONVERTED_MAPPED ;
49- import org .mapstruct .ap .util .Message ;
5052
5153/**
5254 * Represents the mapping between a source and target property, e.g. from {@code String Source#foo} to
5759 */
5860public class PropertyMapping extends ModelElement {
5961
62+ private final String name ;
6063 private final String sourceBeanName ;
6164 private final String targetAccessorName ;
6265 private final Type targetType ;
6366 private final Assignment assignment ;
67+ private final List <String > dependsOn ;
6468
6569 public static class PropertyMappingBuilder {
6670
@@ -74,6 +78,7 @@ public static class PropertyMappingBuilder {
7478 private TypeMirror resultType ;
7579 private SourceReference sourceReference ;
7680 private Collection <String > existingVariableNames ;
81+ private List <String > dependsOn ;
7782
7883 public PropertyMappingBuilder mappingContext (MappingBuilderContext mappingContext ) {
7984 this .ctx = mappingContext ;
@@ -120,6 +125,11 @@ public PropertyMappingBuilder existingVariableNames(Collection<String> existingV
120125 return this ;
121126 }
122127
128+ public PropertyMappingBuilder dependsOn (List <String > dependsOn ) {
129+ this .dependsOn = dependsOn ;
130+ return this ;
131+ }
132+
123133 private enum TargetAccessorType {
124134
125135 GETTER ,
@@ -195,10 +205,12 @@ else if ( targetType.isArrayType() && sourceType.isArrayType() && assignment.get
195205 }
196206
197207 return new PropertyMapping (
208+ targetPropertyName ,
198209 sourceReference .getParameter ().getName (),
199210 targetAccessor .getSimpleName ().toString (),
200211 targetType ,
201- assignment
212+ assignment ,
213+ dependsOn
202214 );
203215 }
204216
@@ -465,10 +477,12 @@ public static class ConstantMappingBuilder {
465477 private SourceMethod method ;
466478 private String constantExpression ;
467479 private ExecutableElement targetAccessor ;
480+ private String targetPropertyName ;
468481 private String dateFormat ;
469482 private List <TypeMirror > qualifiers ;
470483 private TypeMirror resultType ;
471484 private Collection <String > existingVariableNames ;
485+ private List <String > dependsOn ;
472486
473487 public ConstantMappingBuilder mappingContext (MappingBuilderContext mappingContext ) {
474488 this .ctx = mappingContext ;
@@ -490,6 +504,11 @@ public ConstantMappingBuilder targetAccessor(ExecutableElement targetAccessor) {
490504 return this ;
491505 }
492506
507+ public ConstantMappingBuilder targetPropertyName (String targetPropertyName ) {
508+ this .targetPropertyName = targetPropertyName ;
509+ return this ;
510+ }
511+
493512 public ConstantMappingBuilder dateFormat (String dateFormat ) {
494513 this .dateFormat = dateFormat ;
495514 return this ;
@@ -510,6 +529,11 @@ public ConstantMappingBuilder existingVariableNames(Collection<String> existingV
510529 return this ;
511530 }
512531
532+ public ConstantMappingBuilder dependsOn (List <String > dependsOn ) {
533+ this .dependsOn = dependsOn ;
534+ return this ;
535+ }
536+
513537 public PropertyMapping build () {
514538
515539 // source
@@ -525,8 +549,6 @@ public PropertyMapping build() {
525549 targetType = ctx .getTypeFactory ().getReturnType ( targetAccessor );
526550 }
527551
528- String targetPropertyName = Executables .getPropertyName ( targetAccessor );
529-
530552 Assignment assignment = ctx .getMappingResolver ().getTargetAssignment (
531553 method ,
532554 mappedElement ,
@@ -565,7 +587,13 @@ public PropertyMapping build() {
565587 );
566588 }
567589
568- return new PropertyMapping ( targetAccessor .getSimpleName ().toString (), targetType , assignment );
590+ return new PropertyMapping (
591+ targetPropertyName ,
592+ targetAccessor .getSimpleName ().toString (),
593+ targetType ,
594+ assignment ,
595+ dependsOn
596+ );
569597 }
570598 }
571599
@@ -576,6 +604,8 @@ public static class JavaExpressionMappingBuilder {
576604 private String javaExpression ;
577605 private ExecutableElement targetAccessor ;
578606 private Collection <String > existingVariableNames ;
607+ private String targetPropertyName ;
608+ private List <String > dependsOn ;
579609
580610 public JavaExpressionMappingBuilder mappingContext (MappingBuilderContext mappingContext ) {
581611 this .ctx = mappingContext ;
@@ -602,6 +632,16 @@ public JavaExpressionMappingBuilder existingVariableNames(Collection<String> exi
602632 return this ;
603633 }
604634
635+ public JavaExpressionMappingBuilder targetPropertyName (String targetPropertyName ) {
636+ this .targetPropertyName = targetPropertyName ;
637+ return this ;
638+ }
639+
640+ public JavaExpressionMappingBuilder dependsOn (List <String > dependsOn ) {
641+ this .dependsOn = dependsOn ;
642+ return this ;
643+ }
644+
605645 public PropertyMapping build () {
606646
607647 Assignment assignment = AssignmentFactory .createDirect ( javaExpression );
@@ -623,24 +663,38 @@ public PropertyMapping build() {
623663 );
624664 }
625665
626- return new PropertyMapping ( targetAccessor .getSimpleName ().toString (), targetType , assignment );
666+ return new PropertyMapping (
667+ targetPropertyName ,
668+ targetAccessor .getSimpleName ().toString (),
669+ targetType ,
670+ assignment ,
671+ dependsOn
672+ );
627673 }
628674
629675 }
630676
631677 // Constructor for creating mappings of constant expressions.
632- private PropertyMapping (String targetAccessorName , Type targetType , Assignment propertyAssignment ) {
633- this ( null , targetAccessorName , targetType , propertyAssignment );
678+ private PropertyMapping (String name , String targetAccessorName , Type targetType , Assignment propertyAssignment ,
679+ List <String > dependsOn ) {
680+ this ( name , null , targetAccessorName , targetType , propertyAssignment , dependsOn );
634681 }
635682
636- private PropertyMapping (String sourceBeanName , String targetAccessorName , Type targetType , Assignment assignment ) {
637-
683+ private PropertyMapping (String name , String sourceBeanName , String targetAccessorName , Type targetType ,
684+ Assignment assignment , List <String > dependsOn ) {
685+ this .name = name ;
638686 this .sourceBeanName = sourceBeanName ;
639-
640687 this .targetAccessorName = targetAccessorName ;
641688 this .targetType = targetType ;
642-
643689 this .assignment = assignment ;
690+ this .dependsOn = dependsOn != null ? dependsOn : Collections .<String >emptyList ();
691+ }
692+
693+ /**
694+ * Returns the name of this mapping (property name on the target side)
695+ */
696+ public String getName () {
697+ return name ;
644698 }
645699
646700 public String getSourceBeanName () {
@@ -664,12 +718,17 @@ public Set<Type> getImportTypes() {
664718 return assignment .getImportTypes ();
665719 }
666720
721+ public List <String > getDependsOn () {
722+ return dependsOn ;
723+ }
724+
667725 @ Override
668726 public String toString () {
669727 return "PropertyMapping {"
670- + "\n targetName ='" + targetAccessorName + "\' ,"
728+ + "\n name ='" + name + "\' ,"
671729 + "\n targetType=" + targetType + ","
672- + "\n propertyAssignment=" + assignment
730+ + "\n propertyAssignment=" + assignment + ","
731+ + "\n dependsOn=" + dependsOn
673732 + "\n }" ;
674733 }
675734}
0 commit comments