@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));
  }
 @NotNull
 private static Map<String, Pair<GradleModule, IdeaModule>> createModules(
     @NotNull IdeaProject gradleProject, @NotNull GradleProject intellijProject)
     throws IllegalStateException {
   DomainObjectSet<? extends IdeaModule> gradleModules = gradleProject.getModules();
   if (gradleModules == null || gradleModules.isEmpty()) {
     throw new IllegalStateException("No modules found for the target project: " + gradleProject);
   }
   Map<String, Pair<GradleModule, IdeaModule>> result =
       new HashMap<String, Pair<GradleModule, IdeaModule>>();
   for (IdeaModule gradleModule : gradleModules) {
     if (gradleModule == null) {
       continue;
     }
     String moduleName = gradleModule.getName();
     if (moduleName == null) {
       throw new IllegalStateException("Module with undefined name detected: " + gradleModule);
     }
     GradleModule intellijModule =
         new GradleModule(moduleName, intellijProject.getProjectFileDirectoryPath());
     Pair<GradleModule, IdeaModule> previouslyParsedModule = result.get(moduleName);
     if (previouslyParsedModule != null) {
       throw new IllegalStateException(
           String.format(
               "Modules with duplicate name (%s) detected: '%s' and '%s'",
               moduleName, intellijModule, previouslyParsedModule));
     }
     result.put(moduleName, new Pair<GradleModule, IdeaModule>(intellijModule, gradleModule));
     intellijProject.addModule(intellijModule);
   }
   return result;
 }
  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 populateModule(
     @NotNull IdeaModule gradleModule,
     @NotNull GradleModule intellijModule,
     @NotNull GradleProject intellijProject)
     throws IllegalArgumentException, IllegalStateException {
   populateContentRoots(gradleModule, intellijModule);
   populateCompileOutputSettings(gradleModule.getCompilerOutput(), intellijModule);
   populateDependencies(gradleModule, intellijModule, intellijProject);
 }