You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by "kwin (via GitHub)" <gi...@apache.org> on 2024/08/16 16:15:39 UTC
[PR] [MNG-8214] Improve model velocity template to support subclasses [maven]
kwin opened a new pull request, #1660:
URL: https://github.com/apache/maven/pull/1660
This makes the constructor protected and takes the Builder as argument
Following this checklist to help us incorporate your
contribution quickly and easily:
- [ ] Make sure there is a [JIRA issue](https://issues.apache.org/jira/browse/MNG) filed
for the change (usually before you start working on it). Trivial changes like typos do not
require a JIRA issue. Your pull request should address just this issue, without
pulling in other changes.
- [ ] Each commit in the pull request should have a meaningful subject line and body.
- [ ] Format the pull request title like `[MNG-XXX] SUMMARY`,
where you replace `MNG-XXX` and `SUMMARY` with the appropriate JIRA issue.
- [ ] Also format the first line of the commit message like `[MNG-XXX] SUMMARY`.
Best practice is to use the JIRA issue title in both the pull request title and in the first line of the commit message.
- [ ] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
- [ ] Run `mvn clean verify` to make sure basic checks pass. A more thorough check will
be performed on your pull request automatically.
- [ ] You have run the [Core IT][core-its] successfully.
If your pull request is about ~20 lines of code you don't need to sign an
[Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf) if you are unsure
please ask on the developers list.
To make clear that you license your contribution under
the [Apache License Version 2.0, January 2004](http://www.apache.org/licenses/LICENSE-2.0)
you have to acknowledge this by using the following check-box.
- [ ] I hereby declare this contribution to be licenced under the [Apache License Version 2.0, January 2004](http://www.apache.org/licenses/LICENSE-2.0)
- [ ] In any other case, please file an [Apache Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
[core-its]: https://maven.apache.org/core-its/core-it-suite/
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [MNG-8214] Improve model velocity template to support subclasses [maven]
Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet commented on code in PR #1660:
URL: https://github.com/apache/maven/pull/1660#discussion_r1731010876
##########
src/mdo/model.vm:
##########
@@ -145,55 +145,39 @@ public class ${class.name}
#end
/**
- * Constructor for this class, package protected.
+ * Constructor for this class, to be called from its subclasses and {@link Builder}.
* @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,
- InputLocation importedFrom
- #end
- ) {
+ protected ${class.name}(Builder builder) {
#if ( $class.superClass )
- super(
- #foreach ( $field in $inheritedFields )
- #set ( $sep = "#if(${locationTracking}||$field!=${inheritedFields[${inheritedFields.size()} - 1]}),#end" )
- ${field.name}${sep}
- #end
- #if ( $locationTracking )
- locations,
- importedFrom
- #end
- );
+ super(builder);
#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 : "UTF-8");
#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 )
+ Map<Object, InputLocation> newlocs = builder.locations != null ? builder.locations : Collections.emptyMap();
+ Map<Object, InputLocation> oldlocs = builder.base != null && builder.base.locations != null ? builder.base.locations : Collections.emptyMap();
#if ( ! $class.superClass )
- this.locations = ImmutableCollections.copy(locations);
- this.importedFrom = importedFrom;
+ this.locations = new HashMap<>();
Review Comment:
> > I think it would be much easier to simply change the visibility of constructors to protected
>
> This would require duplicating all of the functionality from the super-classes `Builder.build()` method! This is exactly what I want to prevent with this approach.
If you find a better way that keep the current features... but the objects have to be immutable.
Honestly, this is a very rare case, so duplicating a few lines of code may not be so dramatic.
And as I said, the model is now supposed to evolve, rather than be hacked externally (with an outdated XSD + model which do not reflect the reality, etc...)
The code in m-plugin-tools would really benefit from an overall cleanup, so that it can reuse the generated plugin.xml readers/writers and the real model classes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [MNG-8214] Improve model velocity template to support subclasses [maven]
Posted by "kwin (via GitHub)" <gi...@apache.org>.
kwin commented on PR #1660:
URL: https://github.com/apache/maven/pull/1660#issuecomment-2294852304
> However the use case of ExtendedMojoDescriptor is definitely a bad one.
I only partially agree, extending the MDO for this case is not useful as the additional attribute really only is useful in the plugin-tools context and therefore this class is only used internally. Although one could probably do things differently, the effort when not extending the original Maven classes is considerably higher (as one somehow needs to establish the connection between the raw Maven object and the extended plugin tools one).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [MNG-8214] Improve model velocity template to support subclasses [maven]
Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet commented on code in PR #1660:
URL: https://github.com/apache/maven/pull/1660#discussion_r1731523749
##########
src/mdo/model.vm:
##########
@@ -493,40 +482,15 @@ public class ${class.name}
#end
@Nonnull
public ${class.name} build() {
+ // this method should not contain any logic other than creating the object in order to ease subclassing
Review Comment:
Is that a hint that the below code should be removed ? If so, I disagree, as this is an important optimisation.
If not, maybe the line should be removed or made more explicit...
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [MNG-8214] Improve model velocity template to support subclasses [maven]
Posted by "kwin (via GitHub)" <gi...@apache.org>.
kwin commented on code in PR #1660:
URL: https://github.com/apache/maven/pull/1660#discussion_r1720744714
##########
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;
Review Comment:
This requires the base inheritance logic as well
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [MNG-8214] Improve model velocity template to support subclasses [maven]
Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet commented on code in PR #1660:
URL: https://github.com/apache/maven/pull/1660#discussion_r1729584195
##########
src/mdo/model.vm:
##########
@@ -145,55 +145,39 @@ public class ${class.name}
#end
/**
- * Constructor for this class, package protected.
+ * Constructor for this class, to be called from its subclasses and {@link Builder}.
* @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,
- InputLocation importedFrom
- #end
- ) {
+ protected ${class.name}(Builder builder) {
#if ( $class.superClass )
- super(
- #foreach ( $field in $inheritedFields )
- #set ( $sep = "#if(${locationTracking}||$field!=${inheritedFields[${inheritedFields.size()} - 1]}),#end" )
- ${field.name}${sep}
- #end
- #if ( $locationTracking )
- locations,
- importedFrom
- #end
- );
+ super(builder);
#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 : "UTF-8");
#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 )
+ Map<Object, InputLocation> newlocs = builder.locations != null ? builder.locations : Collections.emptyMap();
+ Map<Object, InputLocation> oldlocs = builder.base != null && builder.base.locations != null ? builder.base.locations : Collections.emptyMap();
#if ( ! $class.superClass )
- this.locations = ImmutableCollections.copy(locations);
- this.importedFrom = importedFrom;
+ this.locations = new HashMap<>();
Review Comment:
Fields should be immutable. Maybe the generated should look like:
```
this.locations = Map.of(
"", newlocs.containsKey("") ? newlocs.get("") : oldlocs.get(""),
"modules", newlocs.containsKey("modules") ? newlocs.get("modules") : oldlocs.get("modules"),
"distributionManagement", newlocs.containsKey("distributionManagement") ? newlocs.get("distributionManagement") : oldlocs.get("distributionManagement"),
"properties", newlocs.containsKey("properties") ? newlocs.get("properties") : oldlocs.get("properties"),
"dependencyManagement", newlocs.containsKey("dependencyManagement") ? newlocs.get("dependencyManagement") : oldlocs.get("dependencyManagement"),
"dependencies", newlocs.containsKey("dependencies") ? newlocs.get("dependencies") : oldlocs.get("dependencies"),
"repositories", newlocs.containsKey("repositories") ? newlocs.get("repositories") : oldlocs.get("repositories"),
"pluginRepositories", newlocs.containsKey("pluginRepositories") ? newlocs.get("pluginRepositories") : oldlocs.get("pluginRepositories"),
"reporting", newlocs.containsKey("reporting") ? newlocs.get("reporting") : oldlocs.get("reporting"));
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [MNG-8214] Improve model velocity template to support subclasses [maven]
Posted by "kwin (via GitHub)" <gi...@apache.org>.
kwin commented on code in PR #1660:
URL: https://github.com/apache/maven/pull/1660#discussion_r1730778218
##########
src/mdo/model.vm:
##########
@@ -145,55 +145,39 @@ public class ${class.name}
#end
/**
- * Constructor for this class, package protected.
+ * Constructor for this class, to be called from its subclasses and {@link Builder}.
* @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,
- InputLocation importedFrom
- #end
- ) {
+ protected ${class.name}(Builder builder) {
#if ( $class.superClass )
- super(
- #foreach ( $field in $inheritedFields )
- #set ( $sep = "#if(${locationTracking}||$field!=${inheritedFields[${inheritedFields.size()} - 1]}),#end" )
- ${field.name}${sep}
- #end
- #if ( $locationTracking )
- locations,
- importedFrom
- #end
- );
+ super(builder);
#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 : "UTF-8");
#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 )
+ Map<Object, InputLocation> newlocs = builder.locations != null ? builder.locations : Collections.emptyMap();
+ Map<Object, InputLocation> oldlocs = builder.base != null && builder.base.locations != null ? builder.base.locations : Collections.emptyMap();
#if ( ! $class.superClass )
- this.locations = ImmutableCollections.copy(locations);
- this.importedFrom = importedFrom;
+ this.locations = new HashMap<>();
Review Comment:
Right, however using `Map.of` or `Map.ofEntries` prevents using proper inheritance (i.e. only add locations for local/non-inherited fields). That approach would require adding all entries/completely overwriting the `locations` inherited from the super class.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [MNG-8214] Improve model velocity template to support subclasses [maven]
Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet commented on code in PR #1660:
URL: https://github.com/apache/maven/pull/1660#discussion_r1720833527
##########
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" )
Review Comment:
The `$additionalArguments` need to be removed, they are also fields on the `builder` argument.
##########
src/mdo/model.vm:
##########
@@ -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" )
Review Comment:
Same here
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [MNG-8214] Improve model velocity template to support subclasses [maven]
Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet commented on code in PR #1660:
URL: https://github.com/apache/maven/pull/1660#discussion_r1730817966
##########
src/mdo/model.vm:
##########
@@ -145,55 +145,39 @@ public class ${class.name}
#end
/**
- * Constructor for this class, package protected.
+ * Constructor for this class, to be called from its subclasses and {@link Builder}.
* @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,
- InputLocation importedFrom
- #end
- ) {
+ protected ${class.name}(Builder builder) {
#if ( $class.superClass )
- super(
- #foreach ( $field in $inheritedFields )
- #set ( $sep = "#if(${locationTracking}||$field!=${inheritedFields[${inheritedFields.size()} - 1]}),#end" )
- ${field.name}${sep}
- #end
- #if ( $locationTracking )
- locations,
- importedFrom
- #end
- );
+ super(builder);
#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 : "UTF-8");
#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 )
+ Map<Object, InputLocation> newlocs = builder.locations != null ? builder.locations : Collections.emptyMap();
+ Map<Object, InputLocation> oldlocs = builder.base != null && builder.base.locations != null ? builder.base.locations : Collections.emptyMap();
#if ( ! $class.superClass )
- this.locations = ImmutableCollections.copy(locations);
- this.importedFrom = importedFrom;
+ this.locations = new HashMap<>();
Review Comment:
I really dislike the knowledge that is being spread across the `Builder#build()` method and the actual constructor. But the shallow-copy would be really a performance loss during the initial transformation of the model.
I think it would be much easier to simply change the visibility of constructors to `protected`, I think this would be enough to allow extension from outside the package, as the generated code already allows extension (as we have `Model` extending `ModelBase` and other similar classes).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [MNG-8214] Improve model velocity template to support subclasses [maven]
Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet commented on PR #1660:
URL: https://github.com/apache/maven/pull/1660#issuecomment-2294825243
> This is not about inheritance within MDO models but inheritance of classes generated from MDO, like for https://github.com/apache/maven-plugin-tools/blob/9332b09acc08d2e063f12a8158a57ca8ec56fadd/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/ExtendedMojoDescriptor.java#L31. This is currently not possible due to the restricted visibility of the constructors and the lack of a constructor taking a builder as argument.
Fair point. However the use case of ExtendedMojoDescriptor is definitely a bad one. Those were needed because the models did not really evolved, but we allowed small additions without properly versioning them, and then rely on such hacks.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [MNG-8214] Improve model velocity template to support subclasses [maven]
Posted by "kwin (via GitHub)" <gi...@apache.org>.
kwin merged PR #1660:
URL: https://github.com/apache/maven/pull/1660
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [MNG-8214] Improve model velocity template to support subclasses [maven]
Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet commented on PR #1660:
URL: https://github.com/apache/maven/pull/1660#issuecomment-2294375827
I don't really see what's your use case for that ? We already have inheritance such as `Build` extends `BuildBase` and it works correctly. Is that just a cleanup ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [MNG-8214] Improve model velocity template to support subclasses [maven]
Posted by "kwin (via GitHub)" <gi...@apache.org>.
kwin commented on PR #1660:
URL: https://github.com/apache/maven/pull/1660#issuecomment-2294799091
This is not about inheritance within MDO models but inheritance of classes generated from MDO, like for https://github.com/apache/maven-plugin-tools/blob/9332b09acc08d2e063f12a8158a57ca8ec56fadd/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/ExtendedMojoDescriptor.java#L31. This is currently not possible due to the restricted visibility of the constructors and the lack of a constructor taking a builder as argument.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [MNG-8214] Improve model velocity template to support subclasses [maven]
Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet commented on code in PR #1660:
URL: https://github.com/apache/maven/pull/1660#discussion_r1731010876
##########
src/mdo/model.vm:
##########
@@ -145,55 +145,39 @@ public class ${class.name}
#end
/**
- * Constructor for this class, package protected.
+ * Constructor for this class, to be called from its subclasses and {@link Builder}.
* @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,
- InputLocation importedFrom
- #end
- ) {
+ protected ${class.name}(Builder builder) {
#if ( $class.superClass )
- super(
- #foreach ( $field in $inheritedFields )
- #set ( $sep = "#if(${locationTracking}||$field!=${inheritedFields[${inheritedFields.size()} - 1]}),#end" )
- ${field.name}${sep}
- #end
- #if ( $locationTracking )
- locations,
- importedFrom
- #end
- );
+ super(builder);
#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 : "UTF-8");
#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 )
+ Map<Object, InputLocation> newlocs = builder.locations != null ? builder.locations : Collections.emptyMap();
+ Map<Object, InputLocation> oldlocs = builder.base != null && builder.base.locations != null ? builder.base.locations : Collections.emptyMap();
#if ( ! $class.superClass )
- this.locations = ImmutableCollections.copy(locations);
- this.importedFrom = importedFrom;
+ this.locations = new HashMap<>();
Review Comment:
> > I think it would be much easier to simply change the visibility of constructors to protected
>
> This would require duplicating all of the functionality from the super-classes `Builder.build()` method! This is exactly what I want to prevent with this approach.
If you find a better way that keep the current features... but the objects have to be immutable.
Honestly, this is a very rare case, so duplicating a few lines of code may not be so dramatic.
And as I said, the model is now supposed to evolve, rather than be hacked externally (with an outdated XSD + model which do not reflect the reality, etc...)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [MNG-8214] Improve model velocity template to support subclasses [maven]
Posted by "kwin (via GitHub)" <gi...@apache.org>.
kwin commented on code in PR #1660:
URL: https://github.com/apache/maven/pull/1660#discussion_r1732258290
##########
src/mdo/model.vm:
##########
@@ -493,40 +482,15 @@ public class ${class.name}
#end
@Nonnull
public ${class.name} build() {
+ // this method should not contain any logic other than creating the object in order to ease subclassing
Review Comment:
No it isn't, this needs to be kept as optimization. I clarified the comment in https://github.com/apache/maven/pull/1660/commits/effa73709e4b49a9509f0891ee3c6225b0b9a02c.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [MNG-8214] Improve model velocity template to support subclasses [maven]
Posted by "kwin (via GitHub)" <gi...@apache.org>.
kwin commented on code in PR #1660:
URL: https://github.com/apache/maven/pull/1660#discussion_r1730881137
##########
src/mdo/model.vm:
##########
@@ -145,55 +145,39 @@ public class ${class.name}
#end
/**
- * Constructor for this class, package protected.
+ * Constructor for this class, to be called from its subclasses and {@link Builder}.
* @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,
- InputLocation importedFrom
- #end
- ) {
+ protected ${class.name}(Builder builder) {
#if ( $class.superClass )
- super(
- #foreach ( $field in $inheritedFields )
- #set ( $sep = "#if(${locationTracking}||$field!=${inheritedFields[${inheritedFields.size()} - 1]}),#end" )
- ${field.name}${sep}
- #end
- #if ( $locationTracking )
- locations,
- importedFrom
- #end
- );
+ super(builder);
#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 : "UTF-8");
#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 )
+ Map<Object, InputLocation> newlocs = builder.locations != null ? builder.locations : Collections.emptyMap();
+ Map<Object, InputLocation> oldlocs = builder.base != null && builder.base.locations != null ? builder.base.locations : Collections.emptyMap();
#if ( ! $class.superClass )
- this.locations = ImmutableCollections.copy(locations);
- this.importedFrom = importedFrom;
+ this.locations = new HashMap<>();
Review Comment:
> I think it would be much easier to simply change the visibility of constructors to protected
This would require duplicating all of the functionality from the super-classes `Builder.build()` method! This is exactly what I want to prevent with this approach.
The `locations` map contains `null` values therefore cannot leverage `Map.ofEntries`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [MNG-8214] Improve model velocity template to support subclasses [maven]
Posted by "kwin (via GitHub)" <gi...@apache.org>.
kwin commented on code in PR #1660:
URL: https://github.com/apache/maven/pull/1660#discussion_r1730292226
##########
src/mdo/model.vm:
##########
@@ -145,55 +145,39 @@ public class ${class.name}
#end
/**
- * Constructor for this class, package protected.
+ * Constructor for this class, to be called from its subclasses and {@link Builder}.
* @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,
- InputLocation importedFrom
- #end
- ) {
+ protected ${class.name}(Builder builder) {
#if ( $class.superClass )
- super(
- #foreach ( $field in $inheritedFields )
- #set ( $sep = "#if(${locationTracking}||$field!=${inheritedFields[${inheritedFields.size()} - 1]}),#end" )
- ${field.name}${sep}
- #end
- #if ( $locationTracking )
- locations,
- importedFrom
- #end
- );
+ super(builder);
#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 : "UTF-8");
#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 )
+ Map<Object, InputLocation> newlocs = builder.locations != null ? builder.locations : Collections.emptyMap();
+ Map<Object, InputLocation> oldlocs = builder.base != null && builder.base.locations != null ? builder.base.locations : Collections.emptyMap();
#if ( ! $class.superClass )
- this.locations = ImmutableCollections.copy(locations);
- this.importedFrom = importedFrom;
+ this.locations = new HashMap<>();
Review Comment:
This is orthogonal to this PR. The same logic from https://github.com/apache/maven/pull/1660/files#diff-ad6fc545933fb055ede5c7cf4ca4f05c63de54967e3253e4244b00adcaa7dcc9L503-L511 just has been moved to the constructor.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [MNG-8214] Improve model velocity template to support subclasses [maven]
Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet commented on code in PR #1660:
URL: https://github.com/apache/maven/pull/1660#discussion_r1730326047
##########
src/mdo/model.vm:
##########
@@ -145,55 +145,39 @@ public class ${class.name}
#end
/**
- * Constructor for this class, package protected.
+ * Constructor for this class, to be called from its subclasses and {@link Builder}.
* @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,
- InputLocation importedFrom
- #end
- ) {
+ protected ${class.name}(Builder builder) {
#if ( $class.superClass )
- super(
- #foreach ( $field in $inheritedFields )
- #set ( $sep = "#if(${locationTracking}||$field!=${inheritedFields[${inheritedFields.size()} - 1]}),#end" )
- ${field.name}${sep}
- #end
- #if ( $locationTracking )
- locations,
- importedFrom
- #end
- );
+ super(builder);
#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 : "UTF-8");
#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 )
+ Map<Object, InputLocation> newlocs = builder.locations != null ? builder.locations : Collections.emptyMap();
+ Map<Object, InputLocation> oldlocs = builder.base != null && builder.base.locations != null ? builder.base.locations : Collections.emptyMap();
#if ( ! $class.superClass )
- this.locations = ImmutableCollections.copy(locations);
- this.importedFrom = importedFrom;
+ this.locations = new HashMap<>();
Review Comment:
Well, no. The constructor was getting a possibly mutable map, but it was doing an immutable copy before assigning it to the field.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [MNG-8214] Improve model velocity template to support subclasses [maven]
Posted by "kwin (via GitHub)" <gi...@apache.org>.
kwin commented on code in PR #1660:
URL: https://github.com/apache/maven/pull/1660#discussion_r1731437500
##########
src/mdo/model.vm:
##########
@@ -145,55 +145,39 @@ public class ${class.name}
#end
/**
- * Constructor for this class, package protected.
+ * Constructor for this class, to be called from its subclasses and {@link Builder}.
* @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,
- InputLocation importedFrom
- #end
- ) {
+ protected ${class.name}(Builder builder) {
#if ( $class.superClass )
- super(
- #foreach ( $field in $inheritedFields )
- #set ( $sep = "#if(${locationTracking}||$field!=${inheritedFields[${inheritedFields.size()} - 1]}),#end" )
- ${field.name}${sep}
- #end
- #if ( $locationTracking )
- locations,
- importedFrom
- #end
- );
+ super(builder);
#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 : "UTF-8");
#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 )
+ Map<Object, InputLocation> newlocs = builder.locations != null ? builder.locations : Collections.emptyMap();
+ Map<Object, InputLocation> oldlocs = builder.base != null && builder.base.locations != null ? builder.base.locations : Collections.emptyMap();
#if ( ! $class.superClass )
- this.locations = ImmutableCollections.copy(locations);
- this.importedFrom = importedFrom;
+ this.locations = new HashMap<>();
Review Comment:
Done now in https://github.com/apache/maven/pull/1660/commits/8ce4cdedd2040f9a97b2cc6a586b67b4aae749e6. Please check again.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org