Skip to content

Commit 4554700

Browse files
authored
[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
1 parent 3528314 commit 4554700

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

api/maven-api-core/src/main/java/org/apache/maven/api/DependencyScope.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ public enum DependencyScope {
9999
private static final Map<String, DependencyScope> IDS = Collections.unmodifiableMap(
100100
Stream.of(DependencyScope.values()).collect(Collectors.toMap(s -> s.id, s -> s)));
101101

102+
/**
103+
* {@return the dependency scope for the given identifier, or {@code null} if none}.
104+
* The identifiers are usually in lower cases with {@code '-'} instead of {@code '_'}
105+
* as word separator.
106+
*
107+
* @param id the identifier of the scope (case-sensitive)
108+
*/
102109
public static DependencyScope forId(String id) {
103110
return IDS.get(id);
104111
}

api/maven-api-core/src/main/java/org/apache/maven/api/Session.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,11 @@ Optional<Version> resolveHighestVersion(@Nonnull ArtifactCoordinates artifact, L
833833
/**
834834
* Obtain the {@link DependencyScope} from the specified {@code id}.
835835
* <p>
836-
* Shortcut for {@code DependencyScope.forId(...)}.
836+
* Shortcut for {@code DependencyScope.forId(...)} with a verification that the given identifier exists.
837+
*
838+
* @param id the identifier of the scope (case-sensitive)
839+
* @return the scope for the given identifier (never null)
840+
* @throws IllegalArgumentException if the given identifier is not a known scope
837841
*
838842
* @see org.apache.maven.api.DependencyScope#forId(String)
839843
*/

impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultProject.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,10 @@ public Type getType() {
243243
@Nonnull
244244
@Override
245245
public DependencyScope getScope() {
246-
String scope = dependency.getScope() != null ? dependency.getScope() : "";
246+
String scope = dependency.getScope();
247+
if (scope == null) {
248+
scope = "";
249+
}
247250
return session.requireDependencyScope(scope);
248251
}
249252

impl/maven-impl/src/main/java/org/apache/maven/impl/AbstractSession.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,11 @@ public ProjectScope requireProjectScope(String id) {
967967

968968
@Override
969969
public DependencyScope requireDependencyScope(String id) {
970-
return DependencyScope.forId(nonNull(id, "id"));
970+
DependencyScope scope = DependencyScope.forId(nonNull(id, "id"));
971+
if (scope == null) {
972+
throw new IllegalArgumentException("Invalid dependency scope: " + id);
973+
}
974+
return scope;
971975
}
972976

973977
@Override

0 commit comments

Comments
 (0)