This is an automated email from the ASF dual-hosted git repository. cstamas pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven.git
The following commit(s) were added to refs/heads/master by this push: new 4554700d36 [MNG-8631] Replace a `NullPointerException` by an `IllegalArgumentException` saying that the identifier scope is unknown. (#2160) 4554700d36 is described below commit 4554700d36b737796988b3d974a084412091f9e8 Author: Martin Desruisseaux <martin.desruisse...@geomatys.com> AuthorDate: Wed Mar 19 15:33:26 2025 +0100 [MNG-8631] Replace a `NullPointerException` by an `IllegalArgumentException` saying that the identifier scope is unknown. (#2160) `Session.requireDependencyScope(…)` is annotated as `@Nonnull` but its implementation can still return null, causing `NullPointerException` later. This pull request replace the null value by an `IllegalArgumentException` that describes the problem. --- https://issues.apache.org/jira/browse/MNG-8631 --- .../src/main/java/org/apache/maven/api/DependencyScope.java | 7 +++++++ api/maven-api-core/src/main/java/org/apache/maven/api/Session.java | 6 +++++- .../main/java/org/apache/maven/internal/impl/DefaultProject.java | 5 ++++- .../src/main/java/org/apache/maven/impl/AbstractSession.java | 6 +++++- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/DependencyScope.java b/api/maven-api-core/src/main/java/org/apache/maven/api/DependencyScope.java index f96d61f922..f9f788e003 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/DependencyScope.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/DependencyScope.java @@ -99,6 +99,13 @@ public enum DependencyScope { private static final Map<String, DependencyScope> IDS = Collections.unmodifiableMap( Stream.of(DependencyScope.values()).collect(Collectors.toMap(s -> s.id, s -> s))); + /** + * {@return the dependency scope for the given identifier, or {@code null} if none}. + * The identifiers are usually in lower cases with {@code '-'} instead of {@code '_'} + * as word separator. + * + * @param id the identifier of the scope (case-sensitive) + */ public static DependencyScope forId(String id) { return IDS.get(id); } diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/Session.java b/api/maven-api-core/src/main/java/org/apache/maven/api/Session.java index e5764615b4..3f43210dc0 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/Session.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/Session.java @@ -833,7 +833,11 @@ Optional<Version> resolveHighestVersion(@Nonnull ArtifactCoordinates artifact, L /** * Obtain the {@link DependencyScope} from the specified {@code id}. * <p> - * Shortcut for {@code DependencyScope.forId(...)}. + * Shortcut for {@code DependencyScope.forId(...)} with a verification that the given identifier exists. + * + * @param id the identifier of the scope (case-sensitive) + * @return the scope for the given identifier (never null) + * @throws IllegalArgumentException if the given identifier is not a known scope * * @see org.apache.maven.api.DependencyScope#forId(String) */ diff --git a/impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultProject.java b/impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultProject.java index 8cbfeaddc1..0ac8148029 100644 --- a/impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultProject.java +++ b/impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultProject.java @@ -243,7 +243,10 @@ public Type getType() { @Nonnull @Override public DependencyScope getScope() { - String scope = dependency.getScope() != null ? dependency.getScope() : ""; + String scope = dependency.getScope(); + if (scope == null) { + scope = ""; + } return session.requireDependencyScope(scope); } diff --git a/impl/maven-impl/src/main/java/org/apache/maven/impl/AbstractSession.java b/impl/maven-impl/src/main/java/org/apache/maven/impl/AbstractSession.java index 6025a06475..c883ee215c 100644 --- a/impl/maven-impl/src/main/java/org/apache/maven/impl/AbstractSession.java +++ b/impl/maven-impl/src/main/java/org/apache/maven/impl/AbstractSession.java @@ -967,7 +967,11 @@ public ProjectScope requireProjectScope(String id) { @Override public DependencyScope requireDependencyScope(String id) { - return DependencyScope.forId(nonNull(id, "id")); + DependencyScope scope = DependencyScope.forId(nonNull(id, "id")); + if (scope == null) { + throw new IllegalArgumentException("Invalid dependency scope: " + id); + } + return scope; } @Override