Class SwitchEntry
java.lang.Object
com.github.javaparser.ast.Node
com.github.javaparser.ast.stmt.SwitchEntry
- All Implemented Interfaces:
NodeWithRange<Node>, NodeWithStatements<SwitchEntry>, NodeWithTokenRange<Node>, Observable, Visitable, HasParentNode<Node>, Cloneable
One case in a switch statement
The main Javadoc is inSwitchStmt
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
Expressionin anExpressionStmtwhich is stored as the first and only statement in statements. - The second one is of type BLOCK and stores its
BlockStmtas the first and only statement in statements. - The third one is of type THROWS_STATEMENT and stores its
ThrowStmtas 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- See Also:
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class Node
Node.BreadthFirstIterator, Node.DirectChildrenIterator, Node.ObserverRegistrationMode, Node.ParentsVisitor, Node.Parsedness, Node.PostOrderIterator, Node.PreOrderIterator, Node.TreeTraversal -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Expressionprivate booleanprivate NodeList<Expression> private SwitchEntry.TypeFields inherited from class Node
ABSOLUTE_BEGIN_LINE, ABSOLUTE_END_LINE, LINE_SEPARATOR_KEY, NODE_BY_BEGIN_POSITION, PHANTOM_KEY, prettyPrinterNoCommentsConfiguration, PRINTER_KEY, SYMBOL_RESOLVER_KEY -
Constructor Summary
ConstructorsConstructorDescriptionSwitchEntry(NodeList<Expression> labels, SwitchEntry.Type type, NodeList<Statement> statements) This constructor exists for backwards compatibility for code that instantiated `SwitchEntries` before the `isDefault` and guard fields were added.SwitchEntry(NodeList<Expression> labels, SwitchEntry.Type type, NodeList<Statement> statements, boolean isDefault, Expression guard) SwitchEntry(TokenRange tokenRange, NodeList<Expression> labels, SwitchEntry.Type type, NodeList<Statement> statements) This constructor exists for backwards compatibility for code that instantiated `SwitchEntries` before the `isDefault` and guard fields were added.SwitchEntry(TokenRange tokenRange, NodeList<Expression> labels, SwitchEntry.Type type, NodeList<Statement> statements, boolean isDefault) This constructor is used by the parser and is considered private.SwitchEntry(TokenRange tokenRange, NodeList<Expression> labels, SwitchEntry.Type type, NodeList<Statement> statements, boolean isDefault, Expression guard) This constructor is used by the parser and is considered private. -
Method Summary
Modifier and TypeMethodDescription<R,A> R accept(GenericVisitor<R, A> v, A arg) Accept method for visitor support.<A> voidaccept(VoidVisitor<A> v, A arg) Accept method for visitor support.clone()getGuard()getType()booleanbooleanThis is required for the ConcreteSyntaxModel, specifically to determine whether this entry uses the classic switch statement syntax (e.g.booleanbooleansetDefault(boolean isDefault) setGuard(Expression guard) setLabels(NodeList<Expression> labels) Sets the labelsetStatements(NodeList<Statement> statements) setType(SwitchEntry.Type type) Methods inherited from class Node
addOrphanComment, containsData, createDefaultPrinter, createDefaultPrinter, customInitialization, equals, findAll, findAll, findAll, findByRange, findCompilationUnit, findData, findFirst, findFirst, findFirst, findRootNode, getAllContainedComments, getChildNodes, getChildNodesByType, getComment, getData, getDataKeys, getDefaultPrinterConfiguration, getLineEndingStyle, getLineEndingStyleOrDefault, getNodesByType, getOrphanComments, getParentNode, getParentNodeForChildren, getParsed, getPrinter, getPrinter, getRange, getSymbolResolver, getTokenRange, hashCode, hasScope, isAncestorOf, isPhantom, isRegistered, notifyPropertyChange, register, register, registerForSubtree, remove, removeComment, removeData, removeForced, removeOrphanComment, replace, setAsParentNodeOf, setAsParentNodeOf, setBlockComment, setComment, setData, setLineComment, setParentNode, setParsed, setRange, setTokenRange, stream, stream, toString, toString, tryAddImportToParentCompilationUnit, unregister, walk, walk, walkMethods inherited from interface HasParentNode
findAncestor, findAncestor, findAncestor, hasParentNode, isDescendantOfMethods inherited from interface NodeWithRange
containsWithin, containsWithinRange, getBegin, getEnd, hasRangeMethods inherited from interface NodeWithStatements
addAndGetStatement, addAndGetStatement, addAndGetStatement, addAndGetStatement, addStatement, addStatement, addStatement, addStatement, addStatement, copyStatements, copyStatements, getStatement, isEmpty, setStatement
-
Field Details
-
labels
-
statements
-
type
-
isDefault
private boolean isDefault -
guard
-
-
Constructor Details
-
SwitchEntry
public SwitchEntry() -
SwitchEntry
public SwitchEntry(TokenRange tokenRange, NodeList<Expression> labels, SwitchEntry.Type type, NodeList<Statement> statements) This constructor exists for backwards compatibility for code that instantiated `SwitchEntries` before the `isDefault` and guard fields were added. -
SwitchEntry
public SwitchEntry(NodeList<Expression> labels, SwitchEntry.Type type, NodeList<Statement> statements) This constructor exists for backwards compatibility for code that instantiated `SwitchEntries` before the `isDefault` and guard fields were added. -
SwitchEntry
public SwitchEntry(NodeList<Expression> labels, SwitchEntry.Type type, NodeList<Statement> statements, boolean isDefault, Expression guard) -
SwitchEntry
public SwitchEntry(TokenRange tokenRange, NodeList<Expression> labels, SwitchEntry.Type type, NodeList<Statement> statements, boolean isDefault, Expression guard) This constructor is used by the parser and is considered private. -
SwitchEntry
public SwitchEntry(TokenRange tokenRange, NodeList<Expression> labels, SwitchEntry.Type type, NodeList<Statement> statements, boolean isDefault) This constructor is used by the parser and is considered private.
-
-
Method Details
-
isSwitchStatementEntry
public boolean isSwitchStatementEntry()This is required for the ConcreteSyntaxModel, specifically to determine whether this entry uses the classic switch statement syntax (e.g. `case X: ...`) or the newer switch expression syntax (`case X -> ...`). The entry type is STATEMENT_GROUP in the switch statement case and all other values are for the various switch expressions. -
accept
Description copied from interface:VisitableAccept method for visitor support.- Specified by:
acceptin interfaceVisitable- Type Parameters:
R- the type of the return value of the visitorA- the type the user argument passed to the visitor- Parameters:
v- the visitor implementationarg- the argument passed to the visitor (of type A)- Returns:
- the result of the visit (of type R)
-
accept
Description copied from interface:VisitableAccept method for visitor support. -
getLabels
-
getStatements
- Specified by:
getStatementsin interfaceNodeWithStatements<SwitchEntry>
-
setLabels
Sets the label- Parameters:
labels- the label, can be null- Returns:
- this, the SwitchEntry
-
setStatements
- Specified by:
setStatementsin interfaceNodeWithStatements<SwitchEntry>
-
remove
-
clone
-
getMetaModel
- Overrides:
getMetaModelin classNode- Returns:
- get JavaParser specific node introspection information.
-
getType
-
setType
-
replace
-
isDefault
public boolean isDefault() -
setDefault
-
getGuard
-
setGuard
-
removeGuard
-