gnodet-bot commented on code in PR #14133:
URL: https://github.com/apache/maven/pull/14133#discussion_r4741369934
##########
impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultProjectManager.java:
##########
@@ -85,9 +88,8 @@ public Optional<Path> getPath(@Nonnull Project project) {
@Override
public Collection<ProducedArtifact> getAttachedArtifacts(@Nonnull Project
project) {
requireNonNull(project, "project" + " cannot be null");
- Collection<ProducedArtifact> attached = map(
- getMavenProject(project).getAttachedArtifacts(),
- a -> getSession(project).getArtifact(ProducedArtifact.class,
RepositoryUtils.toArtifact(a)));
+ Collection<ProducedArtifact> attached =
+ map(getMutableProject(project).attachedArtifacts(), a ->
(ProducedArtifact) a);
Review Comment:
⚠️ **Type invariant not enforced**: `attachedArtifacts()` returns
`List<Artifact>` (the base interface), but the cast to `ProducedArtifact`
assumes all stored items are always `ProducedArtifact`. This was previously
enforced by the type parameter `<A extends org.apache.maven.artifact.Artifact>`
— removing it makes the invariant implicit.
In practice all additions go through `attachArtifact()` (takes
`ProducedArtifact`) or `MavenProject.addAttachedArtifact()` (converts via
`getArtifact(ProducedArtifact.class, ...)`), so the cast is currently safe. But
a future `addAttachedArtifact(Artifact)` call with a non-produced artifact
(e.g. a `DownloadedArtifact` from a dependency) would silently compile and
throw `ClassCastException` at runtime.
Consider an `instanceof` guard or an assertion to make the invariant
explicit:
```suggestion
map(getMutableProject(project).attachedArtifacts(), a -> {
if (!(a instanceof ProducedArtifact pa)) {
throw new IllegalStateException("Attached artifact
is not a ProducedArtifact: " + a);
}
return pa;
});
```
##########
impl/maven-core/src/main/java/org/apache/maven/project/MavenProject.java:
##########
@@ -1087,37 +1088,28 @@ public Map<String, List<String>>
getInjectedProfileIds() {
}
/**
- * Add or replace an artifact. This method is now deprecated. Use the
@{MavenProjectHelper} to attach artifacts to a
- * project. In spite of the 'throws' declaration on this API, this method
has never thrown an exception since Maven
- * 3.0.x. Historically, it logged and ignored a second addition of the
same g/a/v/c/t. Now it replaces the file for
- * the artifact, so that plugins (e.g. shade) can change the pathname of
the file for a particular set of
- * coordinates.
+ * Add or replace an artifact.
*
* @param artifact the artifact to add or replace.
* @deprecated Please use {@link MavenProjectHelper}
* @throws DuplicateArtifactAttachmentException will never happen but
leave it for backward compatibility
*/
public void addAttachedArtifact(Artifact artifact) throws
DuplicateArtifactAttachmentException {
- // if already there we remove it and add again
- int index = attachedArtifacts.indexOf(artifact);
- if (index >= 0) {
+ DefaultProject dp = (DefaultProject) mutableProject.project();
+ org.apache.maven.api.Artifact v4 = dp.getSession()
+ .getArtifact(org.apache.maven.api.ProducedArtifact.class,
RepositoryUtils.toArtifact(artifact));
+ if (mutableProject.addAttachedArtifact(v4)) {
LOGGER.warn("artifact '{}' already attached, replacing previous
instance", artifact);
- attachedArtifacts.set(index, artifact);
- } else {
- attachedArtifacts.add(artifact);
}
}
- /**
- * Returns a read-only list of the attached artifacts to this project.
- *
- * @return the attached artifacts of this project
- */
+ /** Returns a read-only list of the attached artifacts to this project. */
public List<Artifact> getAttachedArtifacts() {
- if (attachedArtifacts == null) {
- attachedArtifacts = new ArrayList<>();
- }
- return Collections.unmodifiableList(attachedArtifacts);
+ // session.toArtifact() resolves the path from ArtifactManager
+ DefaultProject dp = (DefaultProject) mutableProject.project();
Review Comment:
⚠️ **New hidden contract — NPE on unregistered `MavenProject`**:
`mutableProject.project()` throws `NullPointerException` with message `"project
has not been initialized"` if the `MutableProject` has never been registered
with a session. Before this commit, `getAttachedArtifacts()` returned the list
directly — no session required.
Concretely: a cloned project (`project.clone()`) gets a fresh `new
MutableProject()` in `deepCopy()` with no `initialize()` call. Any caller that
invokes `clone.getAttachedArtifacts()` without first passing the clone through
`DefaultSession.getProject()` will NPE, even if the clone has no attached
artifacts (because `mutableProject.project()` is called unconditionally before
the stream).
A simple guard eliminates the most common uninitialized case:
```suggestion
if (mutableProject.attachedArtifacts().isEmpty()) {
return List.of();
}
DefaultProject dp = (DefaultProject) mutableProject.project();
```
##########
impl/maven-core/src/main/java/org/apache/maven/project/MavenProject.java:
##########
@@ -1087,37 +1088,28 @@ public Map<String, List<String>>
getInjectedProfileIds() {
}
/**
- * Add or replace an artifact. This method is now deprecated. Use the
@{MavenProjectHelper} to attach artifacts to a
- * project. In spite of the 'throws' declaration on this API, this method
has never thrown an exception since Maven
- * 3.0.x. Historically, it logged and ignored a second addition of the
same g/a/v/c/t. Now it replaces the file for
- * the artifact, so that plugins (e.g. shade) can change the pathname of
the file for a particular set of
- * coordinates.
+ * Add or replace an artifact.
Review Comment:
💡 **Javadoc regression**: The historical compatibility notes were removed.
Specifically, the notes that (a) this method has never actually thrown
`DuplicateArtifactAttachmentException` since Maven 3.0.x, and (b) the
second-addition behavior changed from "log and ignore" to "replace the file"
were useful for callers of this deprecated API (e.g. shade plugin). The
replacement one-liner loses that context.
Consider keeping at least the behavioral note:
```suggestion
* Add or replace an artifact. This method has never thrown {@link
DuplicateArtifactAttachmentException}
* since Maven 3.0.x. A second attachment with the same coordinates
replaces the previous instance.
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]