Stronger encapsulation of collection fields in MavenProject #1744
+705
−464
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Stronger encapsulation of collection fields with immutability and defensive copies. Clarification of the expectations about which fields or return values can be null.
Goals
The goal is to make easier to add new collections in a future commit (for a list of
<Source>
elements), or to modify the way that the current collections are computed (e.g. so that ifsourceDirectories
is not specified, then it is built by default from the<Source>
element). This is currently difficult to do if we are unsure about the contracts of existing collections.Main changes
The following rules are applied in this commit (the previous situation was a mix of different practices):
getDependencyArtifacts()
return an empty collection instead ofnull
.addFoo(…)
method are immutable.clone()
method copies all non-immutable collections, and only them.MavenProject(MavenProject)
assigns fields directly without invoking setter methods, for avoiding the "this-escaped" compiler warning.An exception is made for the
getDependencyArtifacts()
method for compatibility reason, because we observed that Maven codes in other classes test for the nullity of that property instead of emptiness.Opportunistic changes
This pull request contains also the following changes. This changes are isolated in separated commits:
@Nullable
,@Nonnull
and@SuppressWarnings
annotations, and some null checks.toDependency(Artifact)
method.Stream
because it saves some of those precious lines limited by Checkstyle.For example, the following code:
Can be replaced by: