Skip to content

Commit

Permalink
[MNG-8484] Use List.copyOf to make a defensive immutable copy of the …
Browse files Browse the repository at this point in the history
…input collection
gnodet committed Jan 13, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent f5a407c commit 81b7565
Showing 5 changed files with 7 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
package org.apache.maven.api.services;

import java.util.Collection;
import java.util.List;

import org.apache.maven.api.ProducedArtifact;
import org.apache.maven.api.RemoteRepository;
@@ -115,7 +116,7 @@ private static class DefaultArtifactDeployerRequest extends BaseRequest<Session>
int retryFailedDeploymentCount) {
super(session);
this.repository = requireNonNull(repository, "repository cannot be null");
this.artifacts = unmodifiable(requireNonNull(artifacts, "artifacts cannot be null"));
this.artifacts = List.copyOf(requireNonNull(artifacts, "artifacts cannot be null"));
this.retryFailedDeploymentCount = retryFailedDeploymentCount;
}

Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.apache.maven.api.ProducedArtifact;
import org.apache.maven.api.Session;
@@ -89,7 +90,7 @@ static class DefaultArtifactInstallerRequest extends BaseRequest<Session> implem

DefaultArtifactInstallerRequest(@Nonnull Session session, @Nonnull Collection<ProducedArtifact> artifacts) {
super(session);
this.artifacts = unmodifiable(requireNonNull(artifacts, "artifacts cannot be null"));
this.artifacts = List.copyOf(requireNonNull(artifacts, "artifacts cannot be null"));
}

@Nonnull
Original file line number Diff line number Diff line change
@@ -119,7 +119,7 @@ private static class DefaultArtifactResolverRequest extends BaseRequest<Session>
@Nonnull Collection<? extends ArtifactCoordinates> coordinates,
@Nonnull List<RemoteRepository> repositories) {
super(session);
this.coordinates = unmodifiable(requireNonNull(coordinates, "coordinates cannot be null"));
this.coordinates = List.copyOf(requireNonNull(coordinates, "coordinates cannot be null"));
this.repositories = repositories;
}

Original file line number Diff line number Diff line change
@@ -18,10 +18,6 @@
*/
package org.apache.maven.api.services;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import org.apache.maven.api.ProtoSession;
import org.apache.maven.api.annotations.Experimental;
import org.apache.maven.api.annotations.Nonnull;
@@ -46,10 +42,4 @@ protected BaseRequest(@Nonnull S session) {
public S getSession() {
return session;
}

protected static <T> Collection<T> unmodifiable(Collection<T> obj) {
return obj != null && !obj.isEmpty()
? Collections.unmodifiableCollection(new ArrayList<>(obj))
: Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
@@ -400,9 +400,9 @@ static class DefaultDependencyResolverRequest extends BaseRequest<Session>
this.project = project;
this.rootArtifact = rootArtifact;
this.root = root;
this.dependencies = unmodifiable(requireNonNull(dependencies, "dependencies cannot be null"));
this.dependencies = List.copyOf(requireNonNull(dependencies, "dependencies cannot be null"));
this.managedDependencies =
unmodifiable(requireNonNull(managedDependencies, "managedDependencies cannot be null"));
List.copyOf(requireNonNull(managedDependencies, "managedDependencies cannot be null"));
this.verbose = verbose;
this.pathScope = requireNonNull(pathScope, "pathScope cannot be null");
this.pathTypeFilter = (pathTypeFilter != null) ? pathTypeFilter : (t) -> true;

0 comments on commit 81b7565

Please sign in to comment.