Better internal helper methods (#1334)

This commit is contained in:
Guillaume Nodet 2023-12-07 21:05:02 +01:00 committed by GitHub
parent c4f62ff7c8
commit 71eb6de7e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 68 additions and 69 deletions

View File

@ -29,7 +29,6 @@ import java.util.Objects;
import java.util.Optional;
import java.util.WeakHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.maven.api.Artifact;
@ -64,6 +63,7 @@ import org.apache.maven.api.services.VersionParser;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.project.MavenProject;
import static org.apache.maven.internal.impl.Utils.map;
import static org.apache.maven.internal.impl.Utils.nonNull;
public abstract class AbstractSession implements InternalSession {
@ -102,7 +102,7 @@ public abstract class AbstractSession implements InternalSession {
}
public List<Project> getProjects(List<MavenProject> projects) {
return projects == null ? null : projects.stream().map(this::getProject).collect(Collectors.toList());
return projects == null ? null : map(projects, this::getProject);
}
public Project getProject(MavenProject project) {
@ -110,9 +110,7 @@ public abstract class AbstractSession implements InternalSession {
}
public List<org.eclipse.aether.repository.RemoteRepository> toRepositories(List<RemoteRepository> repositories) {
return repositories == null
? null
: repositories.stream().map(this::toRepository).collect(Collectors.toList());
return repositories == null ? null : map(repositories, this::toRepository);
}
public org.eclipse.aether.repository.RemoteRepository toRepository(RemoteRepository repository) {
@ -134,25 +132,19 @@ public abstract class AbstractSession implements InternalSession {
}
public List<ArtifactRepository> toArtifactRepositories(List<RemoteRepository> repositories) {
return repositories == null
? null
: repositories.stream().map(this::toArtifactRepository).collect(Collectors.toList());
return repositories == null ? null : map(repositories, this::toArtifactRepository);
}
public abstract ArtifactRepository toArtifactRepository(RemoteRepository repository);
public List<org.eclipse.aether.graph.Dependency> toDependencies(Collection<DependencyCoordinate> dependencies) {
return dependencies == null
? null
: dependencies.stream().map(this::toDependency).collect(Collectors.toList());
return dependencies == null ? null : map(dependencies, this::toDependency);
}
public abstract org.eclipse.aether.graph.Dependency toDependency(DependencyCoordinate dependency);
public List<org.eclipse.aether.artifact.Artifact> toArtifacts(Collection<Artifact> artifacts) {
return artifacts == null
? null
: artifacts.stream().map(this::toArtifact).collect(Collectors.toList());
return artifacts == null ? null : map(artifacts, this::toArtifact);
}
public org.eclipse.aether.artifact.Artifact toArtifact(Artifact artifact) {

View File

@ -36,8 +36,8 @@ public class DefaultArtifact implements Artifact {
private final String id;
public DefaultArtifact(@Nonnull InternalSession session, @Nonnull org.eclipse.aether.artifact.Artifact artifact) {
this.session = nonNull(session, "session can not be null");
this.artifact = nonNull(artifact, "artifact can not be null");
this.session = nonNull(session, "session");
this.artifact = nonNull(artifact, "artifact");
this.id = getGroupId()
+ ':'
+ getArtifactId()

View File

@ -35,8 +35,8 @@ public class DefaultArtifactCoordinate implements ArtifactCoordinate {
public DefaultArtifactCoordinate(
@Nonnull InternalSession session, @Nonnull org.eclipse.aether.artifact.Artifact coordinate) {
this.session = nonNull(session, "session can not be null");
this.coordinate = nonNull(coordinate, "coordinate can not be null");
this.session = nonNull(session, "session");
this.coordinate = nonNull(coordinate, "coordinate");
}
public org.eclipse.aether.artifact.Artifact getCoordinate() {

View File

@ -34,7 +34,7 @@ import static org.apache.maven.internal.impl.Utils.nonNull;
public class DefaultArtifactCoordinateFactory implements ArtifactCoordinateFactory {
@Override
public ArtifactCoordinate create(@Nonnull ArtifactCoordinateFactoryRequest request) {
nonNull(request, "request can not be null");
nonNull(request, "request");
InternalSession session = InternalSession.from(request.getSession());
ArtifactType type = null;
if (request.getType() != null) {

View File

@ -47,15 +47,15 @@ public class DefaultArtifactDeployer implements ArtifactDeployer {
@Inject
DefaultArtifactDeployer(@Nonnull RepositorySystem repositorySystem) {
this.repositorySystem = nonNull(repositorySystem, "repositorySystem can not be null");
this.repositorySystem = nonNull(repositorySystem, "repositorySystem");
}
@Override
public void deploy(@Nonnull ArtifactDeployerRequest request) {
nonNull(request, "request can not be null");
nonNull(request, "request");
InternalSession session = InternalSession.from(request.getSession());
Collection<Artifact> artifacts = nonNull(request.getArtifacts(), "request.artifacts can not be null");
RemoteRepository repository = nonNull(request.getRepository(), "request.repository can not be null");
Collection<Artifact> artifacts = nonNull(request.getArtifacts(), "request.artifacts");
RemoteRepository repository = nonNull(request.getRepository(), "request.repository");
try {
DeployRequest deployRequest = new DeployRequest()
.setRepository(session.toRepository(repository))

View File

@ -34,7 +34,7 @@ import static org.apache.maven.internal.impl.Utils.nonNull;
public class DefaultArtifactFactory implements ArtifactFactory {
@Override
public Artifact create(@Nonnull ArtifactFactoryRequest request) {
nonNull(request, "request can not be null");
nonNull(request, "request");
InternalSession session = InternalSession.from(request.getSession());
ArtifactType type = null;
if (request.getType() != null) {

View File

@ -46,7 +46,7 @@ public class DefaultArtifactInstaller implements ArtifactInstaller {
@Override
public void install(ArtifactInstallerRequest request) throws ArtifactInstallerException, IllegalArgumentException {
nonNull(request, "request can not be null");
nonNull(request, "request");
InternalSession session = InternalSession.from(request.getSession());
try {
InstallRequest installRequest =

View File

@ -51,13 +51,13 @@ public class DefaultArtifactResolver implements ArtifactResolver {
@Inject
DefaultArtifactResolver(@Nonnull RepositorySystem repositorySystem) {
this.repositorySystem = nonNull(repositorySystem, "repositorySystem can not be null");
this.repositorySystem = nonNull(repositorySystem, "repositorySystem");
}
@Override
public ArtifactResolverResult resolve(ArtifactResolverRequest request)
throws ArtifactResolverException, IllegalArgumentException {
nonNull(request, "request can not be null");
nonNull(request, "request");
InternalSession session = InternalSession.from(request.getSession());
try {
Map<Artifact, Path> paths = new HashMap<>();

View File

@ -58,7 +58,7 @@ public class DefaultDependencyCollector implements DependencyCollector {
@Override
public DependencyCollectorResult collect(@Nonnull DependencyCollectorRequest request)
throws DependencyCollectorException, IllegalArgumentException {
nonNull(request, "request can not be null");
nonNull(request, "request");
InternalSession session = InternalSession.from(request.getSession());
Artifact rootArtifact =

View File

@ -21,8 +21,6 @@ package org.apache.maven.internal.impl;
import javax.inject.Named;
import javax.inject.Singleton;
import java.util.stream.Collectors;
import org.apache.maven.api.DependencyCoordinate;
import org.apache.maven.api.Exclusion;
import org.apache.maven.api.annotations.Nonnull;
@ -30,6 +28,7 @@ import org.apache.maven.api.services.DependencyCoordinateFactory;
import org.apache.maven.api.services.DependencyCoordinateFactoryRequest;
import org.eclipse.aether.artifact.ArtifactType;
import static org.apache.maven.internal.impl.Utils.map;
import static org.apache.maven.internal.impl.Utils.nonNull;
@Named
@ -39,7 +38,7 @@ public class DefaultDependencyCoordinateFactory implements DependencyCoordinateF
@Nonnull
@Override
public DependencyCoordinate create(@Nonnull DependencyCoordinateFactoryRequest request) {
nonNull(request, "request can not be null");
nonNull(request, "request");
InternalSession session = InternalSession.from(request.getSession());
ArtifactType type = null;
@ -58,7 +57,7 @@ public class DefaultDependencyCoordinateFactory implements DependencyCoordinateF
type),
request.getScope(),
request.isOptional(),
request.getExclusions().stream().map(this::toExclusion).collect(Collectors.toList())));
map(request.getExclusions(), this::toExclusion)));
}
private org.eclipse.aether.graph.Exclusion toExclusion(Exclusion exclusion) {

View File

@ -40,7 +40,7 @@ public class DefaultDependencyProperties implements DependencyProperties {
}
public DefaultDependencyProperties(@Nonnull Collection<String> flags) {
nonNull(flags, "null flags");
nonNull(flags, "flags");
HashMap<String, String> map = new HashMap<>();
for (String flag : flags) {
map.put(flag, Boolean.TRUE.toString());
@ -49,7 +49,7 @@ public class DefaultDependencyProperties implements DependencyProperties {
}
public DefaultDependencyProperties(@Nonnull Map<String, String> properties) {
this.properties = Collections.unmodifiableMap(nonNull(properties, "null properties"));
this.properties = Collections.unmodifiableMap(nonNull(properties, "properties"));
}
@Nonnull
@ -60,7 +60,7 @@ public class DefaultDependencyProperties implements DependencyProperties {
@Override
public boolean checkFlag(@Nonnull String flag) {
nonNull(flag, "null flag");
nonNull(flag, "flag");
return Boolean.parseBoolean(properties.getOrDefault(flag, ""));
}
}

View File

@ -33,7 +33,7 @@ public class DefaultLocalRepository implements LocalRepository {
@Inject
public DefaultLocalRepository(@Nonnull org.eclipse.aether.repository.LocalRepository repository) {
this.repository = nonNull(repository, "repository can not be null");
this.repository = nonNull(repository, "repository");
}
@Nonnull

View File

@ -47,7 +47,7 @@ import static org.apache.maven.internal.impl.Utils.nonNull;
public class DefaultModelXmlFactory implements ModelXmlFactory {
@Override
public Model read(@Nonnull XmlReaderRequest request) throws XmlReaderException {
nonNull(request, "request can not be null");
nonNull(request, "request");
Path path = request.getPath();
URL url = request.getURL();
Reader reader = request.getReader();
@ -82,8 +82,8 @@ public class DefaultModelXmlFactory implements ModelXmlFactory {
@Override
public void write(XmlWriterRequest<Model> request) throws XmlWriterException {
nonNull(request, "request can not be null");
Model content = nonNull(request.getContent(), "content can not be null");
nonNull(request, "request");
Model content = nonNull(request.getContent(), "content");
Path path = request.getPath();
OutputStream outputStream = request.getOutputStream();
Writer writer = request.getWriter();

View File

@ -49,6 +49,8 @@ import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.eclipse.sisu.Typed;
import static org.apache.maven.internal.impl.Utils.map;
@Named
@Typed
@SessionScoped
@ -76,10 +78,9 @@ public class DefaultProjectManager implements ProjectManager {
@Override
public Collection<Artifact> getAttachedArtifacts(Project project) {
InternalSession session = ((DefaultProject) project).getSession();
Collection<Artifact> attached = getMavenProject(project).getAttachedArtifacts().stream()
.map(RepositoryUtils::toArtifact)
.map(session::getArtifact)
.collect(Collectors.toList());
Collection<Artifact> attached = map(
getMavenProject(project).getAttachedArtifacts(),
a -> session.getArtifact(RepositoryUtils.toArtifact(a)));
return Collections.unmodifiableCollection(attached);
}
@ -134,10 +135,7 @@ public class DefaultProjectManager implements ProjectManager {
InternalSession.from(session).getMavenSession(),
false,
Collections.emptySet());
return artifacts.stream()
.map(RepositoryUtils::toArtifact)
.map(InternalSession.from(session)::getArtifact)
.collect(Collectors.toList());
return map(artifacts, a -> InternalSession.from(session).getArtifact(RepositoryUtils.toArtifact(a)));
} catch (LifecycleExecutionException | ComponentLookupException e) {
throw new MavenException("Unable to resolve project dependencies", e);
}

View File

@ -27,7 +27,6 @@ import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.apache.maven.RepositoryUtils;
import org.apache.maven.api.DependencyCoordinate;
@ -54,6 +53,7 @@ import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import static org.apache.maven.internal.impl.Utils.map;
import static org.apache.maven.internal.impl.Utils.nonNull;
public class DefaultSession extends AbstractSession {
@ -80,10 +80,9 @@ public class DefaultSession extends AbstractSession {
this.repositorySystem = nonNull(repositorySystem);
this.repositories = repositories != null
? repositories
: mavenSession.getRequest().getRemoteRepositories().stream()
.map(RepositoryUtils::toRepo)
.map(this::getRemoteRepository)
.collect(Collectors.toList());
: map(
mavenSession.getRequest().getRemoteRepositories(),
r -> getRemoteRepository(RepositoryUtils.toRepo(r)));
this.mavenRepositorySystem = mavenRepositorySystem;
this.container = container;
this.runtimeInformation = runtimeInformation;

View File

@ -25,7 +25,6 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.Objects;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.services.xml.SettingsXmlFactory;
@ -38,12 +37,14 @@ import org.apache.maven.api.settings.Settings;
import org.apache.maven.settings.v4.SettingsStaxReader;
import org.apache.maven.settings.v4.SettingsStaxWriter;
import static org.apache.maven.internal.impl.Utils.nonNull;
@Named
@Singleton
public class DefaultSettingsXmlFactory implements SettingsXmlFactory {
@Override
public Settings read(@Nonnull XmlReaderRequest request) throws XmlReaderException {
Objects.requireNonNull(request, "request can not be null");
nonNull(request, "request");
Reader reader = request.getReader();
InputStream inputStream = request.getInputStream();
if (reader == null && inputStream == null) {
@ -68,8 +69,8 @@ public class DefaultSettingsXmlFactory implements SettingsXmlFactory {
@Override
public void write(XmlWriterRequest<Settings> request) throws XmlWriterException {
Objects.requireNonNull(request, "request can not be null");
Settings content = Objects.requireNonNull(request.getContent(), "content can not be null");
nonNull(request, "request");
Settings content = nonNull(request.getContent(), "content");
OutputStream outputStream = request.getOutputStream();
Writer writer = request.getWriter();
if (writer == null && outputStream == null) {

View File

@ -38,12 +38,14 @@ import org.apache.maven.api.toolchain.PersistedToolchains;
import org.apache.maven.toolchain.v4.MavenToolchainsStaxReader;
import org.apache.maven.toolchain.v4.MavenToolchainsStaxWriter;
import static org.apache.maven.internal.impl.Utils.nonNull;
@Named
@Singleton
public class DefaultToolchainsXmlFactory implements ToolchainsXmlFactory {
@Override
public PersistedToolchains read(@Nonnull XmlReaderRequest request) throws XmlReaderException {
Objects.requireNonNull(request, "request can not be null");
Objects.requireNonNull(request, "request");
Reader reader = request.getReader();
InputStream inputStream = request.getInputStream();
if (reader == null && inputStream == null) {
@ -68,8 +70,8 @@ public class DefaultToolchainsXmlFactory implements ToolchainsXmlFactory {
@Override
public void write(XmlWriterRequest<PersistedToolchains> request) throws XmlWriterException {
Objects.requireNonNull(request, "request can not be null");
PersistedToolchains content = Objects.requireNonNull(request.getContent(), "content can not be null");
nonNull(request, "request");
PersistedToolchains content = Objects.requireNonNull(request.getContent(), "content");
OutputStream outputStream = request.getOutputStream();
Writer writer = request.getWriter();
if (writer == null && outputStream == null) {

View File

@ -36,10 +36,10 @@ public class DefaultType implements Type, ArtifactType {
private final DependencyProperties dependencyProperties;
public DefaultType(String id, String extension, String classifier, DependencyProperties dependencyProperties) {
nonNull(id, "null id");
this.extension = nonNull(extension, "null extension");
nonNull(id, "id");
this.extension = nonNull(extension, "extension");
this.classifier = classifier;
nonNull(dependencyProperties, "null dependencyProperties");
nonNull(dependencyProperties, "dependencyProperties");
HashMap<String, String> props = new HashMap<>(dependencyProperties.asMap());
props.put(ArtifactProperties.TYPE, id);
this.dependencyProperties = new DefaultDependencyProperties(props);

View File

@ -70,7 +70,7 @@ public class DefaultTypeRegistry extends AbstractEventSpy implements TypeRegistr
@Override
@Nonnull
public Type getType(String id) {
nonNull(id, "null id");
nonNull(id, "id");
return usedTypes.computeIfAbsent(id, i -> {
Type type = types.get(id);
if (type == null) {

View File

@ -18,6 +18,11 @@
*/
package org.apache.maven.internal.impl;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
class Utils {
static <T> T nonNull(T t) {
if (t == null) {
@ -26,17 +31,21 @@ class Utils {
return t;
}
static <T> T nonNull(T t, String message) {
static <T> T nonNull(T t, String name) {
if (t == null) {
throw new IllegalArgumentException(message);
throw new IllegalArgumentException(name + " cannot be null");
}
return t;
}
static <T> T cast(Class<T> clazz, Object o, String message) {
static <T> T cast(Class<T> clazz, Object o, String name) {
if (!clazz.isInstance(o)) {
throw new IllegalArgumentException(message);
throw new IllegalArgumentException(name + " is not an instance of " + clazz.getName());
}
return clazz.cast(o);
}
static <U, V> List<V> map(Collection<U> list, Function<U, V> mapper) {
return list.stream().map(mapper).collect(Collectors.toList());
}
}

View File

@ -38,8 +38,7 @@ class WrapperNode extends AbstractNode {
@Override
DependencyNode getDependencyNode() {
return Utils.cast(AbstractNode.class, delegate, "delegate is not an instance of AbstractNode")
.getDependencyNode();
return Utils.cast(AbstractNode.class, delegate, "delegate").getDependencyNode();
}
@Override