private static void addSourceDirs(
    @NotNull DefaultExternalSourceSet externalSourceSet, // mutable
    @NotNull Project project,
    @NotNull SourceSet sourceSet,
    @NotNull GradleSourceSetResolutionContext sourceSetResolutionContext
  ) {
    boolean resolveSourceSetDependencies = Boolean.getBoolean("idea.resolveSourceSetDependencies");

    DefaultExternalSourceDirectorySet sourceDirectorySet = new DefaultExternalSourceDirectorySet();
    sourceDirectorySet.setName(sourceSet.getAllJava().getName());
    sourceDirectorySet.setSrcDirs(sourceSet.getAllJava().getSrcDirs());
    sourceDirectorySet.setGradleOutputDirs(sourceSet.getOutput().getClassesDirs().getFiles());
    if (sourceDirectorySet.getGradleOutputDirs().isEmpty()) {
      sourceDirectorySet.setGradleOutputDirs(Collections.singleton(GradleProjectUtil.getBuildDirectory(project)));
    }
    sourceDirectorySet.setCompilerOutputPathInherited(sourceSetResolutionContext.isIdeaInheritOutputDirs);

    DefaultExternalSourceDirectorySet resourcesDirectorySet = new DefaultExternalSourceDirectorySet();
    resourcesDirectorySet.setName(sourceSet.getResources().getName());
    resourcesDirectorySet.setSrcDirs(sourceSet.getResources().getSrcDirs());
    resourcesDirectorySet.setExcludes(sourceSet.getResources().getExcludes());
    resourcesDirectorySet.setIncludes(sourceSet.getResources().getIncludes());
    if (sourceSet.getOutput().getResourcesDir() != null) {
      resourcesDirectorySet.setGradleOutputDirs(Collections.singleton(sourceSet.getOutput().getResourcesDir()));
    }
    if (resourcesDirectorySet.getGradleOutputDirs().isEmpty()) {
      resourcesDirectorySet.setGradleOutputDirs(sourceDirectorySet.getGradleOutputDirs());
    }
    resourcesDirectorySet.setCompilerOutputPathInherited(sourceSetResolutionContext.isIdeaInheritOutputDirs);

    DefaultExternalSourceDirectorySet generatedSourceDirectorySet = null;
    Set<File> generatedSourceDirs = new LinkedHashSet<>(sourceDirectorySet.getSrcDirs());
    generatedSourceDirs.retainAll(sourceSetResolutionContext.ideaGeneratedSourceDirs);
    if (!generatedSourceDirs.isEmpty()) {
      sourceDirectorySet.getSrcDirs().removeAll(generatedSourceDirs);
      sourceSetResolutionContext.unprocessedIdeaGeneratedSourceDirs.removeAll(generatedSourceDirs);

      generatedSourceDirectorySet = new DefaultExternalSourceDirectorySet();
      generatedSourceDirectorySet.setName("generated " + sourceDirectorySet.getName());
      generatedSourceDirectorySet.setSrcDirs(generatedSourceDirs);
      generatedSourceDirectorySet.setGradleOutputDirs(sourceDirectorySet.getGradleOutputDirs());
      generatedSourceDirectorySet.setCompilerOutputPathInherited(sourceDirectorySet.isCompilerOutputPathInherited());
    }

    if (sourceSetResolutionContext.isJavaTestSourceSet(sourceSet)) {
      if (!sourceSetResolutionContext.isIdeaInheritOutputDirs && sourceSetResolutionContext.ideaTestOutputDir != null) {
        sourceDirectorySet.setOutputDir(sourceSetResolutionContext.ideaTestOutputDir);
        resourcesDirectorySet.setOutputDir(sourceSetResolutionContext.ideaTestOutputDir);
      }
      else if (SourceSet.TEST_SOURCE_SET_NAME.equals(sourceSet.getName()) || !resolveSourceSetDependencies) {
        sourceDirectorySet.setOutputDir(new File(project.getProjectDir(), "out/test/classes"));
        resourcesDirectorySet.setOutputDir(new File(project.getProjectDir(), "out/test/resources"));
      }
      else {
        String outputName = StringUtils.toCamelCase(sourceSet.getName(), true);
        sourceDirectorySet.setOutputDir(new File(project.getProjectDir(), String.format("out/%s/classes", outputName)));
        resourcesDirectorySet.setOutputDir(new File(project.getProjectDir(), String.format("out/%s/resources", outputName)));
      }
      if (generatedSourceDirectorySet != null) {
        generatedSourceDirectorySet.setOutputDir(sourceDirectorySet.getOutputDir());
      }

      resourcesDirectorySet.getExcludes().addAll(sourceSetResolutionContext.testResourcesExcludes);
      resourcesDirectorySet.getIncludes().addAll(sourceSetResolutionContext.testResourcesIncludes);
      resourcesDirectorySet.setFilters(sourceSetResolutionContext.testResourceFilters);

      externalSourceSet.addSource(ExternalSystemSourceType.TEST, sourceDirectorySet);
      externalSourceSet.addSource(ExternalSystemSourceType.TEST_RESOURCE, resourcesDirectorySet);
      if (generatedSourceDirectorySet != null) {
        externalSourceSet.addSource(ExternalSystemSourceType.TEST_GENERATED, generatedSourceDirectorySet);
      }
    }
    else {
      if (!sourceSetResolutionContext.isIdeaInheritOutputDirs && sourceSetResolutionContext.ideaOutputDir != null) {
        sourceDirectorySet.setOutputDir(sourceSetResolutionContext.ideaOutputDir);
        resourcesDirectorySet.setOutputDir(sourceSetResolutionContext.ideaOutputDir);
      }
      else if (SourceSet.MAIN_SOURCE_SET_NAME.equals(sourceSet.getName()) || !resolveSourceSetDependencies) {
        sourceDirectorySet.setOutputDir(new File(project.getProjectDir(), "out/production/classes"));
        resourcesDirectorySet.setOutputDir(new File(project.getProjectDir(), "out/production/resources"));
      }
      else {
        String outputName = StringUtils.toCamelCase(sourceSet.getName(), true);
        sourceDirectorySet.setOutputDir(new File(project.getProjectDir(), String.format("out/%s/classes", outputName)));
        resourcesDirectorySet.setOutputDir(new File(project.getProjectDir(), String.format("out/%s/resources", outputName)));
      }
      if (generatedSourceDirectorySet != null) {
        generatedSourceDirectorySet.setOutputDir(sourceDirectorySet.getOutputDir());
      }

      resourcesDirectorySet.getExcludes().addAll(sourceSetResolutionContext.resourcesExcludes);
      resourcesDirectorySet.getIncludes().addAll(sourceSetResolutionContext.resourcesIncludes);
      resourcesDirectorySet.setFilters(sourceSetResolutionContext.resourceFilters);

      externalSourceSet.addSource(ExternalSystemSourceType.SOURCE, sourceDirectorySet);
      externalSourceSet.addSource(ExternalSystemSourceType.RESOURCE, resourcesDirectorySet);
      if (generatedSourceDirectorySet != null) {
        externalSourceSet.addSource(ExternalSystemSourceType.SOURCE_GENERATED, generatedSourceDirectorySet);
      }
    }
  }
private static void addSourceDirs(
    @NotNull DefaultExternalSourceSet externalSourceSet, // mutable
    @NotNull Project project,
    @NotNull SourceSet sourceSet,
    @NotNull GradleSourceSetResolutionContext sourceSetResolutionContext
  )