private void attachLibraries(
      @NotNull Collection<VirtualFile> libraryRoots, Set<VirtualFile> exclusions) {
    ApplicationManager.getApplication().assertIsDispatchThread();

    if (!libraryRoots.isEmpty()) {
      ApplicationManager.getApplication()
          .runWriteAction(
              () -> {
                ModuleRootManager model = ModuleRootManager.getInstance(myModule);
                LibraryOrderEntry goLibraryEntry =
                    OrderEntryUtil.findLibraryOrderEntry(model, getLibraryName());

                if (goLibraryEntry != null && goLibraryEntry.isValid()) {
                  Library library = goLibraryEntry.getLibrary();
                  if (library != null && !((LibraryEx) library).isDisposed()) {
                    fillLibrary(library, libraryRoots, exclusions);
                  }
                } else {
                  LibraryTable libraryTable =
                      LibraryTablesRegistrar.getInstance().getLibraryTable(myModule.getProject());
                  Library library = libraryTable.createLibrary(getLibraryName());
                  fillLibrary(library, libraryRoots, exclusions);
                  ModuleRootModificationUtil.addDependency(myModule, library);
                }
              });
      showNotification(myModule.getProject());
    } else {
      removeLibraryIfNeeded();
    }
  }
 public static void removeExcludedRoot(Module module, VirtualFile root) {
   ModuleRootModificationUtil.updateModel(
       module,
       model -> {
         ContentEntry entry = findContentEntryWithAssertion(model, root);
         entry.removeExcludeFolder(root.getUrl());
       });
 }
 public static void removeAllRoots(Module module, Sdk jdk) {
   ModuleRootModificationUtil.updateModel(
       module,
       model -> {
         model.clear();
         model.setSdk(jdk);
       });
 }
 public static void addExcludedRoot(Module module, VirtualFile dir) {
   ModuleRootModificationUtil.updateModel(
       module,
       model ->
           ApplicationManager.getApplication()
               .runReadAction(
                   () -> {
                     findContentEntryWithAssertion(model, dir).addExcludeFolder(dir);
                   }));
 }
 public static <P extends JpsElement> void addSourceRoot(
     Module module, VirtualFile vDir, @NotNull JpsModuleSourceRootType<P> rootType, P properties) {
   ModuleRootModificationUtil.updateModel(
       module,
       model -> {
         ContentEntry entry = findContentEntry(model, vDir);
         if (entry == null) entry = model.addContentEntry(vDir);
         entry.addSourceFolder(vDir, rootType, properties);
       });
 }
 public static Library addProjectLibrary(
     Module module,
     String libName,
     List<VirtualFile> classesRoots,
     List<VirtualFile> sourceRoots) {
   Ref<Library> result = Ref.create();
   ModuleRootModificationUtil.updateModel(
       module,
       model -> result.set(addProjectLibrary(module, model, libName, classesRoots, sourceRoots)));
   return result.get();
 }
  public static ContentEntry addContentRoot(Module module, VirtualFile vDir) {
    ModuleRootModificationUtil.updateModel(module, model -> model.addContentEntry(vDir));

    for (ContentEntry entry : ModuleRootManager.getInstance(module).getContentEntries()) {
      if (Comparing.equal(entry.getFile(), vDir)) {
        Assert.assertFalse(((ContentEntryImpl) entry).isDisposed());
        return entry;
      }
    }

    return null;
  }
 public static void removeSourceRoot(Module module, VirtualFile root) {
   ModuleRootModificationUtil.updateModel(
       module,
       model -> {
         ContentEntry entry = findContentEntryWithAssertion(model, root);
         for (SourceFolder sourceFolder : entry.getSourceFolders()) {
           if (root.equals(sourceFolder.getFile())) {
             entry.removeSourceFolder(sourceFolder);
             break;
           }
         }
       });
 }
 public static void setCompilerOutputPath(Module module, String url, boolean forTests) {
   ModuleRootModificationUtil.updateModel(
       module,
       model -> {
         CompilerModuleExtension extension =
             model.getModuleExtension(CompilerModuleExtension.class);
         extension.inheritCompilerOutputPath(false);
         if (forTests) {
           extension.setCompilerOutputPathForTests(url);
         } else {
           extension.setCompilerOutputPath(url);
         }
       });
 }
 public static void addLibrary(
     Module module, String libName, String libDir, String[] classRoots, String[] sourceRoots) {
   String proto =
       (classRoots.length > 0 ? classRoots[0] : sourceRoots[0]).endsWith(".jar!/")
           ? JarFileSystem.PROTOCOL
           : LocalFileSystem.PROTOCOL;
   String parentUrl = VirtualFileManager.constructUrl(proto, libDir);
   List<String> classesUrls = new ArrayList<>();
   List<String> sourceUrls = new ArrayList<>();
   for (String classRoot : classRoots) {
     classesUrls.add(parentUrl + classRoot);
   }
   for (String sourceRoot : sourceRoots) {
     sourceUrls.add(parentUrl + sourceRoot);
   }
   ModuleRootModificationUtil.addModuleLibrary(module, libName, classesUrls, sourceUrls);
 }
 public static void setJavadocUrls(Module module, String... urls) {
   ModuleRootModificationUtil.updateModel(
       module,
       model -> model.getModuleExtension(JavaModuleExternalPaths.class).setJavadocUrls(urls));
 }
 public static void setExcludeCompileOutput(Module module, boolean exclude) {
   ModuleRootModificationUtil.updateModel(
       module,
       model -> model.getModuleExtension(CompilerModuleExtension.class).setExcludeOutput(exclude));
 }
 public static void addLibrary(Module module, String libName, String libPath, String... jarArr) {
   ModuleRootModificationUtil.updateModel(
       module, model -> addLibrary(module, model, libName, libPath, jarArr));
 }
 public static void removeContentEntry(Module module, VirtualFile contentRoot) {
   ModuleRootModificationUtil.updateModel(
       module,
       model -> model.removeContentEntry(findContentEntryWithAssertion(model, contentRoot)));
 }
 public static void addSourceContentToRoots(
     Module module, @NotNull VirtualFile vDir, boolean testSource) {
   ModuleRootModificationUtil.updateModel(
       module, model -> model.addContentEntry(vDir).addSourceFolder(vDir, testSource));
 }