You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by kw...@apache.org on 2024/08/16 16:15:25 UTC

(maven) 01/01: [MNG-8214] Improve model velocity template to support subclasses

This is an automated email from the ASF dual-hosted git repository.

kwin pushed a commit to branch feature/MNG-8214-improved-builder-in-velocity
in repository https://gitbox.apache.org/repos/asf/maven.git

commit d62176b719738b193469b7aa3ad1adf3a8a18e6d
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Fri Aug 16 18:15:11 2024 +0200

    [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 be741434ac..2d917798db 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<Object, InputLocation> locations
-    #end
-    ) {
+    #set ( $additionalArguments = "#if(${locationTracking}), Map<Object, InputLocation> 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;
+        this.modelEncoding = builder.modelEncoding;
     #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<Object, InputLocation> 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});
         }
     }