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

Constructors

Link copied to clipboard
constructor()
constructor(tokenRange: TokenRange, labels: NodeList<Expression>, type: SwitchEntry.Type, statements: NodeList<Statement>)
This constructor exists for backwards compatibility for code that instantiated `SwitchEntries` before the `isDefault` and guard fields were added.
constructor(labels: NodeList<Expression>, type: SwitchEntry.Type, statements: NodeList<Statement>)
This constructor exists for backwards compatibility for code that instantiated `SwitchEntries` before the `isDefault` and guard fields were added.
constructor(labels: NodeList<Expression>, type: SwitchEntry.Type, statements: NodeList<Statement>, isDefault: Boolean, guard: Expression)
constructor(tokenRange: TokenRange, labels: NodeList<Expression>, type: SwitchEntry.Type, statements: NodeList<Statement>, isDefault: Boolean, guard: Expression)
This constructor is used by the parser and is considered private.
constructor(tokenRange: TokenRange, labels: NodeList<Expression>, type: SwitchEntry.Type, statements: NodeList<Statement>, isDefault: Boolean)
This constructor is used by the parser and is considered private.

Types

Link copied to clipboard
enum Type

Properties

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open var isDefault: Boolean
Link copied to clipboard
Link copied to clipboard
This can be used to sort nodes on position.
Link copied to clipboard
private open var parentNode: Node
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
private open var range: Range
Link copied to clipboard
Link copied to clipboard
private open var tokenRange: TokenRange
Link copied to clipboard

Functions

Link copied to clipboard
open fun <R, A> accept(v: GenericVisitor<R, A>, arg: A): R
open fun <A> accept(v: VoidVisitor<A>, arg: A)
Accept method for visitor support.
Link copied to clipboard
open fun <A : Statement?> addAndGetStatement(statement: A): A
open fun addAndGetStatement(index: Int, statement: Statement): Statement
Link copied to clipboard
open fun addOrphanComment(comment: Comment)
Link copied to clipboard
open fun addStatement(expr: Expression): N
open fun addStatement(statement: Statement): N
open fun addStatement(index: Int, expr: Expression): N
open fun addStatement(index: Int, statement: Statement): N

