From 93079eadd50514cdef7d71a4b36a2716a2a73bf3 Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Fri, 16 Aug 2024 18:15:11 +0200 Subject: [PATCH] [MNG-8214] Improve model velocity template to support subclasses This makes the constructor protected and takes the Builder as argument --- src/mdo/model.vm | 62 +++++++++++++----------------------------------- 1 file changed, 17 insertions(+), 45 deletions(-) diff --git a/src/mdo/model.vm b/src/mdo/model.vm index be741434ac5e..304681e7d11e 100644 --- a/src/mdo/model.vm +++ b/src/mdo/model.vm @@ -142,46 +142,33 @@ public class ${class.name} #end /** - * Constructor for this class, package protected. + * Constructor for this class, to be called from {@link Builder} and its subclasses. * @see Builder#build() */ - ${class.name}( - #if ( $class == $root ) - String namespaceUri, - String modelEncoding, - #end - #foreach ( $field in $allFields ) - #set ( $sep = "#if(${locationTracking}||$field!=${allFields[${allFields.size()} - 1]}),#end" ) - #set ( $type = ${types.getOrDefault($field,${types.getOrDefault($field.type,$field.type)})} ) - #if ( $type.startsWith("List<") ) - #set ( $type = ${type.replace('List<','Collection<')} ) - #end - $type $field.name${sep} - #end - #if ( $locationTracking ) - Map locations - #end - ) { + #set ( $additionalArguments = "#if(${locationTracking}), Map locations#end" ) + protected ${class.name}(Builder builder${additionalArguments}) { #if ( $class.superClass ) super( - #foreach ( $field in $inheritedFields ) - #set ( $sep = "#if(${locationTracking}||$field!=${inheritedFields[${inheritedFields.size()} - 1]}),#end" ) - ${field.name}${sep} - #end + #set ( $sep = "#if(${locationTracking}),#end" ) + builder${sep} #if ( $locationTracking ) locations #end ); #end #if ( $class == $root ) - this.namespaceUri = namespaceUri; - this.modelEncoding = modelEncoding; + this.namespaceUri = builder.namespaceUri != null ? builder.namespaceUri : (builder.base != null ? builder.base.namespaceUri : null); + this.modelEncoding = builder.modelEncoding != null ? builder.modelEncoding : (builder.base != null ? builder.base.modelEncoding : null); #end #foreach ( $field in $class.getFields($version) ) #if ( $field.type == "java.util.List" || $field.type == "java.util.Properties" || $field.type == "java.util.Map" ) - this.${field.name} = ImmutableCollections.copy(${field.name}); + this.${field.name} = ImmutableCollections.copy(builder.${field.name} != null ? builder.${field.name} : (builder.base != null ? builder.base.${field.name} : null)); #else - this.${field.name} = ${field.name}; + #if ( $field.type == "boolean" || $field.type == "int" ) + this.${field.name} = builder.${field.name} != null ? builder.${field.name} : (builder.base != null ? builder.base.${field.name} : ${field.defaultValue}); + #else + this.${field.name} = builder.${field.name} != null ? builder.${field.name} : (builder.base != null ? builder.base.${field.name} : null); + #end #end #end #if ( $locationTracking ) @@ -384,7 +371,7 @@ public class ${class.name} Map locations; #end - Builder(boolean withDefaults) { + protected Builder(boolean withDefaults) { #if ( $class.superClass ) super(withDefaults); #end @@ -402,7 +389,7 @@ public class ${class.name} } } - Builder(${class.name} base, boolean forceCopy) { + protected Builder(${class.name} base, boolean forceCopy) { #if ( $class.superClass ) super(base, forceCopy); #end @@ -480,23 +467,8 @@ public class ${class.name} locations.put("${field.name}", newlocs.containsKey("${field.name}") ? newlocs.get("${field.name}") : oldlocs.get("${field.name}")); #end #end - return new ${class.name}( - #if ( $class == $root ) - namespaceUri != null ? namespaceUri : (base != null ? base.namespaceUri : ""), - modelEncoding != null ? modelEncoding : (base != null ? base.modelEncoding : "UTF-8"), - #end - #foreach ( $field in $allFields ) - #set ( $sep = "#if(${locationTracking}||$field!=${allFields[${allFields.size()} - 1]}),#end" ) - #if ( $field.type == "boolean" || $field.type == "int" ) - ${field.name} != null ? ${field.name} : (base != null ? base.${field.name} : ${field.defaultValue})${sep} - #else - ${field.name} != null ? ${field.name} : (base != null ? base.${field.name} : null)${sep} - #end - #end - #if ( $locationTracking ) - locations - #end - ); + #set ( $additionalArguments = "#if(${locationTracking}), locations#end" ) + return new ${class.name}(this${additionalArguments}); } }