@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;
 }