private void appendChosenAnnotationsRoot(
     @NotNull final OrderEntry entry, @NotNull final VirtualFile vFile) {
   if (entry instanceof LibraryOrderEntry) {
     Library library = ((LibraryOrderEntry) entry).getLibrary();
     LOG.assertTrue(library != null);
     final ModifiableRootModel rootModel =
         ModuleRootManager.getInstance(entry.getOwnerModule()).getModifiableModel();
     final Library.ModifiableModel model = library.getModifiableModel();
     model.addRoot(vFile, AnnotationOrderRootType.getInstance());
     model.commit();
     rootModel.commit();
   } else if (entry instanceof ModuleSourceOrderEntry) {
     final ModifiableRootModel model =
         ModuleRootManager.getInstance(entry.getOwnerModule()).getModifiableModel();
     final JavaModuleExternalPaths extension =
         model.getModuleExtension(JavaModuleExternalPaths.class);
     extension.setExternalAnnotationUrls(
         ArrayUtil.mergeArrays(extension.getExternalAnnotationsUrls(), vFile.getUrl()));
     model.commit();
   } else if (entry instanceof JdkOrderEntry) {
     final SdkModificator sdkModificator = ((JdkOrderEntry) entry).getJdk().getSdkModificator();
     sdkModificator.addRoot(vFile, AnnotationOrderRootType.getInstance());
     sdkModificator.commitChanges();
   }
   myExternalAnnotations.clear();
 }
  private static void fillLibrary(
      @NotNull Library library,
      @NotNull Collection<VirtualFile> libraryRoots,
      Set<VirtualFile> exclusions) {
    ApplicationManager.getApplication().assertWriteAccessAllowed();

    Library.ModifiableModel libraryModel = library.getModifiableModel();
    for (String root : libraryModel.getUrls(OrderRootType.CLASSES)) {
      libraryModel.removeRoot(root, OrderRootType.CLASSES);
    }
    for (String root : libraryModel.getUrls(OrderRootType.SOURCES)) {
      libraryModel.removeRoot(root, OrderRootType.SOURCES);
    }
    for (VirtualFile libraryRoot : libraryRoots) {
      libraryModel.addRoot(
          libraryRoot,
          OrderRootType
              .CLASSES); // in order to consider GOPATH as library and show it in Ext. Libraries
      libraryModel.addRoot(
          libraryRoot, OrderRootType.SOURCES); // in order to find usages inside GOPATH
    }
    for (VirtualFile root : exclusions) {
      ((LibraryEx.ModifiableModelEx) libraryModel).addExcludedRoot(root.getUrl());
    }
    libraryModel.commit();
  }
  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;
          }
        }
      }
    }
  }
 @Override
 protected void addJUnitDefaultLib(
     ModifiableRootModel rootModel, String junitName, ExpandMacroToPathMap macroMap) {
   final Library library =
       rootModel.getModuleLibraryTable().getModifiableModel().createLibrary(junitName);
   final Library.ModifiableModel modifiableModel = library.getModifiableModel();
   modifiableModel.addRoot(getJunitClsUrl(junitName.contains("4")), OrderRootType.CLASSES);
   modifiableModel.commit();
 }
 @Override
 public void configureModule(
     @NotNull Module module,
     @NotNull ModifiableRootModel model,
     @Nullable ContentEntry contentEntry) {
   Library library = model.getModuleLibraryTable().createLibrary(libraryName);
   Library.ModifiableModel modifiableModel = library.getModifiableModel();
   modifiableModel.addRoot(VfsUtil.getUrlForLibraryRoot(libraryFile), libraryRootType);
   modifiableModel.commit();
 }
 protected Module createModule(final String name) {
   final Module module = super.createModule(name);
   final ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel();
   final LibraryTable.ModifiableModel modifiableModel =
       model.getModuleLibraryTable().getModifiableModel();
   final Library library = modifiableModel.createLibrary("junit");
   final Library.ModifiableModel libModel = library.getModifiableModel();
   libModel.addRoot(
       VfsUtil.getUrlForLibraryRoot(new File(PathUtil.getJarPathForClass(Before.class))),
       OrderRootType.CLASSES);
   libModel.commit();
   model.commit();
   return module;
 }
 private void importMissing(
     @NotNull Set<LibraryDependencyData> toImport,
     @NotNull ModifiableRootModel moduleRootModel,
     @NotNull LibraryTable moduleLibraryTable,
     @NotNull LibraryTable libraryTable,
     @NotNull Module module) {
   for (LibraryDependencyData dependencyData : toImport) {
     LibraryData libraryData = dependencyData.getTarget();
     if (libraryData.isUnresolved()) {
       continue;
     }
     switch (dependencyData.getLevel()) {
       case MODULE:
         @SuppressWarnings("ConstantConditions")
         Library moduleLib = moduleLibraryTable.createLibrary(dependencyData.getName());
         Library.ModifiableModel libModel = moduleLib.getModifiableModel();
         try {
           Map<OrderRootType, Collection<File>> files =
               myLibraryManager.prepareLibraryFiles(libraryData);
           myLibraryManager.registerPaths(files, libModel, dependencyData.getName());
         } finally {
           libModel.commit();
         }
         break;
       case PROJECT:
         final Library projectLib = libraryTable.getLibraryByName(dependencyData.getName());
         if (projectLib == null) {
           assert false;
           continue;
         }
         LibraryOrderEntry orderEntry = moduleRootModel.addLibraryEntry(projectLib);
         LOG.info(
             String.format(
                 "Adding library dependency '%s' to module '%s'",
                 projectLib.getName(), module.getName()));
         orderEntry.setExported(dependencyData.isExported());
         orderEntry.setScope(dependencyData.getScope());
         LOG.info(
             String.format(
                 "Configuring library dependency '%s' of module '%s' to be%s exported and have scope %s",
                 projectLib.getName(),
                 module.getName(),
                 dependencyData.isExported() ? " not" : "",
                 dependencyData.getScope()));
     }
   }
 }
  @Override
  protected void addModuleLibrary(
      ModifiableRootModel rootModel,
      Element element,
      boolean exported,
      String libName,
      String url,
      String srcUrl,
      ExpandMacroToPathMap macroMap) {
    final Library library =
        rootModel.getModuleLibraryTable().getModifiableModel().createLibrary(libName);
    final Library.ModifiableModel modifiableModel = library.getModifiableModel();
    modifiableModel.addRoot(url, OrderRootType.CLASSES);
    if (srcUrl != null) {
      modifiableModel.addRoot(srcUrl, OrderRootType.SOURCES);
    }
    EJavadocUtil.appendJavadocRoots(element, rootModel, myCurrentRoots, modifiableModel);
    modifiableModel.commit();

    setLibraryEntryExported(rootModel, exported, library);
  }
 public Library createOrGetXtendJavaLibrary(
     final ModifiableRootModel rootModel, final Module module) {
   LibraryTablesRegistrar _instance = LibraryTablesRegistrar.getInstance();
   Project _project = module.getProject();
   LibraryTable _libraryTable = _instance.getLibraryTable(_project);
   Library[] _libraries = _libraryTable.getLibraries();
   LibraryTablesRegistrar _instance_1 = LibraryTablesRegistrar.getInstance();
   LibraryTable _libraryTable_1 = _instance_1.getLibraryTable();
   Library[] _libraries_1 = _libraryTable_1.getLibraries();
   final Iterable<Library> libraryTable =
       Iterables.<Library>concat(
           ((Iterable<? extends Library>) Conversions.doWrapArray(_libraries)),
           ((Iterable<? extends Library>) Conversions.doWrapArray(_libraries_1)));
   final Function1<Library, Boolean> _function =
       new Function1<Library, Boolean>() {
         @Override
         public Boolean apply(final Library it) {
           String _name = it.getName();
           boolean _startsWith = false;
           if (_name != null) {
             _startsWith = _name.startsWith(XtendLibraryDescription.XTEND_LIBRARY_NAME);
           }
           return Boolean.valueOf(_startsWith);
         }
       };
   final Iterable<Library> xtendLibs = IterableExtensions.<Library>filter(libraryTable, _function);
   boolean _isEmpty = IterableExtensions.isEmpty(xtendLibs);
   boolean _not = (!_isEmpty);
   if (_not) {
     final Function1<Library, Boolean> _function_1 =
         new Function1<Library, Boolean>() {
           @Override
           public Boolean apply(final Library it) {
             String _name = it.getName();
             return Boolean.valueOf(
                 Objects.equal(_name, XtendLibraryDescription.XTEND_LIBRARY_NAME));
           }
         };
     boolean _exists = IterableExtensions.<Library>exists(xtendLibs, _function_1);
     if (_exists) {
       final Function1<Library, Boolean> _function_2 =
           new Function1<Library, Boolean>() {
             @Override
             public Boolean apply(final Library it) {
               String _name = it.getName();
               return Boolean.valueOf(
                   Objects.equal(_name, XtendLibraryDescription.XTEND_LIBRARY_NAME));
             }
           };
       return IterableExtensions.<Library>findFirst(xtendLibs, _function_2);
     } else {
       return IterableExtensions.<Library>head(xtendLibs);
     }
   } else {
     final NewLibraryConfiguration libDescr = this.xtendLibDescr.createLibraryDescription();
     LibraryTable _moduleLibraryTable = rootModel.getModuleLibraryTable();
     final LibraryTable.ModifiableModel model = _moduleLibraryTable.getModifiableModel();
     String _defaultLibraryName = libDescr.getDefaultLibraryName();
     final Library createdLib = model.createLibrary(_defaultLibraryName);
     final Library.ModifiableModel libModel = createdLib.getModifiableModel();
     HashMap<OrderRootType, List<String>> _libraryRoots = this.xtendLibDescr.libraryRoots();
     final Procedure2<OrderRootType, List<String>> _function_3 =
         new Procedure2<OrderRootType, List<String>>() {
           @Override
           public void apply(final OrderRootType type, final List<String> roots) {
             final Procedure1<String> _function =
                 new Procedure1<String>() {
                   @Override
                   public void apply(final String it) {
                     libModel.addRoot(it, type);
                   }
                 };
             IterableExtensions.<String>forEach(roots, _function);
           }
         };
     MapExtensions.<OrderRootType, List<String>>forEach(_libraryRoots, _function_3);
     libModel.commit();
     model.commit();
     return createdLib;
   }
 }
  private void configSurefirePlugin() {
    List<String> urls = new ArrayList<String>();

    AccessToken accessToken = ReadAction.start();
    try {
      MavenDomProjectModel domModel = null;

      Element config =
          myMavenProject.getPluginConfiguration(
              "org.apache.maven.plugins", "maven-surefire-plugin");
      for (String each :
          MavenJDOMUtil.findChildrenValuesByPath(
              config, "additionalClasspathElements", "additionalClasspathElement")) {
        String url = VfsUtil.pathToUrl(each);

        if (domModel == null) {
          domModel =
              MavenDomUtil.getMavenDomProjectModel(myModule.getProject(), myMavenProject.getFile());
        }

        if (domModel != null) {
          url = MavenPropertyResolver.resolve(url, domModel);
        }

        urls.add(url);
      }
    } finally {
      accessToken.finish();
    }

    LibraryTable moduleLibraryTable = myRootModelAdapter.getRootModel().getModuleLibraryTable();

    Library library = moduleLibraryTable.getLibraryByName(SUREFIRE_PLUGIN_LIBRARY_NAME);
    if (library == null) {
      if (urls.isEmpty()) {
        return;
      }

      library = moduleLibraryTable.createLibrary(SUREFIRE_PLUGIN_LIBRARY_NAME);
      LibraryOrderEntry orderEntry =
          myRootModelAdapter.getRootModel().findLibraryOrderEntry(library);
      orderEntry.setScope(DependencyScope.TEST);
    } else {
      if (urls.isEmpty()) {
        moduleLibraryTable.removeLibrary(library);
        return;
      }
    }

    String[] oldUrls = library.getUrls(OrderRootType.CLASSES);
    if (!urls.equals(Arrays.asList(oldUrls))) {
      Library.ModifiableModel modifiableModel = library.getModifiableModel();

      for (String url : oldUrls) {
        modifiableModel.removeRoot(url, OrderRootType.CLASSES);
      }

      for (String url : urls) {
        modifiableModel.addRoot(url, OrderRootType.CLASSES);
      }

      modifiableModel.commit();
    }
  }
  public void testModuleLibraries() throws Exception {
    File moduleFile = new File(getTestRoot(), "test.iml");
    Module module = createModule(moduleFile);
    final ModuleRootManagerImpl moduleRootManager =
        (ModuleRootManagerImpl) ModuleRootManager.getInstance(module);
    final ModifiableRootModel rootModel = moduleRootManager.getModifiableModel();
    final LibraryTable moduleLibraryTable = rootModel.getModuleLibraryTable();

    final Library unnamedLibrary = moduleLibraryTable.createLibrary();
    final File unnamedLibClasses = new File(getTestRoot(), "unnamedLibClasses");
    final VirtualFile unnamedLibClassesRoot =
        LocalFileSystem.getInstance().findFileByIoFile(unnamedLibClasses);
    final Library.ModifiableModel libraryModifyableModel = unnamedLibrary.getModifiableModel();
    libraryModifyableModel.addRoot(unnamedLibClassesRoot.getUrl(), OrderRootType.CLASSES);

    final Library namedLibrary = moduleLibraryTable.createLibrary("namedLibrary");
    final File namedLibClasses = new File(getTestRoot(), "namedLibClasses");
    final VirtualFile namedLibClassesRoot =
        LocalFileSystem.getInstance().findFileByIoFile(namedLibClasses);
    final Library.ModifiableModel namedLibraryModel = namedLibrary.getModifiableModel();
    namedLibraryModel.addRoot(namedLibClassesRoot.getUrl(), OrderRootType.CLASSES);

    ApplicationManager.getApplication()
        .runWriteAction(
            new Runnable() {
              @Override
              public void run() {
                libraryModifyableModel.commit();
                namedLibraryModel.commit();
              }
            });

    final Iterator libraryIterator = moduleLibraryTable.getLibraryIterator();
    assertEquals(libraryIterator.next(), unnamedLibrary);
    assertEquals(libraryIterator.next(), namedLibrary);

    ApplicationManager.getApplication()
        .runWriteAction(
            new Runnable() {
              @Override
              public void run() {
                rootModel.commit();
              }
            });
    final Element element = new Element("root");
    moduleRootManager.getState().writeExternal(element);
    assertElementEquals(
        element,
        "<root inherit-compiler-output=\"true\">"
            + "<exclude-output />"
            + "<orderEntry type=\"sourceFolder\" forTests=\"false\" />"
            + "<orderEntry type=\"module-library\">"
            + "<library>"
            + "<CLASSES><root url=\"file://$MODULE_DIR$/unnamedLibClasses\" /></CLASSES>"
            + "<JAVADOC />"
            + "<SOURCES />"
            + "</library>"
            + "</orderEntry>"
            + "<orderEntry type=\"module-library\">"
            + "<library name=\"namedLibrary\">"
            + "<CLASSES><root url=\"file://$MODULE_DIR$/namedLibClasses\" /></CLASSES>"
            + "<JAVADOC />"
            + "<SOURCES />"
            + "</library>"
            + "</orderEntry>"
            + "</root>",
        module);
  }