public static void removeDuplicates(@NotNull CompositePackagingElement<?> parent) { List<PackagingElement<?>> prevChildren = new ArrayList<PackagingElement<?>>(); List<PackagingElement<?>> toRemove = new ArrayList<PackagingElement<?>>(); for (PackagingElement<?> child : parent.getChildren()) { if (child instanceof CompositePackagingElement<?>) { removeDuplicates((CompositePackagingElement<?>) child); } boolean merged = false; for (PackagingElement<?> prevChild : prevChildren) { if (child.isEqualTo(prevChild)) { if (child instanceof CompositePackagingElement<?>) { for (PackagingElement<?> childElement : ((CompositePackagingElement<?>) child).getChildren()) { ((CompositePackagingElement<?>) prevChild).addOrFindChild(childElement); } } merged = true; break; } } if (merged) { toRemove.add(child); } else { prevChildren.add(child); } } for (PackagingElement<?> child : toRemove) { parent.removeChild(child); } }
@NotNull private static <S> PackagingElement<S> copyElement( @NotNull PackagingElement<S> element, @NotNull Project project) { //noinspection unchecked final PackagingElement<S> copy = (PackagingElement<S>) element.getType().createEmpty(project); copy.loadState(element.getState()); return copy; }
private static <E extends PackagingElement<?>> boolean processElementRecursively( @NotNull PackagingElement<?> element, @Nullable PackagingElementType<E> type, @NotNull PackagingElementProcessor<? super E> processor, @NotNull PackagingElementResolvingContext resolvingContext, final boolean processSubstitutions, ArtifactType artifactType, @NotNull PackagingElementPath path, Set<PackagingElement<?>> processed) { if (!processor.shouldProcess(element) || !processed.add(element)) { return true; } if (type == null || element.getType().equals(type)) { if (!processor.process((E) element, path)) { return false; } } if (element instanceof CompositePackagingElement<?>) { final CompositePackagingElement<?> composite = (CompositePackagingElement<?>) element; return processElementsRecursively( composite.getChildren(), type, processor, resolvingContext, processSubstitutions, artifactType, path.appendComposite(composite), processed); } else if (element instanceof ComplexPackagingElement<?> && processSubstitutions) { final ComplexPackagingElement<?> complexElement = (ComplexPackagingElement<?>) element; if (processor.shouldProcessSubstitution(complexElement)) { final List<? extends PackagingElement<?>> substitution = complexElement.getSubstitution(resolvingContext, artifactType); if (substitution != null) { return processElementsRecursively( substitution, type, processor, resolvingContext, processSubstitutions, artifactType, path.appendComplex(complexElement), processed); } } } return true; }