Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/SwitchCase.java')
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/SwitchCase.java148
1 files changed, 140 insertions, 8 deletions
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/SwitchCase.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/SwitchCase.java
index c805ceac10..9d10a597b7 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/SwitchCase.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/SwitchCase.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2013 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -23,8 +23,12 @@ import java.util.List;
* <p>
* <pre>
* SwitchCase:
- * <b>case</b> Expression <b>:</b>
- * <b>default</b> <b>:</b>
+ * <b>case</b> Expression <b>:</b>
+ * <b>default</b> <b>:</b>
+ *
+ * Switch case allows multiple expressions and '->' as part of Java 12 preview feature (JEP 325)
+ * <b>case</b> [ Expression { <b>,</b> Expression } ] <b>{ : | ->}</b>
+ * <b>default</b> <b>{ : | ->}</b>
* </pre>
* </p>
*
@@ -37,22 +41,50 @@ public class SwitchCase extends Statement {
/**
* The "expression" structural property of this node type (child type: {@link Expression}).
* @since 3.0
+ * @deprecated In the JLS 12 15.28.1 API, this property is replaced by {@link #EXPRESSIONS2_PROPERTY }.
*/
public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
new ChildPropertyDescriptor(SwitchCase.class, "expression", Expression.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
/**
+ * The "expression" structural property of this node type (child type: {@link Expression}). (added in JEP 325).
+ * @since 3.17
+ */
+ public static final ChildListPropertyDescriptor EXPRESSIONS2_PROPERTY =
+ new ChildListPropertyDescriptor(SwitchCase.class, "expression", Expression.class, CYCLE_RISK); //$NON-NLS-1$);
+
+ /**
+ * The "switchLabeledRule" structural property of this node type (type: {@link Boolean}).
+ * @since 3.17
+ */
+ public static final SimplePropertyDescriptor SWITCH_LABELED_RULE_PROPERTY =
+ new SimplePropertyDescriptor(SwitchCase.class, "switchLabeledRule", boolean.class, MANDATORY); //$NON-NLS-1$
+
+ /**
* A list of property descriptors (element type:
* {@link StructuralPropertyDescriptor}),
* or null if uninitialized.
*/
private static final List PROPERTY_DESCRIPTORS;
+
+ /**
+ * A list of property descriptors (element type:
+ * {@link StructuralPropertyDescriptor}),
+ * or null if uninitialized.
+ */
+ private static final List PROPERTY_DESCRIPTORS_12;
static {
List propertyList = new ArrayList(2);
createPropertyList(SwitchCase.class, propertyList);
addProperty(EXPRESSION_PROPERTY, propertyList);
PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
+
+ propertyList = new ArrayList(2);
+ createPropertyList(SwitchCase.class, propertyList);
+ addProperty(EXPRESSIONS2_PROPERTY , propertyList);
+ addProperty(SWITCH_LABELED_RULE_PROPERTY, propertyList);
+ PROPERTY_DESCRIPTORS_12 = reapPropertyList(propertyList);
}
/**
@@ -66,6 +98,9 @@ public class SwitchCase extends Statement {
* @since 3.0
*/
public static List propertyDescriptors(int apiLevel) {
+ if (apiLevel >= AST.JLS12_INTERNAL) {
+ return PROPERTY_DESCRIPTORS_12;
+ }
return PROPERTY_DESCRIPTORS;
}
@@ -75,6 +110,17 @@ public class SwitchCase extends Statement {
* @see #expressionInitialized
*/
private Expression optionalExpression = null;
+
+ /**
+ * <code>true</code> indicates "->" and <code>false</code> indicates ":".
+ */
+ private boolean switchLabeledRule = false;
+
+
+ /**
+ * The expression list; <code>empty</code> for none;
+ */
+ private ASTNode.NodeList expressions = null;
/**
* Indicates whether <code>optionalExpression</code> has been initialized.
@@ -83,12 +129,16 @@ public class SwitchCase extends Statement {
/**
* Creates a new AST node for a switch case pseudo-statement owned by the
- * given AST. By default, there is an unspecified, but legal, expression.
+ * given AST. By default, there is no expression, but legal, and switchLabeledRule
+ * is false which indicates ":".
*
* @param ast the AST that is to own this node
*/
SwitchCase(AST ast) {
super(ast);
+ if (ast.apiLevel >= AST.JLS12_INTERNAL) {
+ this.expressions = new ASTNode.NodeList(EXPRESSIONS2_PROPERTY );
+ }
}
@Override
@@ -97,6 +147,20 @@ public class SwitchCase extends Statement {
}
@Override
+ final boolean internalGetSetBooleanProperty(SimplePropertyDescriptor property, boolean get, boolean value) {
+ if (property == SWITCH_LABELED_RULE_PROPERTY) {
+ if (get) {
+ return isSwitchLabeledRule();
+ } else {
+ setSwitchLabeledRule(value);
+ return false;
+ }
+ }
+ // allow default implementation to flag the error
+ return super.internalGetSetBooleanProperty(property, get, value);
+ }
+
+ @Override
final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
if (property == EXPRESSION_PROPERTY) {
if (get) {
@@ -111,17 +175,32 @@ public class SwitchCase extends Statement {
}
@Override
+ final List internalGetChildListProperty(ChildListPropertyDescriptor property) {
+ if (property == EXPRESSIONS2_PROPERTY ) {
+ return expressions();
+ }
+ // allow default implementation to flag the error
+ return super.internalGetChildListProperty(property);
+ }
+
+ @Override
final int getNodeType0() {
return SWITCH_CASE;
}
+ @SuppressWarnings("unchecked")
@Override
ASTNode clone0(AST target) {
SwitchCase result = new SwitchCase(target);
result.setSourceRange(getStartPosition(), getLength());
result.copyLeadingComment(this);
- result.setExpression(
- (Expression) ASTNode.copySubtree(target, getExpression()));
+ if (this.ast.apiLevel >= AST.JLS12_INTERNAL) {
+ result.expressions().addAll(
+ ASTNode.copySubtrees(target, expressions()));
+ } else {
+ result.setExpression(
+ (Expression) ASTNode.copySubtree(target, getExpression()));
+ }
return result;
}
@@ -135,7 +214,11 @@ public class SwitchCase extends Statement {
void accept0(ASTVisitor visitor) {
boolean visitChildren = visitor.visit(this);
if (visitChildren) {
- acceptChild(visitor, getExpression());
+ if (this.ast.apiLevel >= AST.JLS12_INTERNAL) {
+ acceptChildren(visitor, this.expressions);
+ } else {
+ acceptChild(visitor, getExpression());
+ }
}
visitor.endVisit(this);
}
@@ -145,6 +228,7 @@ public class SwitchCase extends Statement {
* <code>null</code> if there is none (the "default:" case).
*
* @return the expression node, or <code>null</code> if there is none
+ * @deprecated use expressions() (see JLS 12)
*/
public Expression getExpression() {
if (!this.expressionInitialized) {
@@ -160,6 +244,22 @@ public class SwitchCase extends Statement {
}
return this.optionalExpression;
}
+
+ /**
+ * Returns the list of expressions of this switch case, or
+ * <code>empty</code> if there is none (the "default:" case).
+ *
+ * @return the list of expression nodes
+ * (element type: {@link Expression})
+ * @exception UnsupportedOperationException if this operation is used below JLS12
+ * @since 3.17
+ */
+ public List expressions() {
+ if (this.expressions == null) {
+ unsupportedBelow12();
+ }
+ return this.expressions;
+ }
/**
* Sets the expression of this switch case, or clears it (turns it into
@@ -173,6 +273,7 @@ public class SwitchCase extends Statement {
* <li>the node already has a parent</li>
* <li>a cycle in would be created</li>
* </ul>
+ * @deprecated see JLS 12
*/
public void setExpression(Expression expression) {
ASTNode oldChild = this.optionalExpression;
@@ -181,18 +282,49 @@ public class SwitchCase extends Statement {
this.expressionInitialized = true;
postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
}
+
+ /**
+ * Sets the switchLabeledRule of this switch case as <code>true</code> or <code>false</code>.
+ * <code>true</code> indicates "->" and <code>false</code> indicates ":".
+
+ * @param switchLabeledRule <code>true</code> or <code>false</code>
+ * @exception UnsupportedOperationException if this operation is used below JLS12
+ * @since 3.17
+ */
+ public void setSwitchLabeledRule(boolean switchLabeledRule) {
+ unsupportedBelow12();
+ preValueChange(SWITCH_LABELED_RULE_PROPERTY);
+ this.switchLabeledRule = switchLabeledRule;
+ postValueChange(SWITCH_LABELED_RULE_PROPERTY);
+ }
+
+ /**
+ * Gets the switchLabeledRule of this switch case as <code>true</code> or <code>false</code>.
+ *<code>true</code> indicates "->" and <code>false</code> indicates ":".
+ *
+ * @return switchLabeledRule <code>true</code> or <code>false</code>
+ * @exception UnsupportedOperationException if this operation is used below JLS12
+ * @since 3.17
+ */
+ public boolean isSwitchLabeledRule() {
+ unsupportedBelow12();
+ return this.switchLabeledRule;
+ }
/**
* Returns whether this switch case represents the "default:" case.
* <p>
* This convenience method is equivalent to
- * <code>getExpression() == null</code>.
+ * <code>getExpression() == null</code> or <code>expressions().isEmpty()</code>.
* </p>
*
* @return <code>true</code> if this is the default switch case, and
* <code>false</code> if this is a non-default switch case
*/
public boolean isDefault() {
+ if (this.ast.apiLevel >= AST.JLS12_INTERNAL) {
+ return expressions().isEmpty();
+ }
return getExpression() == null;
}

Back to the top