public void analyzeFiles()

in src/main/java/com/uber/scip/aggregator/FileAnalyzer.java [123:191]


  public void analyzeFiles(List<File> files) throws IOException {
    // Parse and filter files first
    Iterable<? extends JavaFileObject> compilationUnits =
        fileManager.getJavaFileObjectsFromFiles(files);
    Path outputDir = Files.createTempDirectory(UUID.randomUUID().toString());
    Path scipOutputPath = Paths.get(this.outputPath);
    DiagnosticCollector<JavaFileObject> diagnosticCollector =
        this.diagnosticCollectorSupplier.get();

    if ((int) StreamSupport.stream(compilationUnits.spliterator(), false).count() < 1) {
      logger.debug("No valid files to analyze. Writing empty index.");
      // Write empty index file to cache output.
      buildScip(outputDir, scipOutputPath, diagnosticCollector);
      return;
    }
    List<String> allOptions = compilerOptions.getCompilerOptions();

    try {
      semanticDbManager.setTargetRoot(outputDir.toString());
      allOptions.add(semanticDbManager.formatSemanticDBPluginConfig());
      runAnalysis(diagnosticCollector, allOptions, compilationUnits);
    } catch (IllegalStateException e) {
      // Sometimes javac will fail due to incomplete compile cycle.
      // In this case, we can try to limit symbol generation to given target.
      // Example: //3rdparty/jvm/org/apache/hadoop:hadoop-yarn-common-2.8.2.jar
      // Sometimes target missing required dependencies.
      // Example //3rdparty/jvm/org/springframework/boot:spring-boot-2.7.18.jar missing
      // 3rdparty/jvm/jakarta/validation:jakarta.validation-api-2.0.2.jar
      // In this case internal symbols will be unresolved.
      diagnosticCollector = this.diagnosticCollectorSupplier.get();
      compilerOptions.setClasspath(semanticDbManager.getPlugin());
      allOptions = compilerOptions.getCompilerOptions();
      allOptions.add(semanticDbManager.formatSemanticDBPluginConfig());
      try {
        runAnalysis(diagnosticCollector, allOptions, compilationUnits);
      } catch (IllegalStateException nestedStateException) {
        // Best effort
        // Could result java.lang.IllegalStateException: java.lang.NullPointerException: Cannot read
        // field "tree" because "env" is null
        logger.debug("Nested state exception: {}", e.getMessage());
      }
    } finally {
      try {
        buildScip(outputDir, scipOutputPath, diagnosticCollector);
        FileUtils.deleteDirectory(outputDir.toFile());
      } catch (IOException e) {
        logger.warn("Failed to delete temporary directory: {}", outputDir, e);
      }
    }

    // Report any issues that occurred during analysis
    List<CompilationIssue> analysisIssues =
        diagnosticCollector.getDiagnostics().stream()
            .map(CompilationIssue::new)
            .collect(Collectors.toList());

    if (!analysisIssues.isEmpty()) {
      logger.debug("Analysis produced {} issues:", analysisIssues.size());
      analysisIssues.forEach(
          issue ->
              logger.debug(
                  "  {} in {} at position {}:{} - {}",
                  issue.getKind(),
                  issue.getSource(),
                  issue.getLineNumber(),
                  issue.getColumnNumberStart(),
                  issue.getMessage()));
    }
  }