From cfb34a93b4902610901f586a31ca7c9f12e9b2b6 Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Sat, 13 Jul 2024 16:53:19 +0200 Subject: [PATCH] [MRESOLVER-586] Avoid copying the type default properties map When we don't have specific properties for a DefaultArtifact, we can just reuse the type default properties map as it's readonly and immutable. The patch contains some additional changes as I renamed the method and parameters to make them more specific so that the method wouldn't be used for things that wouldn't be safe. --- .../aether/artifact/DefaultArtifact.java | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/maven-resolver-api/src/main/java/org/eclipse/aether/artifact/DefaultArtifact.java b/maven-resolver-api/src/main/java/org/eclipse/aether/artifact/DefaultArtifact.java index bf5827334..88cf581fd 100644 --- a/maven-resolver-api/src/main/java/org/eclipse/aether/artifact/DefaultArtifact.java +++ b/maven-resolver-api/src/main/java/org/eclipse/aether/artifact/DefaultArtifact.java @@ -173,21 +173,27 @@ public DefaultArtifact( } this.version = emptify(version); this.file = null; - this.properties = merge(properties, (type != null) ? type.getProperties() : null); + this.properties = mergeArtifactProperties(properties, (type != null) ? type.getProperties() : null); } - private static Map merge(Map dominant, Map recessive) { + private static Map mergeArtifactProperties( + Map artifactProperties, Map typeDefaultProperties) { Map properties; - if ((dominant == null || dominant.isEmpty()) && (recessive == null || recessive.isEmpty())) { - properties = Collections.emptyMap(); + if (artifactProperties == null || artifactProperties.isEmpty()) { + if (typeDefaultProperties == null || typeDefaultProperties.isEmpty()) { + properties = Collections.emptyMap(); + } else { + // type default properties are already unmodifiable + return typeDefaultProperties; + } } else { properties = new HashMap<>(); - if (recessive != null) { - properties.putAll(recessive); + if (typeDefaultProperties != null) { + properties.putAll(typeDefaultProperties); } - if (dominant != null) { - properties.putAll(dominant); + if (artifactProperties != null) { + properties.putAll(artifactProperties); } properties = Collections.unmodifiableMap(properties); }