public class SwitchEntry extends Node implements NodeWithStatements<SwitchEntry>
SwitchStmt
switch (i) {
case 1:
case 2:
System.out.println(444);
break;
default:
System.out.println(0);
}
case 1 -> 15*15;
case 2 -> { a++; b++; }
case 3 -> throw new Exception();
These are three new variants.
Expression
in an ExpressionStmt
which is stored as the first and only statement in statements.BlockStmt
as the first and only statement in statements.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.8SwitchStmt
,
SwitchExpr
Modifier and Type | Class and Description |
---|---|
static class |
SwitchEntry.Type |
Node.BreadthFirstIterator, Node.DirectChildrenIterator, Node.ObserverRegistrationMode, Node.ParentsVisitor, Node.Parsedness, Node.PostOrderIterator, Node.PreOrderIterator, Node.TreeTraversal
ABSOLUTE_BEGIN_LINE, ABSOLUTE_END_LINE, LINE_SEPARATOR_KEY, NODE_BY_BEGIN_POSITION, PHANTOM_KEY, prettyPrinterNoCommentsConfiguration, PRINTER_KEY, SYMBOL_RESOLVER_KEY
Constructor and Description |
---|
SwitchEntry() |
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(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.
|
Modifier and Type | Method and Description |
---|---|
<R,A> R |
accept(GenericVisitor<R,A> v,
A arg)
Accept method for visitor support.
|
<A> void |
accept(VoidVisitor<A> v,
A arg)
Accept method for visitor support.
|
SwitchEntry |
clone() |
Optional<Expression> |
getGuard() |
NodeList<Expression> |
getLabels() |
SwitchEntryMetaModel |
getMetaModel() |
NodeList<Statement> |
getStatements() |
SwitchEntry.Type |
getType() |
boolean |
isDefault() |
boolean |
isSwitchStatementEntry()
This is required for the ConcreteSyntaxModel, specifically to determine whether this
entry uses the classic switch statement syntax (e.g.
|
boolean |
remove(Node node) |
SwitchEntry |
removeGuard() |
boolean |
replace(Node node,
Node replacementNode) |
SwitchEntry |
setDefault(boolean isDefault) |
SwitchEntry |
setGuard(Expression guard) |
SwitchEntry |
setLabels(NodeList<Expression> labels)
Sets the label
|
SwitchEntry |
setStatements(NodeList<Statement> statements) |
SwitchEntry |
setType(SwitchEntry.Type type) |
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, walk
finalize, getClass, notify, notifyAll, wait, wait, wait
addAndGetStatement, addAndGetStatement, addAndGetStatement, addAndGetStatement, addStatement, addStatement, addStatement, addStatement, addStatement, copyStatements, copyStatements, getStatement, isEmpty, setStatement
findAncestor, findAncestor, findAncestor, hasParentNode, isDescendantOf
containsWithin, containsWithinRange, getBegin, getEnd, hasRange
public SwitchEntry()
public SwitchEntry(TokenRange tokenRange, NodeList<Expression> labels, SwitchEntry.Type type, NodeList<Statement> statements)
public SwitchEntry(NodeList<Expression> labels, SwitchEntry.Type type, NodeList<Statement> statements)
public SwitchEntry(NodeList<Expression> labels, SwitchEntry.Type type, NodeList<Statement> statements, boolean isDefault, Expression guard)
public SwitchEntry(TokenRange tokenRange, NodeList<Expression> labels, SwitchEntry.Type type, NodeList<Statement> statements, boolean isDefault, Expression guard)
public SwitchEntry(TokenRange tokenRange, NodeList<Expression> labels, SwitchEntry.Type type, NodeList<Statement> statements, boolean isDefault)
public boolean isSwitchStatementEntry()
public <R,A> R accept(GenericVisitor<R,A> v, A arg)
Visitable
accept
in interface Visitable
R
- the type of the return value of the visitorA
- the type the user argument passed to the visitorv
- the visitor implementationarg
- the argument passed to the visitor (of type A)public <A> void accept(VoidVisitor<A> v, A arg)
Visitable
public NodeList<Expression> getLabels()
public NodeList<Statement> getStatements()
getStatements
in interface NodeWithStatements<SwitchEntry>
public SwitchEntry setLabels(NodeList<Expression> labels)
labels
- the label, can be nullpublic SwitchEntry setStatements(NodeList<Statement> statements)
setStatements
in interface NodeWithStatements<SwitchEntry>
public SwitchEntry clone()
public SwitchEntryMetaModel getMetaModel()
getMetaModel
in class Node
public SwitchEntry.Type getType()
public SwitchEntry setType(SwitchEntry.Type type)
public boolean isDefault()
public SwitchEntry setDefault(boolean isDefault)
public Optional<Expression> getGuard()
public SwitchEntry setGuard(Expression guard)
public SwitchEntry removeGuard()
Copyright © 2007–2025. All rights reserved.