Class MagicLiteralTracker

java.lang.Object
org.ek9lang.compiler.phase5.MagicLiteralTracker

class MagicLiteralTracker extends Object
Tracks literal occurrences across files for repeated magic literal detection (E11065). A repeated magic literal is the same value appearing 3+ times in a single file or 4+ times across files in the same module. Only String literals are tracked — numeric literals in test/production code are domain data, not duplication bugs. This matches SonarQube S1192 and PMD AvoidDuplicateLiterals (strings only).

Thread-safe: designed to be used during parallel file processing where multiple PreIRListener instances (one per file) share a single MagicLiteralTracker.

Context exemptions: literals in constant declarations, list/dict literals, require/assert statements, constrain declarations, range expressions, named arguments (the parameter name documents the value), and constructor calls (type construction is data, not logic).

  • Constructor Details

    • MagicLiteralTracker

      MagicLiteralTracker()
  • Method Details

    • processLiteral

      void processLiteral(EK9Parser.LiteralContext literal, String moduleName, String filePath, Set<org.antlr.v4.runtime.Token> reportedByTier1)
      Record a literal occurrence for a specific module, if not exempt.
      Parameters:
      literal - the literal parser context
      moduleName - the module containing the literal
      filePath - the source file path
      reportedByTier1 - tokens already reported by Tier 1 (to avoid double-reporting)
    • getRecords

      Get a snapshot copy of literal records for a specific module.
      Parameters:
      moduleName - the module to query
      Returns:
      list of literal records (may be empty, never null)
    • isExempt

      static boolean isExempt(String moduleName)
      Check if a module is exempt from magic literal checking. Built-in modules (org.ek9.*) are exempt.