private static @NotNull Map<String, DefaultExternalSourceSet> collectSourceSets(
    @NotNull Project project,
    @NotNull ModelBuilderContext context
  ) {
    GradleSourceSetResolutionContext sourceSetResolutionContext = new GradleSourceSetResolutionContext(project, context);

    SourceSetContainer sourceSets = JavaPluginUtil.getSourceSetContainer(project);
    if (sourceSets == null) {
      return new LinkedHashMap<>();
    }

    Map<String, DefaultExternalSourceSet> result = new LinkedHashMap<>();
    sourceSets.forEach(sourceSet -> {
      DefaultExternalSourceSet externalSourceSet = new DefaultExternalSourceSet();
      externalSourceSet.setName(sourceSet.getName());
      externalSourceSet.setArtifacts(collectSourceSetArtifacts(project, context, sourceSet));

      addJavaCompilerOptions(externalSourceSet, project, sourceSet, sourceSetResolutionContext);
      addSourceDirs(externalSourceSet, project, sourceSet, sourceSetResolutionContext);
      addLegacyTestSourceDirs(externalSourceSet, project, sourceSetResolutionContext);

      cleanupSharedIdeaSourceDirs(externalSourceSet, sourceSetResolutionContext);

      result.put(externalSourceSet.getName(), externalSourceSet);
    });

    addUnprocessedIdeaSourceDirs(result, sourceSets, sourceSetResolutionContext, SourceSet.MAIN_SOURCE_SET_NAME);
    addUnprocessedIdeaResourceDirs(result, sourceSetResolutionContext, SourceSet.MAIN_SOURCE_SET_NAME);
    addUnprocessedIdeaGeneratedSourcesDirs(result, sourceSetResolutionContext, SourceSet.MAIN_SOURCE_SET_NAME);

    addUnprocessedIdeaSourceDirs(result, sourceSets, sourceSetResolutionContext, SourceSet.TEST_SOURCE_SET_NAME);
    addUnprocessedIdeaResourceDirs(result, sourceSetResolutionContext, SourceSet.TEST_SOURCE_SET_NAME);
    addUnprocessedIdeaGeneratedSourcesDirs(result, sourceSetResolutionContext, SourceSet.TEST_SOURCE_SET_NAME);

    cleanupSharedSourceDirs(result, SourceSet.MAIN_SOURCE_SET_NAME, null);
    cleanupSharedSourceDirs(result, SourceSet.TEST_SOURCE_SET_NAME, SourceSet.MAIN_SOURCE_SET_NAME);

    return result;
  }
private static @NotNull Map<String, DefaultExternalSourceSet> collectSourceSets(
    @NotNull Project project,
    @NotNull ModelBuilderContext context
  )