Class NormalCompletionVisitor

  • All Implemented Interfaces:
    GenericVisitor<Boolean,​Void>

    public class NormalCompletionVisitor
    extends GenericVisitorWithDefaults<Boolean,​Void>
    When deciding into which scope pattern variables should be introduced, it is sometimes necessary to determine whether a statement can complete normally {@see https://docs.oracle.com/javase/specs/jls/se22/html/jls-14.html#jls-14.22}. The JLS specifies that a statement can complete normally only if it is reachable and specifies rules for what it means for a statement to be reachable, but that part can be ignored in JavaParser since having unreachable code results in a compilation error and is thus not supported. This means that all of the rules are implemented with the assumption that provided nodes are reachable. An example of where this is needed is for the following rule regarding pattern variables introduced by if-statements. 6.3.2.2. if Statements {@see https://docs.oracle.com/javase/specs/jls/se22/html/jls-6.html#jls-6.3.2.2}: The following rules apply to a statement if (e) S (ยง14.9.1): A pattern variable is introduced by if (e) S iff (i) it is introduced by e when false and (ii) S cannot complete normally. This means that in this example: if (!(x instanceof Foo f)) { return; } System.out.println(f); f will be in scope for the println call since the block making up the then-block of the if statement (S in the rule above) cannot complete normally (since the last statement, return, cannot complete normally). But, in this example: if (!(x instanceof Foo f)) { } f is not introduced by the if statement since the empty then-block can complete normally.
    • Constructor Detail

      • NormalCompletionVisitor

        public NormalCompletionVisitor()
    • Method Detail

      • visit

        public Boolean visit​(BlockStmt block,
                             Void unused)
        An empty block that is not a switch block can complete normally iff it is reachable. A non-empty block that is not a switch block can complete normally iff the last statement in it can complete normally. The first statement in a non-empty block that is not a switch block is reachable iff the block is reachable. Every other statement S in a non-empty block that is not a switch block is reachable iff the statement preceding S can complete normally.
        Specified by:
        visit in interface GenericVisitor<Boolean,​Void>
        Overrides:
        visit in class GenericVisitorWithDefaults<Boolean,​Void>
      • visit

        public Boolean visit​(WhileStmt whileStmt,
                             Void unused)
        A while statement can complete normally iff at least one of the following is true: - The while statement is reachable and the condition expression is not a constant expression with value true. - There is a reachable break statement that exits the while statement.
        Specified by:
        visit in interface GenericVisitor<Boolean,​Void>
        Overrides:
        visit in class GenericVisitorWithDefaults<Boolean,​Void>
      • visit

        public Boolean visit​(DoStmt doStmt,
                             Void unused)
        A do statement can complete normally iff at least one of the following is true: - The contained statement can complete normally and the condition expression is not a constant expression with value true. - The do statement contains a reachable continue statement with no label, and the do statement is the innermost while, do, or for statement that contains that continue statement, and the continue statement continues that do statement, and the condition expression is not a constant expression with value true. - The do statement contains a reachable continue statement with label L, and the do statement has label L, and the continue statement continues that do statement, and the condition expression is not a constant expression with value true. - There is a reachable break statement that exits the do statement.
        Specified by:
        visit in interface GenericVisitor<Boolean,​Void>
        Overrides:
        visit in class GenericVisitorWithDefaults<Boolean,​Void>
      • visit

        public Boolean visit​(ForStmt forStmt,
                             Void unused)
        A basic for statement can complete normally iff at least one of the following is true: - The for statement is reachable, there is a condition expression, and the condition expression is not a constant expression with value true. - There is a reachable break statement that exits the for statement.
        Specified by:
        visit in interface GenericVisitor<Boolean,​Void>
        Overrides:
        visit in class GenericVisitorWithDefaults<Boolean,​Void>
      • visit

        public Boolean visit​(SwitchStmt switchStmt,
                             Void unused)
        A switch statement whose switch block is empty, or contains only switch labels, can complete normally. A switch statement whose switch block consists of switch labeled statement groups can complete normally iff at least one of the following is true: - The last statement in the switch block can complete normally. - There is at least one switch label after the last switch block statement group. - There is a reachable break statement that exits the switch statement. - The switch statement is not enhanced and its switch block does not contain a default label. A switch statement whose switch block consists of switch rules can complete normally iff at least one of the following is true: - One of the switch rules introduces a switch rule expression (which is necessarily a statement expression). - One of the switch rules introduces a switch rule block that can complete normally. - One of the switch rules introduces a switch rule block that contains a reachable break statement which exits the switch statement. - The switch statement is not enhanced and its switch block does not contain a default label.
        Specified by:
        visit in interface GenericVisitor<Boolean,​Void>
        Overrides:
        visit in class GenericVisitorWithDefaults<Boolean,​Void>
      • containsCorrespondingBreak

        public static boolean containsCorrespondingBreak​(Statement statement)
        Parameters:
        statement - should be one of: SwitchStatement, WhileStatement, DoStatement, ForStatement
        Returns:
        true if a break corresponding to the statement is found; false otherwise