66package org .mapstruct .ap .internal .model .source ;
77
88import java .util .List ;
9+ import java .util .Objects ;
910import java .util .Optional ;
11+ import javax .lang .model .element .AnnotationMirror ;
1012import javax .lang .model .element .ExecutableElement ;
1113import javax .lang .model .type .TypeKind ;
1214import javax .lang .model .util .Types ;
1315
16+ import org .mapstruct .ap .internal .model .common .TypeFactory ;
1417import org .mapstruct .ap .internal .prism .BeanMappingPrism ;
1518import org .mapstruct .ap .internal .prism .BuilderPrism ;
1619import org .mapstruct .ap .internal .prism .NullValueCheckStrategyPrism ;
@@ -36,87 +39,134 @@ public class BeanMapping {
3639 private final BuilderPrism builder ;
3740 private final NullValuePropertyMappingStrategyPrism nullValuePropertyMappingStrategy ;
3841
42+ private final AnnotationMirror mirror ;
43+
3944 /**
40- * creates a mapping for inheritance. Will set ignoreByDefault to false.
45+ * creates a mapping for inheritance. Will set
46+ * - ignoreByDefault to false.
47+ * - resultType to null
4148 *
42- * @param map
43- * @return
49+ * @return new mapping
4450 */
45- public static BeanMapping forInheritance ( BeanMapping map ) {
51+ public static BeanMapping forInheritance (BeanMapping beanMapping ) {
4652 return new BeanMapping (
47- map . selectionParameters ,
48- map .nullValueMappingStrategy ,
49- map .nullValuePropertyMappingStrategy ,
50- map .nullValueCheckStrategy ,
51- map .reportingPolicy ,
53+ SelectionParameters . forInheritance ( beanMapping . selectionParameters ) ,
54+ beanMapping .nullValueMappingStrategy ,
55+ beanMapping .nullValuePropertyMappingStrategy ,
56+ beanMapping .nullValueCheckStrategy ,
57+ beanMapping .reportingPolicy ,
5258 false ,
53- map .ignoreUnmappedSourceProperties ,
54- map .builder
59+ beanMapping .ignoreUnmappedSourceProperties ,
60+ beanMapping .builder ,
61+ beanMapping .mirror
5562 );
5663 }
5764
58- public static BeanMapping fromPrism (BeanMappingPrism beanMapping , ExecutableElement method ,
59- FormattingMessager messager , Types typeUtils ) {
60-
61- if ( beanMapping == null ) {
62- return null ;
63- }
64-
65- boolean resultTypeIsDefined = !TypeKind .VOID .equals ( beanMapping .resultType ().getKind () );
65+ public static class Builder {
6666
67- NullValueMappingStrategyPrism nullValueMappingStrategy =
68- null == beanMapping .values .nullValueMappingStrategy ()
69- ? null
70- : NullValueMappingStrategyPrism .valueOf ( beanMapping .nullValueMappingStrategy () );
67+ private BeanMappingPrism prism ;
68+ private ExecutableElement method ;
69+ private FormattingMessager messager ;
70+ private Types typeUtils ;
71+ private TypeFactory typeFactory ;
7172
72- NullValuePropertyMappingStrategyPrism nullValuePropertyMappingStrategy =
73- null == beanMapping .values .nullValuePropertyMappingStrategy ()
74- ? null
75- : NullValuePropertyMappingStrategyPrism .valueOf ( beanMapping .nullValuePropertyMappingStrategy () );
76-
77- NullValueCheckStrategyPrism nullValueCheckStrategy =
78- null == beanMapping .values .nullValueCheckStrategy ()
79- ? null
80- : NullValueCheckStrategyPrism .valueOf ( beanMapping .nullValueCheckStrategy () );
73+ public Builder beanMappingPrism (BeanMappingPrism beanMappingPrism ) {
74+ this .prism = beanMappingPrism ;
75+ return this ;
76+ }
8177
82- boolean ignoreByDefault = beanMapping .ignoreByDefault ();
83- BuilderPrism builderMapping = null ;
84- if ( beanMapping .values .builder () != null ) {
85- builderMapping = beanMapping .builder ();
78+ public Builder method (ExecutableElement method ) {
79+ this .method = method ;
80+ return this ;
8681 }
8782
88- if ( ! resultTypeIsDefined && beanMapping . qualifiedBy (). isEmpty () && beanMapping . qualifiedByName (). isEmpty ()
89- && beanMapping . ignoreUnmappedSourceProperties (). isEmpty ()
90- && ( nullValueMappingStrategy == null ) && ( nullValuePropertyMappingStrategy == null )
91- && ( nullValueCheckStrategy == null ) && ! ignoreByDefault && builderMapping == null ) {
83+ public Builder messager ( FormattingMessager messager ) {
84+ this . messager = messager ;
85+ return this ;
86+ }
9287
93- messager .printMessage ( method , Message .BEANMAPPING_NO_ELEMENTS );
88+ public Builder typeUtils (Types typeUtils ) {
89+ this .typeUtils = typeUtils ;
90+ return this ;
9491 }
9592
96- SelectionParameters cmp = new SelectionParameters (
97- beanMapping .qualifiedBy (),
98- beanMapping .qualifiedByName (),
99- resultTypeIsDefined ? beanMapping .resultType () : null ,
100- typeUtils
101- );
93+ public Builder typeFactory (TypeFactory typeFactory ) {
94+ this .typeFactory = typeFactory ;
95+ return this ;
96+ }
10297
103- //TODO Do we want to add the reporting policy to the BeanMapping as well? To give more granular support?
104- return new BeanMapping (
105- cmp ,
106- nullValueMappingStrategy ,
107- nullValuePropertyMappingStrategy ,
108- nullValueCheckStrategy ,
109- null ,
110- ignoreByDefault ,
111- beanMapping .ignoreUnmappedSourceProperties (),
112- builderMapping
113- );
98+ public BeanMapping build () {
99+
100+ if ( prism == null ) {
101+ return null ;
102+ }
103+
104+ Objects .requireNonNull ( method );
105+ Objects .requireNonNull ( messager );
106+ Objects .requireNonNull ( method );
107+ Objects .requireNonNull ( typeUtils );
108+ Objects .requireNonNull ( typeFactory );
109+
110+ boolean resultTypeIsDefined = !TypeKind .VOID .equals ( prism .resultType ().getKind () );
111+
112+ NullValueMappingStrategyPrism nullValueMappingStrategy =
113+ null == prism .values .nullValueMappingStrategy ()
114+ ? null
115+ : NullValueMappingStrategyPrism .valueOf ( prism .nullValueMappingStrategy () );
116+
117+ NullValuePropertyMappingStrategyPrism nullValuePropertyMappingStrategy =
118+ null == prism .values .nullValuePropertyMappingStrategy ()
119+ ? null
120+ :
121+ NullValuePropertyMappingStrategyPrism .valueOf ( prism .nullValuePropertyMappingStrategy () );
122+
123+ NullValueCheckStrategyPrism nullValueCheckStrategy =
124+ null == prism .values .nullValueCheckStrategy ()
125+ ? null
126+ : NullValueCheckStrategyPrism .valueOf ( prism .nullValueCheckStrategy () );
127+
128+ boolean ignoreByDefault = prism .ignoreByDefault ();
129+ BuilderPrism builderMapping = null ;
130+ if ( prism .values .builder () != null ) {
131+ builderMapping = prism .builder ();
132+ }
133+
134+ if ( !resultTypeIsDefined && prism .qualifiedBy ().isEmpty () &&
135+ prism .qualifiedByName ().isEmpty ()
136+ && prism .ignoreUnmappedSourceProperties ().isEmpty ()
137+ && ( nullValueMappingStrategy == null ) && ( nullValuePropertyMappingStrategy == null )
138+ && ( nullValueCheckStrategy == null ) && !ignoreByDefault && builderMapping == null ) {
139+
140+ messager .printMessage ( method , Message .BEANMAPPING_NO_ELEMENTS );
141+ }
142+
143+ SelectionParameters cmp = new SelectionParameters (
144+ prism .qualifiedBy (),
145+ prism .qualifiedByName (),
146+ resultTypeIsDefined ? prism .resultType () : null ,
147+ typeUtils
148+ );
149+
150+ //TODO Do we want to add the reporting policy to the BeanMapping as well? To give more granular support?
151+ return new BeanMapping (
152+ cmp ,
153+ nullValueMappingStrategy ,
154+ nullValuePropertyMappingStrategy ,
155+ nullValueCheckStrategy ,
156+ null ,
157+ ignoreByDefault ,
158+ prism .ignoreUnmappedSourceProperties (),
159+ builderMapping ,
160+ prism .mirror
161+ );
162+ }
114163 }
115164
116165 private BeanMapping (SelectionParameters selectionParameters , NullValueMappingStrategyPrism nvms ,
117166 NullValuePropertyMappingStrategyPrism nvpms , NullValueCheckStrategyPrism nvcs ,
118167 ReportingPolicyPrism reportingPolicy , boolean ignoreByDefault ,
119- List <String > ignoreUnmappedSourceProperties , BuilderPrism builder ) {
168+ List <String > ignoreUnmappedSourceProperties , BuilderPrism builder ,
169+ AnnotationMirror mirror ) {
120170 this .selectionParameters = selectionParameters ;
121171 this .nullValueMappingStrategy = nvms ;
122172 this .nullValuePropertyMappingStrategy = nvpms ;
@@ -125,6 +175,7 @@ private BeanMapping(SelectionParameters selectionParameters, NullValueMappingStr
125175 this .ignoreByDefault = ignoreByDefault ;
126176 this .ignoreUnmappedSourceProperties = ignoreUnmappedSourceProperties ;
127177 this .builder = builder ;
178+ this .mirror = mirror ;
128179 }
129180
130181 public SelectionParameters getSelectionParameters () {
@@ -155,6 +206,10 @@ public List<String> getIgnoreUnmappedSourceProperties() {
155206 return ignoreUnmappedSourceProperties ;
156207 }
157208
209+ public AnnotationMirror getMirror () {
210+ return mirror ;
211+ }
212+
158213 /**
159214 * derives the builder prism given the options and configuration
160215 * @param method containing mandatory configuration and the mapping options (optionally containing a beanmapping)
0 commit comments