@NotNull
  private static AbstractGradleDependency buildDependency(
      @NotNull GradleModule ownerModule,
      @NotNull IdeaModuleDependency dependency,
      @NotNull GradleProject intellijProject)
      throws IllegalStateException {
    IdeaModule module = dependency.getDependencyModule();
    if (module == null) {
      throw new IllegalStateException(
          String.format(
              "Can't parse gradle module dependency '%s'. Reason: referenced module is null",
              dependency));
    }

    String moduleName = module.getName();
    if (moduleName == null) {
      throw new IllegalStateException(
          String.format(
              "Can't parse gradle module dependency '%s'. Reason: referenced module name is undefined (module: '%s') ",
              dependency, module));
    }

    Set<String> registeredModuleNames = new HashSet<String>();
    for (GradleModule gradleModule : intellijProject.getModules()) {
      registeredModuleNames.add(gradleModule.getName());
      if (gradleModule.getName().equals(moduleName)) {
        return new GradleModuleDependency(ownerModule, gradleModule);
      }
    }
    throw new IllegalStateException(
        String.format(
            "Can't parse gradle module dependency '%s'. Reason: no module with such name (%s) is found. Registered modules: %s",
            dependency, moduleName, registeredModuleNames));
  }
 private static void removeExistingModulesConfigs(
     @NotNull Collection<? extends GradleModule> modules) {
   for (GradleModule module : modules) {
     // Remove existing '*.iml' file if necessary.
     final String moduleFilePath = module.getModuleFilePath();
     File file = new File(moduleFilePath);
     if (file.isFile()) {
       boolean success = file.delete();
       if (!success) {
         GradleLog.LOG.warn("Can't remove existing module file at '" + moduleFilePath + "'");
       }
     }
   }
 }
  private static void populateDependencies(
      @NotNull IdeaModule gradleModule,
      @NotNull GradleModule intellijModule,
      @NotNull GradleProject intellijProject) {
    DomainObjectSet<? extends IdeaDependency> dependencies = gradleModule.getDependencies();
    if (dependencies == null) {
      return;
    }
    for (IdeaDependency dependency : dependencies) {
      if (dependency == null) {
        continue;
      }
      AbstractGradleDependency intellijDependency = null;
      if (dependency instanceof IdeaModuleDependency) {
        intellijDependency =
            buildDependency(intellijModule, (IdeaModuleDependency) dependency, intellijProject);
      } else if (dependency instanceof IdeaSingleEntryLibraryDependency) {
        intellijDependency =
            buildDependency(
                intellijModule, (IdeaSingleEntryLibraryDependency) dependency, intellijProject);
      }

      if (intellijDependency == null) {
        continue;
      }

      intellijDependency.setExported(dependency.getExported());
      DependencyScope scope = parseScope(dependency.getScope());
      if (scope != null) {
        intellijDependency.setScope(scope);
      }
      intellijModule.addDependency(intellijDependency);
    }
  }
 /**
  * Populates {@link GradleModule#getContentRoots() content roots} of the given intellij module on
  * the basis of the information contained at the given gradle module.
  *
  * @param gradleModule holder of the module information received from the gradle tooling api
  * @param intellijModule corresponding module from intellij gradle plugin domain
  * @throws IllegalArgumentException if given gradle module contains invalid data
  */
 private static void populateContentRoots(
     @NotNull IdeaModule gradleModule, @NotNull GradleModule intellijModule)
     throws IllegalArgumentException {
   DomainObjectSet<? extends IdeaContentRoot> contentRoots = gradleModule.getContentRoots();
   if (contentRoots == null) {
     return;
   }
   for (IdeaContentRoot gradleContentRoot : contentRoots) {
     if (gradleContentRoot == null) {
       continue;
     }
     File rootDirectory = gradleContentRoot.getRootDirectory();
     if (rootDirectory == null) {
       continue;
     }
     GradleContentRoot intellijContentRoot =
         new GradleContentRoot(intellijModule, rootDirectory.getAbsolutePath());
     populateContentRoot(
         intellijContentRoot, SourceType.SOURCE, gradleContentRoot.getSourceDirectories());
     populateContentRoot(
         intellijContentRoot, SourceType.TEST, gradleContentRoot.getTestDirectories());
     Set<File> excluded = gradleContentRoot.getExcludeDirectories();
     if (excluded != null) {
       for (File file : excluded) {
         intellijContentRoot.storePath(SourceType.EXCLUDED, file.getAbsolutePath());
       }
     }
     intellijModule.addContentRoot(intellijContentRoot);
   }
 }
  private static void populateCompileOutputSettings(
      @Nullable IdeaCompilerOutput gradleSettings, @NotNull GradleModule intellijModule) {
    if (gradleSettings == null) {
      return;
    }

    File sourceCompileOutputPath = gradleSettings.getOutputDir();
    if (sourceCompileOutputPath != null) {
      intellijModule.setCompileOutputPath(
          SourceType.SOURCE, sourceCompileOutputPath.getAbsolutePath());
    }

    File testCompileOutputPath = gradleSettings.getTestOutputDir();
    if (testCompileOutputPath != null) {
      intellijModule.setCompileOutputPath(SourceType.TEST, testCompileOutputPath.getAbsolutePath());
    }
    intellijModule.setInheritProjectCompileOutputPath(
        gradleSettings.getInheritOutputDirs()
            || sourceCompileOutputPath == null
            || testCompileOutputPath == null);
  }