gnodet commented on code in PR #12540:
URL: https://github.com/apache/maven/pull/12540#discussion_r3651354954
##########
src/mdo/java/ImmutableCollections.java:
##########
@@ -120,6 +120,12 @@ static <K, V> Map<K, V> copy(Map<K, V> map) {
Map.Entry<K, V> entry = map.entrySet().iterator().next();
return singletonMap(entry.getKey(), entry.getValue());
default:
+ // Check if this is already an immutable JDK map (from
Map.of(), Map.copyOf(), etc.)
+ // Those throw UnsupportedOperationException on put() and
don't need copying
+ String className = map.getClass().getName();
+ if
(className.startsWith("java.util.ImmutableCollections$")) {
Review Comment:
Good catch — the JDK internal class names are indeed not guaranteed and
could change between versions. Rather than switching to a mutation-based test
(try/catch on `put()` has its own downsides: performance overhead on the hot
path, reliance on exception semantics), I've simply removed the check entirely.
In practice, JDK `Map.of()`/`Map.copyOf()` maps rarely appear in the model
builder — most maps are either our own `AbstractImmutableMap` (already
short-circuited) or `HashMap` from merging. The wrapping cost for the
occasional JDK immutable map is a single array copy of the entries, which is
negligible.
--
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]