Computable<Collection<I>> (final @NotNull Collection<? extends DataNode<E>> toImport, @NotNull final ProjectData projectData, @NotNull final Project project, @NotNull final IdeModifiableModelsProvider modelsProvider) { return () -> { MultiMap<String /*module name*/, String /*dep name*/> byModuleName = MultiMap.create(); for (DataNode<E> node : toImport) { final E data = node.getData(); Module ownerModule = modelsProvider.findIdeModule(data.getOwnerModule()); if (ownerModule == null && modelsProvider.getUnloadedModuleDescription(data.getOwnerModule()) != null) { continue; } assert ownerModule != null; String depName; if(data instanceof ModuleDependencyData) { Module targetModule = modelsProvider.findIdeModule(((ModuleDependencyData)data).getTarget()); if (targetModule == null && modelsProvider.getUnloadedModuleDescription(((ModuleDependencyData)data).getTarget()) != null) { continue; } assert targetModule != null; depName = targetModule.getName(); } else { depName = getInternalName(data); } byModuleName.putValue(ownerModule.getName(), depName); } final ModifiableModuleModel modifiableModuleModel = modelsProvider.getModifiableModuleModel(); List<I> orphanEntries = new SmartList<>(); for (Module module : modelsProvider.getModules(projectData)) { for (OrderEntry entry : modelsProvider.getOrderEntries(module)) { // do not remove recently created library w/o name if (entry instanceof LibraryOrderEntry && ((LibraryOrderEntry)entry).getLibraryName() == null && ((LibraryOrderEntry)entry).getRootUrls(OrderRootType.CLASSES).length == 0) { continue; } if (getOrderEntryType().isInstance(entry)) { final String moduleName = modifiableModuleModel.getActualName(entry.getOwnerModule()); //noinspection unchecked if (!byModuleName.get(moduleName).contains(getOrderEntryName(modelsProvider, (I)entry))) { //noinspection unchecked orphanEntries.add((I)entry); } } } } return orphanEntries; }; }