Class SwitchExpr

  • All Implemented Interfaces:
    ASTNode, Expression

    public final class SwitchExpr
    extends Object
    implements Expression
    A switch expression (Java 14, JEP 361): switch (sel) { case L -> result; default -> r; }.
    • Constructor Detail

      • SwitchExpr

        public SwitchExpr​(Expression selector,
                          List<SwitchExpr.Arm> arms,
                          SourceType type,
                          SourceLocation location)
        Creates a switch expression and adopts the selector, every label, and every arm result as children.
        Parameters:
        selector - the value switched on
        arms - the arms, or null for none
        type - the type the expression yields
        location - the source position, or null for UNKNOWN
      • SwitchExpr

        public SwitchExpr​(Expression selector,
                          List<SwitchExpr.Arm> arms,
                          SourceType type)
        Creates a switch expression with no source position.
        Parameters:
        selector - the value switched on
        arms - the arms, or null for none
        type - the type the expression yields
    • Method Detail

      • getSelector

        public Expression getSelector()
        Returns:
        the selector
      • setSelector

        public void setSelector​(Expression selector)
        Replaces the value switched on, adopting the new selector and releasing the old one.
        Parameters:
        selector - the new selector, or null
      • getLocation

        public SourceLocation getLocation()
        Description copied from interface: ASTNode
        Gets the source location of this node.
        Specified by:
        getLocation in interface ASTNode
        Returns:
        the location
      • getParent

        public ASTNode getParent()
        Description copied from interface: ASTNode
        Gets the parent node in the AST tree.
        Specified by:
        getParent in interface ASTNode
        Returns:
        the parent
      • setParent

        public void setParent​(ASTNode parent)
        Records the node this expression hangs under.
        Specified by:
        setParent in interface ASTNode
        Parameters:
        parent - the enclosing node
      • getType

        public SourceType getType()
        Description copied from interface: Expression
        Gets the inferred type of this expression.
        Specified by:
        getType in interface Expression
        Returns:
        the type of this expression
      • getChildren

        public List<ASTNode> getChildren()
        Description copied from interface: ASTNode
        Lists the direct children a node holds, in source order.
        Specified by:
        getChildren in interface ASTNode
        Returns:
        the direct children, empty for a leaf
      • accept

        public <T> T accept​(SourceVisitor<T> visitor)
        Description copied from interface: ASTNode
        Accepts a visitor for this node.
        Specified by:
        accept in interface ASTNode
        Type Parameters:
        T - the return type of the visitor
        Parameters:
        visitor - the visitor to accept
        Returns:
        the result from the visitor