Class MethodAccessModifierRules

java.lang.Object
org.ek9lang.compiler.common.MethodAccessModifierRules

public final class MethodAccessModifierRules extends Object
Single source of truth for which access modifier is legal on a method / operator / constructor within a given aggregate genus.

This encodes exactly the genus-based visibility rules the compiler enforces in VisibilityOfOperationsOrError (phase 2). It is deliberately a pure function over the method's shape (genus, the candidate access modifier, override / open / constructor / operator flags) so it can be reused by tooling WITHOUT dragging in error emission — most importantly by the change_visibility viability predicate, which must decide whether a PROPOSED modifier is legal, not just validate the one already written in source.

Rules covered (matching VisibilityOfOperationsOrError):

  • COMPONENT method marked protectedMETHOD_MODIFIER_PROTECTED_IN_COMPONENT
  • SERVICE method marked protectedMETHOD_MODIFIER_PROTECTED_IN_SERVICE
  • CLASS_TRAIT method with any non-public modifier → METHOD_MODIFIER_NOT_REQUIRED_IN_TRAIT
  • CLASS method marked protected when not an override AND the class is not open → METHOD_MODIFIER_PROTECTED_IN_CLOSED_CLASS
  • RECORD method that is neither a constructor nor an operator → RECORDS_ONLY_SUPPORT_CONSTRUCTOR_AND_OPERATOR_METHODS

Rules NOT covered here (enforced in other phases, layered on by the viability predicate): the redundant explicit public (METHOD_ACCESS_MODIFIER_DEFAULT, phase 1), override + private (METHOD_ACCESS_MODIFIER_PRIVATE_OVERRIDE, phase 1), and an override having to match its parent's modifier (METHOD_ACCESS_MODIFIERS_DIFFER, phase 3).

  • Constructor Details

    • MethodAccessModifierRules

      public MethodAccessModifierRules()
  • Method Details

    • visibilityViolation

      public Optional<ErrorListener.SemanticClassification> visibilityViolation(SymbolGenus genus, String accessModifier, boolean isOverride, boolean aggregateIsOpen, boolean isConstructor, boolean isOperator)
      Determine whether a method/operator/constructor of the given shape may carry the supplied access modifier within an aggregate of the given genus.
      Parameters:
      genus - the enclosing aggregate's genus
      accessModifier - the candidate access modifier ("public" / "protected" / "private")
      isOverride - whether the method overrides an inherited method
      aggregateIsOpen - whether the enclosing aggregate is open for extension
      isConstructor - whether the method is a constructor
      isOperator - whether the method is an operator
      Returns:
      the semantic violation if the modifier is illegal there, otherwise empty