public OrderEntry[] getOrderEntries() { if (myModifiableRootModel == null) { // do not clone all model if not necessary return ModuleRootManager.getInstance(getModule()).getOrderEntries(); } else { return myModifiableRootModel.getOrderEntries(); } }
@Override @Nullable protected ClasspathTableItem<?> createTableItem(final Library item) { // clear invalid order entry corresponding to added library if any final ModifiableRootModel rootModel = myClasspathPanel.getRootModel(); final OrderEntry[] orderEntries = rootModel.getOrderEntries(); for (OrderEntry orderEntry : orderEntries) { if (orderEntry instanceof LibraryOrderEntry) { final LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry) orderEntry; if (item.equals(libraryOrderEntry.getLibrary())) { return ClasspathTableItem.createItem(libraryOrderEntry, myContext); } String name = item.getName(); if (name != null && name.equals(libraryOrderEntry.getLibraryName())) { if (orderEntry.isValid()) { Messages.showErrorDialog( ProjectBundle.message("classpath.message.library.already.added", item.getName()), ProjectBundle.message("classpath.title.adding.dependency")); return null; } else { rootModel.removeOrderEntry(orderEntry); } } } } final LibraryOrderEntry orderEntry = rootModel.addLibraryEntry(item); DependencyScope defaultScope = getDefaultScope(item); if (defaultScope != null) { orderEntry.setScope(defaultScope); } return ClasspathTableItem.createItem(orderEntry, myContext); }
public void unConfigureModule(@NotNull ModifiableRootModel model) { for (OrderEntry orderEntry : model.getOrderEntries()) { if (orderEntry instanceof LibraryOrderEntry) { LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry) orderEntry; Library library = libraryOrderEntry.getLibrary(); if (library != null) { String libraryName = library.getName(); if (libraryName != null && libraryName.equals(this.libraryName)) { // Dispose attached roots Library.ModifiableModel modifiableModel = library.getModifiableModel(); for (String rootUrl : library.getRootProvider().getUrls(OrderRootType.CLASSES)) { modifiableModel.removeRoot(rootUrl, OrderRootType.CLASSES); } modifiableModel.commit(); model.getModuleLibraryTable().removeLibrary(library); break; } } } } }
private static void addDependencyOnDartPackagesLibrary( @NotNull final Module module, @NotNull final Library library) { final ModifiableRootModel modifiableModel = ModuleRootManager.getInstance(module).getModifiableModel(); try { for (final OrderEntry orderEntry : modifiableModel.getOrderEntries()) { if (orderEntry instanceof LibraryOrderEntry && LibraryTablesRegistrar.PROJECT_LEVEL.equals( ((LibraryOrderEntry) orderEntry).getLibraryLevel()) && DartPackagesLibraryType.DART_PACKAGES_LIBRARY_NAME.equals( ((LibraryOrderEntry) orderEntry).getLibraryName())) { return; // dependency already exists } } modifiableModel.addLibraryEntry(library); ApplicationManager.getApplication() .runWriteAction( new Runnable() { @Override public void run() { modifiableModel.commit(); } }); } finally { if (!modifiableModel.isDisposed()) { modifiableModel.dispose(); } } }
private void syncExistingAndRemoveObsolete( @NotNull IdeModifiableModelsProvider modelsProvider, @NotNull Map<Set<String>, LibraryDependencyData> moduleLibrariesToImport, @NotNull Map<String, LibraryDependencyData> projectLibrariesToImport, @NotNull Set<LibraryDependencyData> toImport, @NotNull Map<OrderEntry, OrderAware> orderEntryDataMap, @NotNull ModifiableRootModel moduleRootModel, boolean hasUnresolvedLibraries) { for (OrderEntry entry : moduleRootModel.getOrderEntries()) { if (entry instanceof ModuleLibraryOrderEntryImpl) { ModuleLibraryOrderEntryImpl moduleLibraryOrderEntry = (ModuleLibraryOrderEntryImpl) entry; Library library = moduleLibraryOrderEntry.getLibrary(); if (library == null) { LOG.warn( "Skipping module-level library entry because it doesn't have backing Library object. Entry: " + entry); continue; } final VirtualFile[] libraryFiles = library.getFiles(OrderRootType.CLASSES); final Set<String> moduleLibraryKey = ContainerUtilRt.newHashSet(libraryFiles.length); for (VirtualFile file : libraryFiles) { moduleLibraryKey.add( ExternalSystemApiUtil.getLocalFileSystemPath(file) + moduleLibraryOrderEntry.getScope().name()); } LibraryDependencyData existing = moduleLibrariesToImport.remove(moduleLibraryKey); if (existing == null || !StringUtil.equals(existing.getInternalName(), library.getName())) { moduleRootModel.removeOrderEntry(entry); } else { orderEntryDataMap.put(entry, existing); syncExistingLibraryDependency( modelsProvider, existing, library, moduleRootModel, moduleLibraryOrderEntry.getOwnerModule()); toImport.remove(existing); } } else if (entry instanceof LibraryOrderEntry) { LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry) entry; String libraryName = libraryOrderEntry.getLibraryName(); LibraryDependencyData existing = projectLibrariesToImport.remove(libraryName + libraryOrderEntry.getScope().name()); if (existing != null) { toImport.remove(existing); orderEntryDataMap.put(entry, existing); } else if (!hasUnresolvedLibraries) { // There is a possible case that a project has been successfully imported from external // model and after // that network/repo goes down. We don't want to drop existing binary mappings then. moduleRootModel.removeOrderEntry(entry); } } } }
private static void setLibraryEntryExported( ModifiableRootModel rootModel, boolean exported, Library library) { for (OrderEntry orderEntry : rootModel.getOrderEntries()) { if (orderEntry instanceof LibraryOrderEntry && ((LibraryOrderEntry) orderEntry).isModuleLevel() && Comparing.equal(((LibraryOrderEntry) orderEntry).getLibrary(), library)) { ((LibraryOrderEntry) orderEntry).setExported(exported); break; } } }
private boolean askAndRemoveDuplicatedLibraryEntry( @NotNull ModifiableRootModel rootModel, @NotNull CustomLibraryDescription description) { List<OrderEntry> existingEntries = new ArrayList<OrderEntry>(); final LibrariesContainer container = myModel.getLibrariesContainer(); for (OrderEntry entry : rootModel.getOrderEntries()) { if (!(entry instanceof LibraryOrderEntry)) continue; final Library library = ((LibraryOrderEntry) entry).getLibrary(); if (library == null) continue; if (LibraryPresentationManager.getInstance() .isLibraryOfKind(library, container, description.getSuitableLibraryKinds())) { existingEntries.add(entry); } } if (!existingEntries.isEmpty()) { String message; if (existingEntries.size() > 1) { message = "There are already " + existingEntries.size() + " " + myFrameworkType.getPresentableName() + " libraries.\n Do you want to replace they?"; } else { final String name = existingEntries.get(0).getPresentableName(); message = "There is already a " + myFrameworkType.getPresentableName() + " library '" + name + "'.\n Do you want to replace it?"; } final int result = Messages.showYesNoCancelDialog( rootModel.getProject(), message, "Library Already Exists", "&Replace", "&Add", "&Cancel", null); if (result == 0) { for (OrderEntry entry : existingEntries) { rootModel.removeOrderEntry(entry); } } else if (result != 1) { return false; } } return true; }
private static void removeGenModule(@NotNull final Module libModule) { final String genModuleName = getGenModuleName(libModule); final ModuleManager moduleManager = ModuleManager.getInstance(libModule.getProject()); final ModifiableRootModel model = ModuleRootManager.getInstance(libModule).getModifiableModel(); for (OrderEntry entry : model.getOrderEntries()) { if (entry instanceof ModuleOrderEntry && genModuleName.equals(((ModuleOrderEntry) entry).getModuleName())) { model.removeOrderEntry(entry); } } ApplicationManager.getApplication() .runWriteAction( new Runnable() { @Override public void run() { model.commit(); } }); final Module genModule = moduleManager.findModuleByName(genModuleName); if (genModule == null) { return; } moduleManager.disposeModule(genModule); final VirtualFile moduleFile = genModule.getModuleFile(); if (moduleFile != null) { ApplicationManager.getApplication() .invokeLater( new Runnable() { @Override public void run() { ApplicationManager.getApplication() .runWriteAction( new Runnable() { @Override public void run() { try { moduleFile.delete(libModule.getProject()); } catch (IOException e) { LOG.error(e); } } }); } }); } }
private void initOrderEntries() { for (OrderEntry e : myRootModel.getOrderEntries()) { if (e instanceof ModuleSourceOrderEntry || e instanceof JdkOrderEntry) continue; if (e instanceof LibraryOrderEntry) { if (!isMavenLibrary(((LibraryOrderEntry) e).getLibrary())) continue; } if (e instanceof ModuleOrderEntry) { Module m = ((ModuleOrderEntry) e).getModule(); if (m != null && !MavenProjectsManager.getInstance(myRootModel.getProject()).isMavenizedModule(m)) continue; } myRootModel.removeOrderEntry(e); } }
private static int rearrangeOrderEntryOfType( ModifiableRootModel rootModel, Class<? extends OrderEntry> orderEntryClass) { OrderEntry[] orderEntries = rootModel.getOrderEntries(); int moduleSourcesIdx = 0; for (OrderEntry orderEntry : orderEntries) { if (orderEntryClass.isAssignableFrom(orderEntry.getClass())) { break; } moduleSourcesIdx++; } orderEntries = ArrayUtil.append(orderEntries, orderEntries[moduleSourcesIdx]); orderEntries = ArrayUtil.remove(orderEntries, moduleSourcesIdx); rootModel.rearrangeOrderEntries(orderEntries); return orderEntries.length - 1; }
public void readClasspath( ModifiableRootModel model, final Collection<String> unknownLibraries, Collection<String> unknownJdks, final Set<String> usedVariables, Set<String> refsToModules, final String testPattern, Element classpathElement) throws IOException, ConversionException { for (OrderEntry orderEntry : model.getOrderEntries()) { if (!(orderEntry instanceof ModuleSourceOrderEntry)) { model.removeOrderEntry(orderEntry); } } int idx = 0; for (Object o : classpathElement.getChildren(EclipseXml.CLASSPATHENTRY_TAG)) { try { readClasspathEntry( model, unknownLibraries, unknownJdks, usedVariables, refsToModules, testPattern, (Element) o, idx++, EclipseModuleManagerImpl.getInstance(model.getModule()), ((BasePathMacroManager) PathMacroManager.getInstance(model.getModule())) .getExpandMacroMap()); } catch (ConversionException e) { ErrorLog.rethrow(ErrorLog.Level.Warning, null, EclipseXml.CLASSPATH_FILE, e); } } if (!model.isSdkInherited() && model.getSdkName() == null) { EclipseModuleManagerImpl.getInstance(model.getModule()).setForceConfigureJDK(); model.inheritSdk(); } }
@Nullable private static LibraryOrderEntry findLibraryOrderEntry( @NotNull ModifiableRootModel moduleRootModel, @NotNull Library library, @NotNull DependencyScope scope) { LibraryOrderEntry candidate = null; for (OrderEntry orderEntry : moduleRootModel.getOrderEntries()) { if (orderEntry instanceof LibraryOrderEntry) { final LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry) orderEntry; if (library == libraryOrderEntry.getLibrary()) { return libraryOrderEntry; } if (library.equals(libraryOrderEntry.getLibrary())) { if (libraryOrderEntry.getScope() == scope) { return libraryOrderEntry; } else { candidate = libraryOrderEntry; } } } } return candidate; }
private static void removeObsolete( @NotNull Map<Set<String>, LibraryDependencyData> moduleLibrariesToImport, @NotNull Map<String, LibraryDependencyData> projectLibrariesToImport, @NotNull Set<LibraryDependencyData> toImport, @NotNull ModifiableRootModel moduleRootModel) { Set<String> moduleLibraryKey = ContainerUtilRt.newHashSet(); for (OrderEntry entry : moduleRootModel.getOrderEntries()) { if (entry instanceof ModuleLibraryOrderEntryImpl) { Library library = ((ModuleLibraryOrderEntryImpl) entry).getLibrary(); if (library == null) { LOG.warn( "Skipping module-level library entry because it doesn't have backing Library object. Entry: " + entry); continue; } moduleLibraryKey.clear(); for (VirtualFile file : library.getFiles(OrderRootType.CLASSES)) { moduleLibraryKey.add(ExternalSystemApiUtil.getLocalFileSystemPath(file)); } LibraryDependencyData existing = moduleLibrariesToImport.remove(moduleLibraryKey); if (existing == null) { moduleRootModel.removeOrderEntry(entry); } else { toImport.remove(existing); } } else if (entry instanceof LibraryOrderEntry) { String libraryName = ((LibraryOrderEntry) entry).getLibraryName(); LibraryDependencyData existing = projectLibrariesToImport.remove(libraryName); if (existing == null) { moduleRootModel.removeOrderEntry(entry); } else { toImport.remove(existing); } } } }