net.sourceforge.pmd.lang.java.ast.ASTSwitchLabel Maven / Gradle / Ivy
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.ast;
import java.util.Iterator;
import net.sourceforge.pmd.lang.ast.NodeStream;
/**
* Represents either a {@code case} or {@code default} label inside
* a {@linkplain ASTSwitchStatement switch statement} or {@linkplain ASTSwitchExpression expression}.
* Since Java 14, labels may have several expressions.
*
*
*
* SwitchLabel ::= "case" {@linkplain ASTExpression Expression} ("," {@linkplain ASTExpression Expression} )*
* | "case" "null [ "," "default" ]
* | "case" ( {@linkplain ASTTypePattern TypePattern} | {@linkplain ASTRecordPattern RecordPattern} )
* | "default"
*
*
*
* Note: case null and the case patterns are a Java 21 language feature
*
* @see JEP 441: Pattern Matching for switch
* @see JEP 432: Record Patterns (Second Preview)
*/
public final class ASTSwitchLabel extends AbstractJavaNode implements Iterable {
private boolean isDefault;
ASTSwitchLabel(int id) {
super(id);
}
void setDefault() {
isDefault = true;
}
/** Returns true if this is the {@code default} label. */
// todo `case default`
public boolean isDefault() {
return isDefault;
}
/**
* Returns the expressions of this label, or an empty list if this
* is the default label. This may contain {@linkplain ASTPatternExpression pattern expressions}
* to represent patterns.
*/
public NodeStream getExprList() {
return children(ASTExpression.class);
}
@Override
protected R acceptVisitor(JavaVisitor super P, ? extends R> visitor, P data) {
return visitor.visit(this, data);
}
@Override
public Iterator iterator() {
return children(ASTExpression.class).iterator();
}
}