Stronger encapsulation of collection fields in MavenProject #1744
+720
−482
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 the current collections are computed (e.g. so that ifsourceDirectories
is no specified, it is built by default from the<Source>
). 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 of null.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
getDependencyArtifacts()
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 commit contains also the following changes:
@Nullable
,@Nonnull
and@SuppressWarnings
annotations, and some null checks.toDependency(Artifact)
method.For example, the following code:
Can be replaced by: