@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 GradleProject populateProject(
     @NotNull IdeaProject project, @NotNull String projectPath) {
   String projectDirPath = GradleUtil.toCanonicalPath(PathUtil.getParentPath(projectPath));
   // Gradle API doesn't expose project compile output path yet.
   GradleProject result = new GradleProject(projectDirPath, projectDirPath + "/out");
   result.setName(project.getName());
   result.setJdkVersion(project.getJdkName());
   result.setLanguageLevel(project.getLanguageLevel().getLevel());
   return result;
 }
 @NotNull
 @Override
 public Object getGradleKey(
     @NotNull GradleProject entity, @NotNull GradleChangesCalculationContext context) {
   // TODO den consider the known changes
   return entity.getName();
 }
  @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 void checkLanguageLevel(
     @NotNull GradleProject gradleProject,
     @NotNull Project intellijProject,
     @NotNull Set<GradleProjectStructureChange> currentChanges) {
   LanguageLevel gradleLevel = gradleProject.getLanguageLevel();
   LanguageLevel intellijLevel = myPlatformFacade.getLanguageLevel(intellijProject);
   if (gradleLevel != intellijLevel) {
     currentChanges.add(new GradleLanguageLevelChange(gradleLevel, intellijLevel));
   }
 }
 private static void checkName(
     @NotNull GradleProject gradleProject,
     @NotNull Project intellijProject,
     @NotNull Set<GradleProjectStructureChange> currentChanges) {
   String gradleName = gradleProject.getName();
   String intellijName = intellijProject.getName();
   if (!gradleName.equals(intellijName)) {
     currentChanges.add(new GradleProjectRenameChange(gradleName, intellijName));
   }
 }
  @Override
  public void calculate(
      @NotNull GradleProject gradleEntity,
      @NotNull Project intellijEntity,
      @NotNull GradleChangesCalculationContext context) {
    calculateProjectChanges(gradleEntity, intellijEntity, context.getCurrentChanges());

    final Set<? extends GradleModule> gradleSubEntities = gradleEntity.getModules();
    final Collection<Module> intellijSubEntities = myPlatformFacade.getModules(intellijEntity);
    GradleDiffUtil.calculate(
        myModuleChangesCalculator, gradleSubEntities, intellijSubEntities, context);

    LibraryTable libraryTable = myPlatformFacade.getProjectLibraryTable(intellijEntity);
    GradleDiffUtil.calculate(
        myLibraryChangesCalculator,
        gradleEntity.getLibraries(),
        Arrays.asList(libraryTable.getLibraries()),
        context);
  }
  @NotNull
  private GradleProject doResolveProjectInfo(
      @NotNull final GradleTaskId id,
      @NotNull String projectPath,
      @NotNull ProjectConnection connection,
      boolean downloadLibraries)
      throws IllegalArgumentException, IllegalStateException {
    ModelBuilder<? extends IdeaProject> modelBuilder =
        myHelper.getModelBuilder(id, connection, downloadLibraries);
    IdeaProject project = modelBuilder.get();
    GradleProject result = populateProject(project, projectPath);

    // We need two different steps ('create' and 'populate') in order to handle module dependencies,
    // i.e. when one module is
    // configured to be dependency for another one, corresponding dependency module object should be
    // available during
    // populating dependent module object.
    Map<String, Pair<GradleModule, IdeaModule>> modules = createModules(project, result);
    populateModules(modules.values(), result);
    myLibraryNamesMixer.mixNames(result.getLibraries());
    return result;
  }
  @NotNull
  private static AbstractGradleDependency buildDependency(
      @NotNull GradleModule ownerModule,
      @NotNull IdeaSingleEntryLibraryDependency dependency,
      @NotNull GradleProject intellijProject)
      throws IllegalStateException {
    File binaryPath = dependency.getFile();
    if (binaryPath == null) {
      throw new IllegalStateException(
          String.format(
              "Can't parse external library dependency '%s'. Reason: it doesn't specify path to the binaries",
              dependency));
    }

    // Gradle API doesn't provide library name at the moment.
    GradleLibrary library = new GradleLibrary(FileUtil.getNameWithoutExtension(binaryPath));
    library.addPath(LibraryPathType.BINARY, binaryPath.getAbsolutePath());

    File sourcePath = dependency.getSource();
    if (sourcePath != null) {
      library.addPath(LibraryPathType.SOURCE, sourcePath.getAbsolutePath());
    }

    File javadocPath = dependency.getJavadoc();
    if (javadocPath != null) {
      library.addPath(LibraryPathType.DOC, javadocPath.getAbsolutePath());
    }

    if (!intellijProject.addLibrary(library)) {
      for (GradleLibrary registeredLibrary : intellijProject.getLibraries()) {
        if (registeredLibrary.equals(library)) {
          return new GradleLibraryDependency(ownerModule, registeredLibrary);
        }
      }
    }

    return new GradleLibraryDependency(ownerModule, library);
  }