open fun addStatement(statement: String): N
It will use parseStatement inside, so it should end with a semicolon
Link copied to clipboard
@Nullable
open fun associatedSpecificationComments(): @Nullable NodeList<Comment>
Link copied to clipboard
open fun clone(): SwitchEntry
Link copied to clipboard
@Nullable
open fun comment(): @Nullable Comment
Link copied to clipboard
open fun containsData(key: DataKey<out Any>): Boolean
Link copied to clipboard
open fun containsWithin(other: Node): Boolean
Link copied to clipboard
open fun containsWithinRange(other: Node): Boolean
Checks whether the range of the given Node is contained within the range of this NodeWithRange.
Link copied to clipboard
open fun copyStatements(nodeList: NodeList<Statement>): N
open fun copyStatements(other: NodeWithStatements<out Any>): N
Link copied to clipboard
open fun equals(obj: Any): Boolean
Link copied to clipboard
open fun <T : Node?> findAll(nodeType: Class<T>): List<T>
Walks the AST with pre-order traversal, returning all nodes of type "nodeType".
open fun <T : Node?> findAll(nodeType: Class<T>, traversal: Node.TreeTraversal): List<T>
Walks the AST with specified traversal order, returning all nodes of type "nodeType".
open fun <T : Node?> findAll(nodeType: Class<T>, predicate: Predicate<T>): List<T>
Walks the AST with pre-order traversal, returning all nodes of type "nodeType" that match the predicate.
Link copied to clipboard
open fun <N> findAncestor(types: Array<Class<N>>): Optional<N>
Walks the parents of this node and returns the first node of type type, or empty() if none is found.
open fun <N> findAncestor(type: Class<N>, predicate: Predicate<N>): Optional<N>
Walks the parents of this node and returns the first node of type type that matches predicate, or empty() if none is found.
open fun <N> findAncestor(predicate: Predicate<N>, types: Array<Class<N>>): Optional<N>
Walks the parents of this node and returns the first node that matches one of types types, or empty() if none is found.
Link copied to clipboard
open fun findByRange(range: Range): Optional<Node>
Link copied to clipboard
Link copied to clipboard
open fun <M> findData(key: DataKey<M>): Optional<M>
Gets data for this node using the given key or returns an Optional.empty().
Link copied to clipboard
open fun <N : Node?> findFirst(nodeType: Class<N>): Optional<N>
Walks the AST with pre-order traversal, returning the first node of type "nodeType" or empty() if none is found.
open fun <T> findFirst(traversal: Node.TreeTraversal, consumer: (Node) -> Optional<T>): Optional<T>
Walks the AST, applying the function for every node, with traversal algorithm "traversal".
open fun <N : Node?> findFirst(nodeType: Class<N>, predicate: Predicate<N>): Optional<N>
Walks the AST with pre-order traversal, returning the first node of type "nodeType" that matches "predicate" or empty() if none is found.
Link copied to clipboard
open fun findRootNode(): Node
Finds the root node of this AST by finding the topmost parent.
Link copied to clipboard
This is the list of Comment which are contained in the Node either because they are properly associated to one of its children or because they are floating around inside the Node
Link copied to clipboard
This field is used by key to associated (JML) comments to this node.
Link copied to clipboard
The begin position of this node in the source file.
Link copied to clipboard
open fun getChildNodes(): List<Node>
Contains all nodes that have this node set as their parent.
Link copied to clipboard
open fun <N : Node?> getChildNodesByType(clazz: Class<N>): List<N>
Recursively finds all nodes of a certain type.
Link copied to clipboard
This is a comment associated with this node.
Link copied to clipboard
open fun <M> getData(key: DataKey<M>): M
Gets data for this node using the given key.
Link copied to clipboard
open fun getDataKeys(): Set<DataKey<out Any>>
This method was added to support the clone method.
Link copied to clipboard
open fun getEnd(): Optional<Position>
The end position of this node in the source file.
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open fun <N : Node?> getNodesByType(clazz: Class<N>): List<N>
Link copied to clipboard
This is a list of Comment which are inside the node and are not associated with any meaningful AST Node.
Link copied to clipboard
Returns the parent node, or Optional.empty if no parent is set.
Link copied to clipboard
Returns the parent node from the perspective of the children of this node.
Link copied to clipboard
open fun <T : Node?> getParentNodeOfType(clazz: Class<T>): Optional<T>
Finds the first parent of the given class.
Link copied to clipboard
open fun getRange(): Optional<Range>
Link copied to clipboard
open fun getStatement(i: Int): Statement
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
@Nullable
open fun guard(): @Nullable Expression
Link copied to clipboard
fun hashCode(): Int
Link copied to clipboard
Returns true if the parent has a parent
Link copied to clipboard
open fun hasRange(): Boolean
Link copied to clipboard
open fun hasScope(): Boolean
Link copied to clipboard
open fun isAncestorOf(descendant: Node): Boolean
Determines whether this node is an ancestor of the given node.
Link copied to clipboard
open fun isDescendantOf(ancestor: Node): Boolean
Determines whether this HasParentNode node is a descendant of the given node.
Link copied to clipboard
open fun isEmpty(): Boolean
Link copied to clipboard
open fun isPhantom(): Boolean
Link copied to clipboard
open fun isRegistered(observer: AstObserver): Boolean
Was this observer registered?
Link copied to clipboard
This is required for the ConcreteSyntaxModel, specifically to determine whether this entry uses the classic switch statement syntax (e.g.
Link copied to clipboard
@NonNull
open fun labels(): @NonNull NodeList<Expression>
Link copied to clipboard
open fun <P> notifyPropertyChange(property: ObservableProperty, oldValue: P, newValue: P)
Link copied to clipboard
open fun register(observer: AstObserver)
Register an observer.
Register a new observer for the given node.
Link copied to clipboard
open fun registerForSubtree(observer: AstObserver)
Register the observer for the current node and all the contained node and nodelists, recursively.
Link copied to clipboard
open fun remove(): Boolean
Try to remove this node from the parent
open fun remove(node: Node): Boolean
Link copied to clipboard
open fun removeComment(): Node
Link copied to clipboard
open fun removeData(key: DataKey<out Any>)
Remove data by key.
Link copied to clipboard
open fun removeForced()
Forcibly removes this node from the AST.
Link copied to clipboard
Link copied to clipboard
open fun removeOrphanComment(comment: Comment): Boolean
Link copied to clipboard
open fun replace(node: Node): Boolean
Try to replace this node in the parent with the supplied node.
open fun replace(node: Node, replacementNode: Node): Boolean
Link copied to clipboard
open fun setAssociatedSpecificationComments(@Nullable associatedSpecificationComments: @Nullable NodeList<Comment>): Node
Link copied to clipboard
fun setBlockComment(comment: String): Node
Use this to store additional information to this node.
Link copied to clipboard
open fun setComment(comment: Comment): Node
Use this to store additional information to this node.
Link copied to clipboard
open fun <M> setData(key: DataKey<M>, object: M)
Sets data for this node using the given key.
Link copied to clipboard
open fun setGuard(@Nullable guard: @Nullable Expression): SwitchEntry
Link copied to clipboard
fun setLineComment(comment: String): Node
Use this to store additional information to this node.
Link copied to clipboard
open fun setStatement(i: Int, statement: Statement): N
Link copied to clipboard
abstract fun setStatements(statements: NodeList<Statement>): N
Link copied to clipboard
@NonNull
open fun statements(): @NonNull NodeList<Statement>
Link copied to clipboard
open fun stream(): Stream<Node>
Make a stream of nodes using pre-order traversal.
open fun stream(traversal: Node.TreeTraversal): Stream<Node>
Make a stream of nodes using traversal algorithm "traversal".
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
@NonNull
open fun type(): @NonNull SwitchEntry.Type
Link copied to clipboard
open fun unregister(observer: AstObserver)
Unregister an observer.
Link copied to clipboard
open fun walk(consumer: Consumer<Node>)
Walks the AST, calling the consumer for every node with pre-order traversal.
open fun walk(traversal: Node.TreeTraversal, consumer: Consumer<Node>)
Walks the AST, calling the consumer for every node, with traversal algorithm "traversal".
open fun <T : Node?> walk(nodeType: Class<T>, consumer: Consumer<T>)
Walks the AST with pre-order traversal, calling the consumer for every node of type "nodeType".