Class TraitImplementationAnalyzer

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

final class TraitImplementationAnalyzer extends Object
Analyzes how traits are implemented in an aggregate to detect E11023 pattern (missing 'by' delegation).

The E11023 anti-pattern occurs when:

  • A class implements a trait WITHOUT using 'by' delegation
  • The class has a field of the trait type (or compatible type)
  • The developer manually implements all trait methods by forwarding to that field

This is boilerplate that could be eliminated by using 'by' delegation:

// Anti-pattern (manual forwarding):
class Foo with trait of Bar
  barImpl as Bar?
  override method1() -> barImpl.method1()
  override method2() -> barImpl.method2()
  ...

// Better (use 'by' delegation):
class Foo with trait of Bar by barImpl
  barImpl as Bar?
  // Methods automatically delegated!

Detection approach:

  1. Find traits implemented WITHOUT 'by'
  2. Check if the class has a field assignable to that trait
  3. Count how many trait methods are overridden in this class
  4. If all/most trait methods are overridden and there's a compatible field = likely forwarding

Note: We cannot detect actual forwarding without IR analysis. This is a heuristic that identifies LIKELY forwarding patterns.

  • Constructor Details

    • TraitImplementationAnalyzer

      TraitImplementationAnalyzer()
  • Method Details

    • analyzeTraitsWithoutDelegation

      List<TraitImplementationAnalyzer.TraitAnalysis> analyzeTraitsWithoutDelegation(IAggregateSymbol aggregate, EK9Parser.TraitsListContext traitsListContext)
      Analyze traits implemented WITHOUT 'by' delegation to detect potential E11023 patterns.
      Parameters:
      aggregate - The aggregate to analyze
      traitsListContext - Parse tree context for trait declarations
      Returns:
      List of analysis results for each trait implemented without 'by'