Skip to content

Commit d62176b

Browse files
committed
[MNG-8214] Improve model velocity template to support subclasses
This makes the constructor protected and takes the Builder as argument
1 parent 1ee18d3 commit d62176b

File tree

1 file changed

+17
-45
lines changed

1 file changed

+17
-45
lines changed

src/mdo/model.vm

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -142,46 +142,33 @@ public class ${class.name}
142142
#end
143143

144144
/**
145-
* Constructor for this class, package protected.
145+
* Constructor for this class, to be called from {@link Builder} and its subclasses.
146146
* @see Builder#build()
147147
*/
148-
${class.name}(
149-
#if ( $class == $root )
150-
String namespaceUri,
151-
String modelEncoding,
152-
#end
153-
#foreach ( $field in $allFields )
154-
#set ( $sep = "#if(${locationTracking}||$field!=${allFields[${allFields.size()} - 1]}),#end" )
155-
#set ( $type = ${types.getOrDefault($field,${types.getOrDefault($field.type,$field.type)})} )
156-
#if ( $type.startsWith("List<") )
157-
#set ( $type = ${type.replace('List<','Collection<')} )
158-
#end
159-
$type $field.name${sep}
160-
#end
161-
#if ( $locationTracking )
162-
Map<Object, InputLocation> locations
163-
#end
164-
) {
148+
#set ( $additionalArguments = "#if(${locationTracking}), Map<Object, InputLocation> locations#end" )
149+
protected ${class.name}(Builder builder${additionalArguments}) {
165150
#if ( $class.superClass )
166151
super(
167-
#foreach ( $field in $inheritedFields )
168-
#set ( $sep = "#if(${locationTracking}||$field!=${inheritedFields[${inheritedFields.size()} - 1]}),#end" )
169-
${field.name}${sep}
170-
#end
152+
#set ( $sep = "#if(${locationTracking}),#end" )
153+
builder${sep}
171154
#if ( $locationTracking )
172155
locations
173156
#end
174157
);
175158
#end
176159
#if ( $class == $root )
177-
this.namespaceUri = namespaceUri;
178-
this.modelEncoding = modelEncoding;
160+
this.namespaceUri = builder.namespaceUri;
161+
this.modelEncoding = builder.modelEncoding;
179162
#end
180163
#foreach ( $field in $class.getFields($version) )
181164
#if ( $field.type == "java.util.List" || $field.type == "java.util.Properties" || $field.type == "java.util.Map" )
182-
this.${field.name} = ImmutableCollections.copy(${field.name});
165+
this.${field.name} = ImmutableCollections.copy(builder.${field.name} != null ? builder.${field.name} : (builder.base != null ? builder.base.${field.name} : null));
183166
#else
184-
this.${field.name} = ${field.name};
167+
#if ( $field.type == "boolean" || $field.type == "int" )
168+
this.${field.name} = builder.${field.name} != null ? builder.${field.name} : (builder.base != null ? builder.base.${field.name} : ${field.defaultValue});
169+
#else
170+
this.${field.name} = builder.${field.name} != null ? builder.${field.name} : (builder.base != null ? builder.base.${field.name} : null);
171+
#end
185172
#end
186173
#end
187174
#if ( $locationTracking )
@@ -384,7 +371,7 @@ public class ${class.name}
384371
Map<Object, InputLocation> locations;
385372
#end
386373

387-
Builder(boolean withDefaults) {
374+
protected Builder(boolean withDefaults) {
388375
#if ( $class.superClass )
389376
super(withDefaults);
390377
#end
@@ -402,7 +389,7 @@ public class ${class.name}
402389
}
403390
}
404391

405-
Builder(${class.name} base, boolean forceCopy) {
392+
protected Builder(${class.name} base, boolean forceCopy) {
406393
#if ( $class.superClass )
407394
super(base, forceCopy);
408395
#end
@@ -480,23 +467,8 @@ public class ${class.name}
480467
locations.put("${field.name}", newlocs.containsKey("${field.name}") ? newlocs.get("${field.name}") : oldlocs.get("${field.name}"));
481468
#end
482469
#end
483-
return new ${class.name}(
484-
#if ( $class == $root )
485-
namespaceUri != null ? namespaceUri : (base != null ? base.namespaceUri : ""),
486-
modelEncoding != null ? modelEncoding : (base != null ? base.modelEncoding : "UTF-8"),
487-
#end
488-
#foreach ( $field in $allFields )
489-
#set ( $sep = "#if(${locationTracking}||$field!=${allFields[${allFields.size()} - 1]}),#end" )
490-
#if ( $field.type == "boolean" || $field.type == "int" )
491-
${field.name} != null ? ${field.name} : (base != null ? base.${field.name} : ${field.defaultValue})${sep}
492-
#else
493-
${field.name} != null ? ${field.name} : (base != null ? base.${field.name} : null)${sep}
494-
#end
495-
#end
496-
#if ( $locationTracking )
497-
locations
498-
#end
499-
);
470+
#set ( $additionalArguments = "#if(${locationTracking}), locations#end" )
471+
return new ${class.name}(this${additionalArguments});
500472
}
501473
}
502474

0 commit comments

Comments
 (0)