Class ProductionAssertionDetector

java.lang.Object
org.ek9lang.compiler.phase5.callgraph.ProductionAssertionDetector

public final class ProductionAssertionDetector extends Object
Detects assertion statements that are reachable from non-test program entry points.

E81012: Production assertion - assertion reachable from non-test program

Assertions (assert, assertThrows, assertDoesNotThrow) are meant for TESTING only. Production code should use 'require' for precondition validation instead.

The distinction is semantic:

  • assert: "This SHOULD be true during testing" (test validation)
  • require: "This MUST be true in production" (precondition)

Having assertions in production code indicates confused intent and should be converted to 'require' statements or removed.

  • Constructor Details

    • ProductionAssertionDetector

      public ProductionAssertionDetector(CallGraph callGraph, CallableErrorListenerLookup errorListenerLookup)
      Create a detector for assertions in production code.
      Parameters:
      callGraph - The call graph with assertion and reachability information
      errorListenerLookup - Function to get ErrorListener for emitting errors
  • Method Details

    • detectAndReport

      public List<String> detectAndReport()
      Detect all assertions in production code and emit E81012 errors.
      Returns:
      List of callable names containing production assertions (for reporting/testing)
    • detect

      public void detect(Consumer<CallableInfo> consumer)
      Alternative API for just detection without error emission. Useful for testing and analysis.
      Parameters:
      consumer - Callback for each production assertion callable found
    • getAssertionsInProductionCode

      public Set<String> getAssertionsInProductionCode()
      Get the set of callables with assertions in production code. Delegates to CallGraph.getAssertionsInProductionCode().
      Returns:
      Set of fully qualified names of callables with production assertions