001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.util.graph.transformer;
020
021import java.util.ArrayList;
022import java.util.Collection;
023import java.util.Collections;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027import java.util.Objects;
028
029import org.eclipse.aether.ConfigurationProperties;
030import org.eclipse.aether.RepositoryException;
031import org.eclipse.aether.RepositorySystemSession;
032import org.eclipse.aether.artifact.Artifact;
033import org.eclipse.aether.collection.DependencyGraphTransformationContext;
034import org.eclipse.aether.graph.DefaultDependencyNode;
035import org.eclipse.aether.graph.Dependency;
036import org.eclipse.aether.graph.DependencyNode;
037import org.eclipse.aether.util.ConfigUtils;
038import org.eclipse.aether.util.artifact.ArtifactIdUtils;
039
040import static java.util.Objects.requireNonNull;
041
042/**
043 * A high-performance dependency graph transformer that resolves version and scope conflicts among dependencies.
044 * This is the recommended conflict resolver implementation that provides O(N) performance characteristics,
045 * significantly improving upon the O(N²) worst-case performance of {@link ClassicConflictResolver}.
046 * <p>
047 * For a given set of conflicting nodes, one node will be chosen as the winner. How losing nodes are handled
048 * depends on the configured verbosity level: they may be removed entirely, have their children removed, or
049 * be left in place with conflict information. The exact rules by which a winning node and its effective scope
050 * are determined are controlled by user-supplied implementations of {@link ConflictResolver.VersionSelector}, {@link ConflictResolver.ScopeSelector},
051 * {@link ConflictResolver.OptionalitySelector} and {@link ConflictResolver.ScopeDeriver}.
052 * <p>
053 * <strong>Performance Characteristics:</strong>
054 * <ul>
055 * <li><strong>Time Complexity:</strong> O(N) where N is the number of dependency nodes</li>
056 * <li><strong>Memory Usage:</strong> Creates a parallel tree structure for conflict-free processing</li>
057 * <li><strong>Scalability:</strong> Excellent performance on large multi-module projects</li>
058 * </ul>
059 * <p>
060 * <strong>Algorithm Overview:</strong>
061 * <ol>
062 * <li><strong>Path Tree Construction:</strong> Builds a cycle-free parallel tree structure from the input
063 *     dependency graph, where each {@code Path} represents a unique route to a dependency node</li>
064 * <li><strong>Conflict Partitioning:</strong> Groups paths by conflict ID (based on groupId:artifactId:classifier:extension coordinates)</li>
065 * <li><strong>Topological Processing:</strong> Processes conflict groups in topologically sorted order</li>
066 * <li><strong>Winner Selection:</strong> Uses provided selectors to choose winners within each conflict group</li>
067 * <li><strong>Graph Transformation:</strong> Applies changes back to the original dependency graph</li>
068 * </ol>
069 * <p>
070 * <strong>Key Differences from {@link ClassicConflictResolver}:</strong>
071 * <ul>
072 * <li><strong>Performance:</strong> O(N) vs O(N²) time complexity</li>
073 * <li><strong>Memory Strategy:</strong> Uses parallel tree structure vs in-place graph modification</li>
074 * <li><strong>Cycle Handling:</strong> Explicitly breaks cycles during tree construction</li>
075 * <li><strong>Processing Order:</strong> Level-by-level from root vs depth-first traversal</li>
076 * </ul>
077 * <p>
078 * <strong>When to Use:</strong>
079 * <ul>
080 * <li>Default choice for all new projects and Maven 4+ installations</li>
081 * <li>Large multi-module projects with many dependencies</li>
082 * <li>Performance-critical build environments</li>
083 * <li>Any scenario where {@link ClassicConflictResolver} shows performance bottlenecks</li>
084 * </ul>
085 * <p>
086 * <strong>Implementation Note:</strong> This conflict resolver builds a cycle-free "parallel" structure based on the
087 * passed-in dependency graph, and applies operations level by level starting from the root. The parallel {@code Path}
088 * tree ensures that cycles in the original graph don't affect the conflict resolution algorithm's performance.
089 *
090 * @see ClassicConflictResolver
091 * @since 2.0.11
092 */
093public final class PathConflictResolver extends ConflictResolver {
094    /**
095     * This implementation of conflict resolver is able to show more precise information regarding cycles in standard
096     * verbose mode. But, to make it really drop-in-replacement, we "tame down" this information. Still, users needing it
097     * may want to enable this for easier cycle detection, but in that case this conflict resolver will provide "extra nodes"
098     * not present on "standard verbosity level" with "classic" conflict resolver, that may lead to IT issues down the
099     * stream. Hence, the default is to provide as much information as much verbose "classic" does.
100     *
101     * @since 2.0.12
102     * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
103     * @configurationType {@link java.lang.Boolean}
104     * @configurationDefaultValue {@link #DEFAULT_SHOW_CYCLES_IN_STANDARD_VERBOSITY}
105     */
106    public static final String CONFIG_PROP_SHOW_CYCLES_IN_STANDARD_VERBOSITY = ConfigurationProperties.PREFIX_AETHER
107            + "conflictResolver." + ConflictResolver.PATH_CONFLICT_RESOLVER + ".showCyclesInStandardVerbosity";
108
109    public static final boolean DEFAULT_SHOW_CYCLES_IN_STANDARD_VERBOSITY = false;
110
111    private final ConflictResolver.VersionSelector versionSelector;
112    private final ConflictResolver.ScopeSelector scopeSelector;
113    private final ConflictResolver.ScopeDeriver scopeDeriver;
114    private final ConflictResolver.OptionalitySelector optionalitySelector;
115
116    /**
117     * Creates a new conflict resolver instance with the specified hooks.
118     *
119     * @param versionSelector the version selector to use, must not be {@code null}
120     * @param scopeSelector the scope selector to use, must not be {@code null}
121     * @param optionalitySelector the optionality selector ot use, must not be {@code null}
122     * @param scopeDeriver the scope deriver to use, must not be {@code null}
123     */
124    public PathConflictResolver(
125            ConflictResolver.VersionSelector versionSelector,
126            ConflictResolver.ScopeSelector scopeSelector,
127            ConflictResolver.OptionalitySelector optionalitySelector,
128            ConflictResolver.ScopeDeriver scopeDeriver) {
129        this.versionSelector = requireNonNull(versionSelector, "version selector cannot be null");
130        this.scopeSelector = requireNonNull(scopeSelector, "scope selector cannot be null");
131        this.optionalitySelector = requireNonNull(optionalitySelector, "optionality selector cannot be null");
132        this.scopeDeriver = requireNonNull(scopeDeriver, "scope deriver cannot be null");
133    }
134
135    @SuppressWarnings("unchecked")
136    @Override
137    public DependencyNode transformGraph(DependencyNode node, DependencyGraphTransformationContext context)
138            throws RepositoryException {
139        requireNonNull(node, "node cannot be null");
140        requireNonNull(context, "context cannot be null");
141        List<String> sortedConflictIds = (List<String>) context.get(TransformationContextKeys.SORTED_CONFLICT_IDS);
142        if (sortedConflictIds == null) {
143            ConflictIdSorter sorter = new ConflictIdSorter();
144            sorter.transformGraph(node, context);
145
146            sortedConflictIds = (List<String>) context.get(TransformationContextKeys.SORTED_CONFLICT_IDS);
147        }
148
149        @SuppressWarnings("unchecked")
150        Map<String, Object> stats = (Map<String, Object>) context.get(TransformationContextKeys.STATS);
151        long time1 = System.nanoTime();
152
153        Map<DependencyNode, String> conflictIds =
154                (Map<DependencyNode, String>) context.get(TransformationContextKeys.CONFLICT_IDS);
155        if (conflictIds == null) {
156            throw new RepositoryException("conflict groups have not been identified");
157        }
158
159        State state = new State(
160                ConflictResolver.getVerbosity(context.getSession()),
161                ConfigUtils.getBoolean(
162                        context.getSession(),
163                        DEFAULT_SHOW_CYCLES_IN_STANDARD_VERBOSITY,
164                        CONFIG_PROP_SHOW_CYCLES_IN_STANDARD_VERBOSITY),
165                versionSelector.getInstance(node, context),
166                scopeSelector.getInstance(node, context),
167                scopeDeriver.getInstance(node, context),
168                optionalitySelector.getInstance(node, context),
169                conflictIds,
170                sortedConflictIds.size(),
171                node);
172
173        // loop over topographically sorted conflictIds
174        int conflictItemCount = 0;
175        for (String conflictId : sortedConflictIds) {
176            // paths in given conflict group to consider; filter out those moved out of scope
177            List<Path> allPaths = state.partitions.get(conflictId);
178            List<Path> activePaths = new ArrayList<>(allPaths.size());
179            List<ConflictItem> items = new ArrayList<>(allPaths.size());
180            for (Path p : allPaths) {
181                if (!p.outOfScope) {
182                    activePaths.add(p);
183                    items.add(new ConflictItem(p));
184                }
185            }
186            // Replace partition entry with filtered list to release references to out-of-scope
187            // paths (and their detached subtrees), allowing GC during resolution
188            state.partitions.put(conflictId, activePaths);
189            if (activePaths.isEmpty()) {
190                // this means that whole group "fall out of scope" (are all on loser branches); skip
191                continue;
192            }
193            conflictItemCount += activePaths.size();
194
195            // create conflict context for given conflictId
196            ConflictContext ctx = new ConflictContext(node, state.conflictIds, items, conflictId);
197
198            // select winner (is done by VersionSelector)
199            state.versionSelector.selectVersion(ctx);
200            if (ctx.winner == null) {
201                throw new RepositoryException("conflict resolver did not select winner among " + items);
202            }
203            // select scope (no side effect between this and above operations)
204            state.scopeSelector.selectScope(ctx);
205            // select optionality (no side effect between this and above operations)
206            state.optionalitySelector.selectOptionality(ctx);
207
208            // we have a winner path
209            Path winnerPath = ctx.winner.path;
210
211            // mark conflictId as resolved with winner; sanity check
212            if (state.resolvedIds.containsKey(conflictId)) {
213                throw new RepositoryException("conflict resolver already have winner for conflictId=" + conflictId
214                        + ": " + state.resolvedIds);
215            }
216            state.resolvedIds.put(conflictId, winnerPath);
217
218            // loop over considered paths and apply selection results
219            for (Path path : activePaths) {
220                // apply selected properties scope/optional to winner (winner carries version; others are losers)
221                if (path == winnerPath) {
222                    path.scope = ctx.scope;
223                    path.optional = ctx.optional;
224                }
225
226                // reset children as inheritance may be affected by this node scope/optionality change
227                if (path.children != null) {
228                    for (Path c : path.children) {
229                        c.pull(0);
230                    }
231                }
232                // derive with new values from this to children only; observe winner flag
233                path.derive(1, path == winnerPath);
234                // push this node full level changes to DN graph
235                path.push(0);
236            }
237        }
238
239        if (stats != null) {
240            long time2 = System.nanoTime();
241            stats.put("ConflictResolver.totalTime", time2 - time1);
242            stats.put("ConflictResolver.conflictItemCount", conflictItemCount);
243        }
244
245        return node;
246    }
247
248    /**
249     * State of conflict resolution processing, to make this component (held in session) re-entrant by multiple threads.
250     */
251    private static class State {
252        /**
253         * Verbosity to be applied, see {@link ConflictResolver.Verbosity}.
254         */
255        private final ConflictResolver.Verbosity verbosity;
256
257        /**
258         * Whether to show nodes entering cycles, for easier identification. If this is enabled, this implementation
259         * of conflict resolver will show more data than classic.
260         */
261        private final boolean showCyclesInStandardVerbosity;
262
263        /**
264         * The {@link ConflictResolver.VersionSelector} to use.
265         */
266        private final ConflictResolver.VersionSelector versionSelector;
267
268        /**
269         * The {@link ConflictResolver.ScopeSelector} to use.
270         */
271        private final ConflictResolver.ScopeSelector scopeSelector;
272
273        /**
274         * The {@link ConflictResolver.ScopeDeriver} to use.
275         */
276        private final ConflictResolver.ScopeDeriver scopeDeriver;
277
278        /**
279         * The {@link ConflictResolver.OptionalitySelector} to use/
280         */
281        private final ConflictResolver.OptionalitySelector optionalitySelector;
282
283        /**
284         * The node to conflictId mapping from {@link ConflictMarker}.
285         */
286        private final Map<DependencyNode, String> conflictIds;
287
288        /**
289         * A mapping from conflictId to paths represented as {@link Path}s that exist for each conflictId. In other
290         * words all paths to each {@link DependencyNode} that are member of same conflictId group.
291         * Uses {@link ArrayList} per partition; out-of-scope paths are marked via {@link Path#outOfScope} flag
292         * and filtered at query time, avoiding the per-entry overhead of LinkedHashSet/HashMap.Node.
293         */
294        private final Map<String, List<Path>> partitions;
295
296        /**
297         * A mapping from conflictIds to winner {@link Path}, hence {@link DependencyNode}  for given conflictId.
298         */
299        private final Map<String, Path> resolvedIds;
300
301        /**
302         * The root {@link Path}.
303         */
304        private final Path root;
305
306        /**
307         * Pooled {@link ScopeContext} instance reused across derive() calls to avoid allocating a new
308         * object per node. Reset via {@link ScopeContext#reset(String, String)} before each use.
309         */
310        private final ScopeContext scopeContext;
311
312        @SuppressWarnings("checkstyle:ParameterNumber")
313        private State(
314                ConflictResolver.Verbosity verbosity,
315                boolean showCyclesInStandardVerbosity,
316                ConflictResolver.VersionSelector versionSelector,
317                ConflictResolver.ScopeSelector scopeSelector,
318                ConflictResolver.ScopeDeriver scopeDeriver,
319                ConflictResolver.OptionalitySelector optionalitySelector,
320                Map<DependencyNode, String> conflictIds,
321                int conflictIdCount,
322                DependencyNode node)
323                throws RepositoryException {
324            this.verbosity = verbosity;
325            this.showCyclesInStandardVerbosity = showCyclesInStandardVerbosity;
326            this.versionSelector = versionSelector;
327            this.scopeSelector = scopeSelector;
328            this.scopeDeriver = scopeDeriver;
329            this.optionalitySelector = optionalitySelector;
330            this.conflictIds = conflictIds;
331            // Right-size maps: conflictIdCount gives exact number of partitions and resolved entries
332            this.partitions = new HashMap<>(conflictIdCount * 4 / 3 + 1);
333            this.resolvedIds = new HashMap<>(conflictIdCount * 4 / 3 + 1);
334            this.scopeContext = new ScopeContext(null, null);
335            this.root = build(node);
336        }
337
338        /**
339         * Consumes the dirty graph and builds internal structures out of {@link Path} instances that is always a
340         * tree. As a side effect, {@link #partitions} are being filled up as well, that combined with topo
341         * sorted conflictIds can serve as a starting to point to walk the graph.
342         */
343        private Path build(DependencyNode node) throws RepositoryException {
344            String nodeConflictId = this.conflictIds.get(node);
345            Path root = new Path(this, node, nodeConflictId, null);
346            gatherCRNodes(root);
347            return root;
348        }
349
350        /**
351         * Iteratively builds {@link Path} graph by observing each node associated {@link DependencyNode}.
352         * Uses an explicit stack instead of recursion to avoid {@link StackOverflowError} on very deep
353         * dependency graphs (reported in large multi-module projects with 13+ levels of recursion).
354         */
355        private void gatherCRNodes(Path root) throws RepositoryException {
356            ArrayList<Path> stack = new ArrayList<>();
357            stack.add(root);
358            while (!stack.isEmpty()) {
359                Path node = stack.remove(stack.size() - 1);
360                List<DependencyNode> children = node.dn.getChildren();
361                if (!children.isEmpty()) {
362                    // add children; we will get back those really added (not causing cycles)
363                    List<Path> added = node.addChildren(children);
364                    // push in reverse order so first child is processed first (DFS order)
365                    for (int i = added.size() - 1; i >= 0; i--) {
366                        stack.add(added.get(i));
367                    }
368                }
369            }
370        }
371    }
372
373    /**
374     * Represents a unique path within the dependency graph from the root to a specific {@link DependencyNode}.
375     * This is the core data structure that enables the O(N) performance of {@link PathConflictResolver}.
376     * <p>
377     * <strong>Key Concepts:</strong>
378     * <ul>
379     * <li><strong>Path Uniqueness:</strong> Each {@code Path} instance represents a distinct route through
380     *     the dependency graph, even if multiple paths lead to the same {@code DependencyNode}</li>
381     * <li><strong>Cycle-Free Structure:</strong> The {@code Path} tree is guaranteed to be acyclic, even
382     *     when the original dependency graph contains cycles</li>
383     * <li><strong>Parallel Structure:</strong> This creates a "clean" tree alongside the original "dirty"
384     *     graph for efficient processing</li>
385     * </ul>
386     * <p>
387     * <strong>Example:</strong> If dependency A appears in the graph via two different routes:
388     * <pre>
389     * Root → B → A (path 1)
390     * Root → C → A (path 2)
391     * </pre>
392     * Two separate {@code Path} instances will be created, both pointing to the same {@code DependencyNode} A,
393     * but representing different paths through the dependency tree.
394     * <p>
395     * <strong>Memory Optimization:</strong> While this creates additional objects, it enables the algorithm
396     * to process conflicts in O(N) time rather than O(N²), making it much more efficient for large graphs.
397     * <p>
398     * <strong>Conflict Resolution:</strong> Paths are grouped by conflict ID (based on groupId:artifactId:classifier:extension coordinates),
399     * and the conflict resolution algorithm can efficiently process each group independently.
400     */
401    private static class Path {
402        // given
403        private final State state;
404        private DependencyNode dn;
405        private final String conflictId;
406        private final Path parent;
407        // derived
408        private final int depth;
409        // Lazy: null for leaf nodes (never populated by addChildren), right-sized for non-leaves.
410        // This avoids allocating an ArrayList + backing array for every leaf node in the tree
411        // (typically 60-70% of all nodes), saving ~40 bytes per leaf.
412        private List<Path> children;
413        // mutated
414        private String scope;
415        private boolean optional;
416        // Flag used instead of removing from partition sets; avoids LinkedHashSet overhead (~48 bytes/entry)
417        private boolean outOfScope;
418
419        private Path(State state, DependencyNode dn, String conflictId, Path parent) {
420            this.state = state;
421            this.dn = dn;
422            this.conflictId = conflictId;
423            this.parent = parent;
424            this.depth = parent != null ? parent.depth + 1 : 0;
425            pull(0);
426
427            this.state
428                    .partitions
429                    .computeIfAbsent(this.conflictId, k -> new ArrayList<>())
430                    .add(this);
431        }
432
433        /**
434         * Checks whether the given conflictId appears on the path from this node to the root.
435         * This replaces the previous approach of copying a HashSet at every child, which was the
436         * dominant source of memory consumption (millions of HashMap.Node objects for large graphs).
437         */
438        private boolean hasConflictIdOnPathToRoot(String targetConflictId) {
439            Path current = this;
440            while (current != null) {
441                if (Objects.equals(current.conflictId, targetConflictId)) {
442                    return true;
443                }
444                current = current.parent;
445            }
446            return false;
447        }
448
449        /**
450         * Pulls (possibly updated) scope and optional values from associated {@link DependencyNode} to this instance,
451         * going down toward children recursively the required count of levels.
452         */
453        private void pull(int levels) {
454            Dependency d = dn.getDependency();
455            if (d != null) {
456                this.scope = d.getScope();
457                this.optional = d.isOptional();
458            } else {
459                this.scope = "";
460                this.optional = false;
461            }
462            int newLevels = levels - 1;
463            if (newLevels >= 0 && this.children != null) {
464                for (Path child : this.children) {
465                    child.pull(newLevels);
466                }
467            }
468        }
469
470        /**
471         * Derives (from this to children direction) values that are "inherited" in tree: scope and optionality in the tree
472         * recursively going down required count of "levels".
473         */
474        private void derive(int levels, boolean winner) throws RepositoryException {
475            if (!winner) {
476                if (this.parent != null) {
477                    if ((dn.getManagedBits() & DependencyNode.MANAGED_SCOPE) == 0) {
478                        state.scopeContext.reset(this.parent.scope, this.scope);
479                        state.scopeDeriver.deriveScope(state.scopeContext);
480                        this.scope = state.scopeContext.derivedScope;
481                    }
482                    if ((dn.getManagedBits() & DependencyNode.MANAGED_OPTIONAL) == 0) {
483                        if (!this.optional && this.parent.optional) {
484                            this.optional = true;
485                        }
486                    }
487                } else {
488                    this.scope = "";
489                    this.optional = false;
490                }
491            }
492            int newLevels = levels - 1;
493            if (newLevels >= 0 && this.children != null) {
494                for (Path child : this.children) {
495                    child.derive(newLevels, false);
496                }
497            }
498        }
499
500        /**
501         * Pushes (applies) the scope and optional and structural changes to associated {@link DependencyNode} modifying
502         * the graph of it. Verbosity is observed, and depending on it the conflicting/loser nodes are removed, or
503         * just their children is removed (with special care for version ranges, see {@link #relatedSiblingsCount(Artifact, Path)}
504         * or by just doing nothing with them only marking losers in full verbosity mode.
505         */
506        private void push(int levels) {
507            if (this.parent != null) {
508                Path winner = this.state.resolvedIds.get(this.conflictId);
509                if (winner == null) {
510                    throw new IllegalStateException(
511                            "Winner selection did not happen for conflictId=" + this.conflictId);
512                }
513                if (!Objects.equals(winner.conflictId, this.conflictId)) {
514                    throw new IllegalStateException(
515                            "ConflictId mix-up: this=" + this.conflictId + " winner=" + winner.conflictId);
516                }
517
518                if (winner == this) {
519                    // copy onto dn; if applicable
520                    if (this.dn.getDependency() != null) {
521                        this.dn.setData(
522                                ConflictResolver.NODE_DATA_ORIGINAL_SCOPE,
523                                this.dn.getDependency().getScope());
524                        this.dn.setData(
525                                ConflictResolver.NODE_DATA_ORIGINAL_OPTIONALITY,
526                                this.dn.getDependency().getOptional());
527                        this.dn.setScope(this.scope);
528                        this.dn.setOptional(this.optional);
529                    }
530                } else {
531                    // loser; move out of scope
532                    moveOutOfScope();
533                    boolean markLoser = false;
534                    switch (state.verbosity) {
535                        case NONE:
536                            // remove loser dn
537                            if (this.parent.children != null) {
538                                this.parent.children.remove(this);
539                            }
540                            this.parent.dn.setChildren(new ArrayList<>(this.parent.dn.getChildren()));
541                            this.parent.dn.getChildren().remove(this.dn);
542                            this.children = null;
543                            break;
544                        case STANDARD:
545                            // is redundant if:
546                            // - is not same as winner, and has related siblings (version range)
547                            // - same instance of DN is direct dependency on path leading here
548                            boolean isRedundant =
549                                    (!ArtifactIdUtils.equalsId(this.dn.getArtifact(), winner.dn.getArtifact())
550                                            && relatedSiblingsCount(this.dn.getArtifact(), this.parent) > 1);
551                            if (!this.state.showCyclesInStandardVerbosity) {
552                                isRedundant = isRedundant
553                                        || this.parent.isDirectDependencyOnPathToRoot(this.dn.getArtifact());
554                            }
555                            if (isRedundant) {
556                                // is redundant dn; remove dn
557                                if (this.parent.children != null) {
558                                    this.parent.children.remove(this);
559                                }
560                                this.parent.dn.setChildren(new ArrayList<>(this.parent.dn.getChildren()));
561                                this.parent.dn.getChildren().remove(this.dn);
562                                this.children = null;
563                            } else {
564                                // copy loser dn; without children
565                                DependencyNode dnCopy = new DefaultDependencyNode(this.dn);
566                                dnCopy.setChildren(Collections.emptyList());
567
568                                // swap it out in DN graph; in case of cycles this may happen more than once
569                                int idx = this.parent.dn.getChildren().indexOf(this.dn);
570                                if (idx >= 0) {
571                                    this.parent.dn.getChildren().set(idx, dnCopy);
572                                }
573                                this.dn = dnCopy;
574
575                                this.children = null;
576                                markLoser = true;
577                            }
578                            break;
579                        case FULL:
580                            // copy loser dn; with children
581                            DependencyNode dnCopy = new DefaultDependencyNode(this.dn);
582                            dnCopy.setChildren(new ArrayList<>(this.dn.getChildren()));
583
584                            // swap it out in DN graph; in case of cycles this may happen more than once
585                            int idx = this.parent.dn.getChildren().indexOf(this.dn);
586                            if (idx >= 0) {
587                                this.parent.dn.getChildren().set(idx, dnCopy);
588                            }
589                            this.dn = dnCopy;
590
591                            markLoser = true;
592                            break;
593                        default:
594                            throw new IllegalArgumentException("Unknown " + state.verbosity);
595                    }
596                    if (markLoser) {
597                        this.dn.setData(ConflictResolver.NODE_DATA_WINNER, winner.dn);
598                        this.dn.setData(
599                                ConflictResolver.NODE_DATA_ORIGINAL_SCOPE,
600                                this.dn.getDependency().getScope());
601                        this.dn.setData(
602                                ConflictResolver.NODE_DATA_ORIGINAL_OPTIONALITY,
603                                this.dn.getDependency().getOptional());
604                        this.dn.setScope(this.scope);
605                        this.dn.setOptional(this.optional);
606                    }
607                }
608            }
609
610            // Note: push() is always called with levels=0, so newLevels would be -1
611            // and the recursive block would never execute. The recursive structure is
612            // intentionally not present; all push() calls happen from the main loop.
613        }
614
615        /**
616         * Returns {@code true} if given artifact is a direct dependency on the path leading from this toward root.
617         * A "direct dependency" is one at depth 1 (immediate child of root). Rather than recursing through every
618         * ancestor, this walks directly to the depth-1 node and performs one allocation-free comparison.
619         * <p>
620         * Note: this check and use of this method is ONLY present to make this conflict resolver produce SAME output
621         * as {@link ClassicConflictResolver} does, but IMHO this rule here is very arbitrary, moreover, in "standard"
622         * (where it is only used) verbosity it in facts HIDES the trace of possible cycles.
623         *
624         * @see #CONFIG_PROP_SHOW_CYCLES_IN_STANDARD_VERBOSITY
625         */
626        private boolean isDirectDependencyOnPathToRoot(Artifact artifact) {
627            // Walk up to depth-1 ancestor (direct dependency of root) instead of recursing every level
628            Path current = this;
629            while (current != null && current.depth > 1) {
630                current = current.parent;
631            }
632            return current != null
633                    && current.depth == 1
634                    && ArtifactIdUtils.equalsVersionlessId(current.dn.getArtifact(), artifact);
635        }
636
637        /**
638         * Counts "relatives" (GACE equal) artifacts under same parent; this is for cleaning up redundant nodes in
639         * case of version ranges, where same GACE is resolved into multiple GACEV as range is resolved. In {@link ConflictResolver.Verbosity#STANDARD}
640         * verbosity mode we remove "redundant" nodes (of a range) leaving only "winner equal" loser, that have same GACEV as winner.
641         */
642        private int relatedSiblingsCount(Artifact artifact, Path parent) {
643            if (parent.children == null) {
644                return 0;
645            }
646            String groupId = artifact.getGroupId();
647            String artifactId = artifact.getArtifactId();
648            int count = 0;
649            for (Path n : parent.children) {
650                Artifact a = n.dn.getArtifact();
651                if (Objects.equals(groupId, a.getGroupId()) && Objects.equals(artifactId, a.getArtifactId())) {
652                    count++;
653                }
654            }
655            return count;
656        }
657
658        /**
659         * Marks this and all child {@link Path} nodes as out of scope; essentially marks whole subtree
660         * from "this and below" as loser, to not be considered in subsequent winner selections.
661         * Uses a boolean flag instead of removing from partition collections, avoiding the per-entry
662         * overhead of LinkedHashSet (~48 bytes/entry). Out-of-scope paths are filtered at query time.
663         * <p>
664         * Uses an explicit stack instead of recursion to avoid {@link StackOverflowError} on deep
665         * dependency graphs, consistent with the iterative approach in
666         * {@link State#gatherCRNodes(Path)}. Also nulls children references on out-of-scope nodes
667         * to allow GC of detached subtrees during resolution.
668         */
669        private void moveOutOfScope() {
670            ArrayList<Path> stack = new ArrayList<>();
671            stack.add(this);
672            while (!stack.isEmpty()) {
673                Path node = stack.remove(stack.size() - 1);
674                node.outOfScope = true;
675                if (node.children != null) {
676                    stack.addAll(node.children);
677                    node.children = null;
678                }
679            }
680        }
681
682        /**
683         * Adds node children: this method should be "batch" used, as all (potential) children should be added at once.
684         * Method will return really added {@link Path} instances, as this class avoids cycles. Those forming a cycle
685         * are not recursed (not returned in list), keeping {@link Path} cycle free.
686         * <p>
687         * Cycle detection is performed by walking up the parent chain via
688         * {@link #hasConflictIdOnPathToRoot(String)} instead of maintaining a per-node set, trading O(depth) per
689         * check for dramatically reduced memory allocation on large dependency graphs.
690         * This implies that this conflict resolver, by its nature "redoes" the
691         * {@link TransformationContextKeys#CYCLIC_CONFLICT_IDS} calculated by {@link ConflictIdSorter}.
692         */
693        private List<Path> addChildren(List<DependencyNode> children) throws RepositoryException {
694            // Right-size the children list to avoid ArrayList default capacity waste
695            this.children = new ArrayList<>(children.size());
696            ArrayList<Path> added = new ArrayList<>(children.size());
697            for (DependencyNode child : children) {
698                String childConflictId = this.state.conflictIds.get(child);
699                boolean cycle = hasConflictIdOnPathToRoot(childConflictId);
700                Path c = new Path(this.state, child, childConflictId, this);
701                this.children.add(c);
702                c.derive(0, false);
703                if (!cycle) {
704                    added.add(c);
705                }
706            }
707            return added;
708        }
709
710        /**
711         * Dump for debug.
712         */
713        private void dump(String padding) {
714            System.out.println(padding + this.dn + ": " + this.scope + "/" + this.optional);
715            if (this.children != null) {
716                for (Path child : this.children) {
717                    child.dump(padding + "  ");
718                }
719            }
720        }
721
722        /**
723         * For easier debug.
724         */
725        @Override
726        public String toString() {
727            return this.dn.toString();
728        }
729    }
730
731    /**
732     * A context used to hold information that is relevant for deriving the scope of a child dependency.
733     *
734     * @see ConflictResolver.ScopeDeriver
735     * @noinstantiate This class is not intended to be instantiated by clients in production code, the constructor may
736     *                change without notice and only exists to enable unit testing
737     */
738    private static final class ScopeContext extends ConflictResolver.ScopeContext {
739        private String parentScope;
740        private String childScope;
741        private String derivedScope;
742
743        /**
744         * Creates a new scope context with the specified properties.
745         *
746         * @param parentScope the scope of the parent dependency, may be {@code null}
747         * @param childScope the scope of the child dependency, may be {@code null}
748         * @noreference This class is not intended to be instantiated by clients in production code, the constructor may
749         *              change without notice and only exists to enable unit testing
750         */
751        private ScopeContext(String parentScope, String childScope) {
752            this.parentScope = (parentScope != null) ? parentScope : "";
753            this.derivedScope = (childScope != null) ? childScope : "";
754            this.childScope = (childScope != null) ? childScope : "";
755        }
756
757        /**
758         * Resets this context for reuse, avoiding allocation of a new instance per derive() call.
759         */
760        private void reset(String parentScope, String childScope) {
761            this.parentScope = (parentScope != null) ? parentScope : "";
762            this.derivedScope = (childScope != null) ? childScope : "";
763            this.childScope = (childScope != null) ? childScope : "";
764        }
765
766        /**
767         * Gets the scope of the parent dependency. This is usually the scope that was derived by earlier invocations of
768         * the scope deriver.
769         *
770         * @return the scope of the parent dependency, never {@code null}
771         */
772        public String getParentScope() {
773            return parentScope;
774        }
775
776        /**
777         * Gets the original scope of the child dependency. This is the scope that was declared in the artifact
778         * descriptor of the parent dependency.
779         *
780         * @return the original scope of the child dependency, never {@code null}
781         */
782        public String getChildScope() {
783            return childScope;
784        }
785
786        /**
787         * Gets the derived scope of the child dependency. This is initially equal to {@link #getChildScope()} until the
788         * scope deriver makes changes.
789         *
790         * @return the derived scope of the child dependency, never {@code null}
791         */
792        public String getDerivedScope() {
793            return derivedScope;
794        }
795
796        /**
797         * Sets the derived scope of the child dependency.
798         *
799         * @param derivedScope the derived scope of the dependency, may be {@code null}
800         */
801        public void setDerivedScope(String derivedScope) {
802            this.derivedScope = (derivedScope != null) ? derivedScope : "";
803        }
804    }
805
806    /**
807     * A conflicting dependency.
808     *
809     * @noinstantiate This class is not intended to be instantiated by clients in production code, the constructor may
810     *                change without notice and only exists to enable unit testing
811     */
812    private static final class ConflictItem extends ConflictResolver.ConflictItem {
813        private final Path path;
814        private final List<DependencyNode> parent;
815        private final Artifact artifact;
816        private final DependencyNode node;
817        private final int depth;
818        private final String scope;
819        private final int optionalities;
820
821        private ConflictItem(Path path) {
822            this.path = path;
823            if (path.parent != null) {
824                DependencyNode parent = path.parent.dn;
825                this.parent = parent.getChildren();
826                this.artifact = parent.getArtifact();
827            } else {
828                this.parent = null;
829                this.artifact = null;
830            }
831            this.node = path.dn;
832            this.depth = path.depth;
833            this.scope = path.scope;
834            this.optionalities = path.optional ? OPTIONAL_TRUE : OPTIONAL_FALSE;
835        }
836
837        /**
838         * Determines whether the specified conflict item is a sibling of this item.
839         *
840         * @param item the other conflict item, must not be {@code null}
841         * @return {@code true} if the given item has the same parent as this item, {@code false} otherwise
842         */
843        @Override
844        public boolean isSibling(ConflictResolver.ConflictItem item) {
845            return parent == ((ConflictItem) item).parent;
846        }
847
848        /**
849         * Gets the dependency node involved in the conflict.
850         *
851         * @return the involved dependency node, never {@code null}
852         */
853        @Override
854        public DependencyNode getNode() {
855            return node;
856        }
857
858        /**
859         * Gets the dependency involved in the conflict, short for {@code getNode.getDependency()}.
860         *
861         * @return the involved dependency, never {@code null}
862         */
863        @Override
864        public Dependency getDependency() {
865            return node.getDependency();
866        }
867
868        /**
869         * Gets the zero-based depth at which the conflicting node occurs in the graph. As such, the depth denotes the
870         * number of parent nodes. If actually multiple paths lead to the node, the return value denotes the smallest
871         * possible depth.
872         *
873         * @return the zero-based depth of the node in the graph
874         */
875        @Override
876        public int getDepth() {
877            return depth;
878        }
879
880        /**
881         * Gets the derived scopes of the dependency. In general, the same dependency node could be reached via
882         * different paths and each path might result in a different derived scope.
883         *
884         * @return the (read-only) set of derived scopes of the dependency, never {@code null}
885         * @see ConflictResolver.ScopeDeriver
886         */
887        @Override
888        public Collection<String> getScopes() {
889            return Collections.singleton(scope);
890        }
891
892        /**
893         * Gets the derived optionalities of the dependency. In general, the same dependency node could be reached via
894         * different paths and each path might result in a different derived optionality.
895         *
896         * @return a bit field consisting of {@link PathConflictResolver.ConflictItem#OPTIONAL_FALSE} and/or
897         *         {@link PathConflictResolver.ConflictItem#OPTIONAL_TRUE} indicating the derived optionalities the
898         *         dependency was encountered with
899         */
900        @Override
901        public int getOptionalities() {
902            return optionalities;
903        }
904
905        @Override
906        public String toString() {
907            return node + " @ " + depth + " < " + artifact;
908        }
909    }
910
911    /**
912     * A context used to hold information that is relevant for resolving version and scope conflicts.
913     *
914     * @see ConflictResolver.VersionSelector
915     * @see ConflictResolver.ScopeSelector
916     * @noinstantiate This class is not intended to be instantiated by clients in production code, the constructor may
917     *                change without notice and only exists to enable unit testing
918     */
919    private static final class ConflictContext extends ConflictResolver.ConflictContext {
920        private final DependencyNode root;
921        private final Map<DependencyNode, String> conflictIds;
922        private final Collection<ConflictResolver.ConflictItem> items;
923        private final String conflictId;
924
925        // elected properties
926        private ConflictItem winner;
927        private String scope;
928        private Boolean optional;
929
930        private ConflictContext(
931                DependencyNode root,
932                Map<DependencyNode, String> conflictIds,
933                Collection<ConflictItem> items,
934                String conflictId) {
935            this.root = root;
936            this.conflictIds = conflictIds;
937            this.items = Collections.unmodifiableCollection(items);
938            this.conflictId = conflictId;
939        }
940
941        /**
942         * Gets the root node of the dependency graph being transformed.
943         *
944         * @return the root node of the dependency graph, never {@code null}
945         */
946        @Override
947        public DependencyNode getRoot() {
948            return root;
949        }
950
951        /**
952         * Determines whether the specified dependency node belongs to this conflict context.
953         *
954         * @param node the dependency node to check, must not be {@code null}
955         * @return {@code true} if the given node belongs to this conflict context, {@code false} otherwise
956         */
957        @Override
958        public boolean isIncluded(DependencyNode node) {
959            return conflictId.equals(conflictIds.get(node));
960        }
961
962        /**
963         * Gets the collection of conflict items in this context.
964         *
965         * @return the (read-only) collection of conflict items in this context, never {@code null}
966         */
967        @Override
968        public Collection<ConflictResolver.ConflictItem> getItems() {
969            return items;
970        }
971
972        /**
973         * Gets the conflict item which has been selected as the winner among the conflicting dependencies.
974         *
975         * @return the winning conflict item or {@code null} if not set yet
976         */
977        @Override
978        public ConflictResolver.ConflictItem getWinner() {
979            return winner;
980        }
981
982        /**
983         * Sets the conflict item which has been selected as the winner among the conflicting dependencies.
984         *
985         * @param winner the winning conflict item, may be {@code null}
986         */
987        @Override
988        public void setWinner(ConflictResolver.ConflictItem winner) {
989            this.winner = (ConflictItem) winner;
990        }
991
992        /**
993         * Gets the effective scope of the winning dependency.
994         *
995         * @return the effective scope of the winning dependency or {@code null} if none
996         */
997        @Override
998        public String getScope() {
999            return scope;
1000        }
1001
1002        /**
1003         * Sets the effective scope of the winning dependency.
1004         *
1005         * @param scope the effective scope, may be {@code null}
1006         */
1007        @Override
1008        public void setScope(String scope) {
1009            this.scope = scope;
1010        }
1011
1012        /**
1013         * Gets the effective optional flag of the winning dependency.
1014         *
1015         * @return the effective optional flag or {@code null} if none
1016         */
1017        @Override
1018        public Boolean getOptional() {
1019            return optional;
1020        }
1021
1022        /**
1023         * Sets the effective optional flag of the winning dependency.
1024         *
1025         * @param optional the effective optional flag, may be {@code null}
1026         */
1027        @Override
1028        public void setOptional(Boolean optional) {
1029            this.optional = optional;
1030        }
1031
1032        @Override
1033        public String toString() {
1034            return winner + " @ " + scope + " < " + items;
1035        }
1036    }
1037}