Skip to content

Commit

Permalink
Exception usage cleanup (#1910)
Browse files Browse the repository at this point in the history
This is just a cleanup of exception usage (by making them checked, fixing compiler issues, and undoing the change). There are at least two bugs (runtime escapes) fixed in this PR.
  • Loading branch information
cstamas authored Nov 16, 2024
1 parent c4834ef commit ab7d766
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.maven.api.model.Repository;
import org.apache.maven.api.services.ArtifactCoordinatesFactory;
import org.apache.maven.api.services.DependencyCoordinatesFactory;
import org.apache.maven.api.services.VersionResolverException;
import org.apache.maven.api.settings.Settings;

/**
Expand Down Expand Up @@ -742,7 +743,7 @@ Map<PathType, List<Path>> resolveDependencies(
* @see org.apache.maven.api.services.VersionResolver#resolve(Session, ArtifactCoordinates) (String)
*/
@Nonnull
Version resolveVersion(@Nonnull ArtifactCoordinates artifact);
Version resolveVersion(@Nonnull ArtifactCoordinates artifact) throws VersionResolverException;

/**
* Expands a version range to a list of matching versions, in ascending order.
Expand All @@ -758,7 +759,7 @@ Map<PathType, List<Path>> resolveDependencies(
* @see org.apache.maven.api.services.VersionRangeResolver#resolve(Session, ArtifactCoordinates) (String)
*/
@Nonnull
List<Version> resolveVersionRange(@Nonnull ArtifactCoordinates artifact);
List<Version> resolveVersionRange(@Nonnull ArtifactCoordinates artifact) throws VersionResolverException;

/**
* Expands a version range to a list of matching versions, in ascending order.
Expand All @@ -775,7 +776,8 @@ Map<PathType, List<Path>> resolveDependencies(
* @see org.apache.maven.api.services.VersionRangeResolver#resolve(Session, ArtifactCoordinates) (String)
*/
@Nonnull
List<Version> resolveVersionRange(@Nonnull ArtifactCoordinates artifact, List<RemoteRepository> repositories);
List<Version> resolveVersionRange(@Nonnull ArtifactCoordinates artifact, List<RemoteRepository> repositories)
throws VersionResolverException;

/**
* Parses the specified version string, for example "1.0".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ interface ModelBuilderSession {
ModelBuilderResult build(ModelBuilderRequest request) throws ModelBuilderException;
}

Model buildRawModel(ModelBuilderRequest request);
Model buildRawModel(ModelBuilderRequest request) throws ModelBuilderException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public interface ProjectBuilder extends Service {
* @throws IllegalArgumentException if an argument is {@code null} or invalid
*/
@Nonnull
ProjectBuilderResult build(ProjectBuilderRequest request);
ProjectBuilderResult build(ProjectBuilderRequest request) throws ProjectBuilderException;

/**
* Creates a {@link org.apache.maven.api.Project} from a POM file.
Expand All @@ -52,7 +52,8 @@ public interface ProjectBuilder extends Service {
* @see #build(ProjectBuilderRequest)
*/
@Nonnull
default ProjectBuilderResult build(@Nonnull Session session, @Nonnull Source source) {
default ProjectBuilderResult build(@Nonnull Session session, @Nonnull Source source)
throws ProjectBuilderException {
return build(ProjectBuilderRequest.build(session, source));
}

Expand All @@ -66,7 +67,7 @@ default ProjectBuilderResult build(@Nonnull Session session, @Nonnull Source sou
* @see #build(ProjectBuilderRequest)
*/
@Nonnull
default ProjectBuilderResult build(@Nonnull Session session, @Nonnull Path path) {
default ProjectBuilderResult build(@Nonnull Session session, @Nonnull Path path) throws ProjectBuilderException {
return build(ProjectBuilderRequest.build(session, path));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.nio.file.Path;

import org.apache.maven.api.model.Model;
import org.apache.maven.model.building.ModelBuildingException;
import org.apache.maven.api.services.ModelBuilderException;
import org.apache.maven.project.MavenProject;
import org.eclipse.aether.RepositorySystemSession;

Expand All @@ -35,5 +35,5 @@
interface ConsumerPomBuilder {

Model build(RepositorySystemSession session, MavenProject project, Path src)
throws ModelBuildingException, IOException, XMLStreamException;
throws ModelBuilderException, IOException, XMLStreamException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@

import org.apache.maven.api.feature.Features;
import org.apache.maven.api.model.Model;
import org.apache.maven.api.services.ModelBuilderException;
import org.apache.maven.internal.transformation.ConsumerPomArtifactTransformer;
import org.apache.maven.model.building.ModelBuildingException;
import org.apache.maven.model.v4.MavenStaxWriter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.artifact.ProjectArtifact;
Expand Down Expand Up @@ -112,7 +112,7 @@ TransformedArtifact createConsumerPomArtifact(
}

void transform(MavenProject project, RepositorySystemSession session, Path src, Path tgt)
throws ModelBuildingException, XMLStreamException, IOException {
throws ModelBuilderException, XMLStreamException, IOException {
Model model = builder.build(session, project, src);
write(model, tgt);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;

import org.apache.maven.api.services.ModelBuilderException;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.internal.transformation.TransformationFailedException;
import org.apache.maven.model.building.ModelBuildingException;
import org.apache.maven.project.MavenProject;
import org.eclipse.aether.RepositorySystemSession;

Expand Down Expand Up @@ -97,13 +97,12 @@ public synchronized File getFile() {
return null;
}
return target.toFile();
} catch (IOException | NoSuchAlgorithmException | XMLStreamException | ModelBuildingException e) {
} catch (IOException | NoSuchAlgorithmException | XMLStreamException | ModelBuilderException e) {
throw new TransformationFailedException(e);
}
}

private String mayUpdate()
throws IOException, NoSuchAlgorithmException, XMLStreamException, ModelBuildingException {
private String mayUpdate() throws IOException, NoSuchAlgorithmException, XMLStreamException, ModelBuilderException {
String result;
Path src = sourcePathProvider.get();
if (src == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
import org.apache.maven.api.services.VersionParser;
import org.apache.maven.api.services.VersionRangeResolver;
import org.apache.maven.api.services.VersionResolver;
import org.apache.maven.api.services.VersionResolverException;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
Expand Down Expand Up @@ -839,17 +840,18 @@ public VersionConstraint parseVersionConstraint(String versionConstraint) {
}

@Override
public Version resolveVersion(ArtifactCoordinates artifact) {
public Version resolveVersion(ArtifactCoordinates artifact) throws VersionResolverException {
return getService(VersionResolver.class).resolve(this, artifact).getVersion();
}

@Override
public List<Version> resolveVersionRange(ArtifactCoordinates artifact) {
public List<Version> resolveVersionRange(ArtifactCoordinates artifact) throws VersionResolverException {
return getService(VersionRangeResolver.class).resolve(this, artifact).getVersions();
}

@Override
public List<Version> resolveVersionRange(ArtifactCoordinates artifact, List<RemoteRepository> repositories) {
public List<Version> resolveVersionRange(ArtifactCoordinates artifact, List<RemoteRepository> repositories)
throws VersionResolverException {
return getService(VersionRangeResolver.class)
.resolve(this, artifact, repositories)
.getVersions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ Model readParent(Model childModel) throws ModelBuilderException {
return parentModel;
}

private Model resolveParent(Model childModel) {
private Model resolveParent(Model childModel) throws ModelBuilderException {
Model parentModel = null;
if (isBuildRequest()) {
parentModel = readParentLocally(childModel);
Expand All @@ -906,7 +906,7 @@ private Model resolveParent(Model childModel) {
}

private Model readParentLocally(Model childModel) throws ModelBuilderException {
ModelSource candidateSource = null;
ModelSource candidateSource;

Parent parent = childModel.getParent();
String parentPath = parent.getRelativePath();
Expand Down Expand Up @@ -1493,7 +1493,7 @@ private Model doReadRawModel() throws ModelBuilderException {
/**
* Reads the request source's parent.
*/
Model readAsParentModel() {
Model readAsParentModel() throws ModelBuilderException {
return cache(request.getSource(), PARENT, this::doReadAsParentModel);
}

Expand Down Expand Up @@ -1664,7 +1664,7 @@ private Model doLoadDependencyManagement(
importSource = modelResolver.resolveModel(
request.getSession(), repositories, dependency, new AtomicReference<>());
}
} catch (ModelBuilderException e) {
} catch (ModelBuilderException | ModelResolverException e) {
StringBuilder buffer = new StringBuilder(256);
buffer.append("Non-resolvable import POM");
if (!containsCoordinates(e.getMessage(), groupId, artifactId, version)) {
Expand Down Expand Up @@ -1719,7 +1719,8 @@ private Model doLoadDependencyManagement(
return importModel;
}

ModelSource resolveReactorModel(String groupId, String artifactId, String version) {
ModelSource resolveReactorModel(String groupId, String artifactId, String version)
throws ModelBuilderException {
Set<ModelSource> sources = mappedSources.get(new GAKey(groupId, artifactId));
if (sources != null) {
for (ModelSource source : sources) {
Expand All @@ -1737,7 +1738,7 @@ private <T> T cache(String groupId, String artifactId, String version, String ta
return cache.computeIfAbsent(groupId, artifactId, version, tag, supplier);
}

private <T> T cache(Source source, String tag, Supplier<T> supplier) {
private <T> T cache(Source source, String tag, Supplier<T> supplier) throws ModelBuilderException {
return cache.computeIfAbsent(source, tag, supplier);
}

Expand Down Expand Up @@ -1831,7 +1832,7 @@ private static List<String> getSubprojects(Model activated) {
public Model buildRawModel(ModelBuilderRequest request) throws ModelBuilderException {
ModelBuilderSessionState build = new ModelBuilderSessionState(request);
Model model = build.readRawModel();
if (((ModelProblemCollector) build).hasErrors()) {
if (build.hasErrors()) {
throw build.newModelBuilderException();
}
return model;
Expand Down

0 comments on commit ab7d766

Please sign in to comment.