Class SwitchEntry

  • All Implemented Interfaces:
    NodeWithRange<Node>, NodeWithStatements<SwitchEntry>, NodeWithTokenRange<Node>, Observable, Visitable, HasParentNode<Node>, Cloneable

    public class SwitchEntry
    extends Node
    implements NodeWithStatements<SwitchEntry>

    One case in a switch statement

    The main Javadoc is in SwitchStmt

    Java 1.0-11

    
     switch (i) {
       case 1:
       case 2:
         System.out.println(444);
         break;
       default:
         System.out.println(0);
     }
     

    This contains three SwitchEntrys. All of them are of type STATEMENT_GROUP.
    • The first one has label 1 and no statements.
    • The second has label 2 and two statements (the println and the break).
    • The third, the default, has no label and one statement.

    Java 12-

    
         case 1 -> 15*15;
         case 2 -> { a++; b++; }
         case 3 -> throw new Exception();
     
    These are three new variants.
    • The first one is of type EXPRESSION and stores its Expression in an ExpressionStmt which is stored as the first and only statement in statements.
    • The second one is of type BLOCK and stores its BlockStmt as the first and only statement in statements.
    • The third one is of type THROWS_STATEMENT and stores its ThrowStmt as the first and only statement in statements.
    
         case MONDAY, FRIDAY, SUNDAY -> 6;
     
    Multiple case labels are now allowed.
    
         case 16*16, 10+10 -> 6;
     
    Many kinds of expressions are now allowed. Note (https://github.com/javaparser/javaparser/pull/4679): The JavaParser representation for SwitchEntry is (slightly) incorrect. JP Assumes that the body of a SwitchEntry will be a list of statements which was true before switch expressions were added, but is no longer the case for this rule. The workaround for this was to wrap the expression in an ExpressionStmt node which works well, but is not entirely correct according to the JLS since the ExpressionStmt in this specific case can contain any expression, not just those which are legal expression statements according to the JLS, for example below (a lambda is not a valid expression statement, but the below snippet is still legal Java code):
    
          return switch (o) {
              case String s -> (arg) -> System.out.println(arg + s);
              case null, default -> (arg) -> {};
          };
     
    https://docs.oracle.com/javase/specs/jls/se21/html/jls-15.html#jls-15.28 https://docs.oracle.com/javase/specs/jls/se21/html/jls-14.html#jls-14.8
    Author:
    Julio Vilmar Gesser
    See Also:
    SwitchStmt, SwitchExpr