| 1 | /******************************************************************************* |
|---|---|
| 2 | * Copyright (c) 2000, 2019 IBM Corporation and others. |
| 3 | * |
| 4 | * This program and the accompanying materials |
| 5 | * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | * which accompanies this distribution, and is available at |
| 7 | * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | * |
| 9 | * SPDX-License-Identifier: EPL-2.0 |
| 10 | * |
| 11 | * Contributors: |
| 12 | * IBM Corporation - initial API and implementation |
| 13 | *******************************************************************************/ |
| 14 | |
| 15 | package org.eclipse.jdt.core.dom; |
| 16 | |
| 17 | import java.util.ArrayList; |
| 18 | import java.util.List; |
| 19 | |
| 20 | /** |
| 21 | * Switch statement AST node type. |
| 22 | * <pre> |
| 23 | * SwitchStatement: |
| 24 | * <b>switch</b> <b>(</b> Expression <b>)</b> |
| 25 | * <b>{</b> { SwitchCase | Statement } <b>}</b> |
| 26 | * SwitchCase: |
| 27 | * <b>case</b> Expression <b>:</b> |
| 28 | * <b>default</b> <b>:</b> |
| 29 | * </pre> |
| 30 | * <code>SwitchCase</code> nodes are treated as a kind of |
| 31 | * <code>Statement</code>. |
| 32 | * |
| 33 | * @since 2.0 |
| 34 | * @noinstantiate This class is not intended to be instantiated by clients. |
| 35 | */ |
| 36 | @SuppressWarnings({"rawtypes", "unchecked"}) |
| 37 | public class SwitchStatement extends Statement { |
| 38 | |
| 39 | /** |
| 40 | * The "expression" structural property of this node type (child type: {@link Expression}). |
| 41 | * @since 3.0 |
| 42 | */ |
| 43 | public static final ChildPropertyDescriptor EXPRESSION_PROPERTY = |
| 44 | new ChildPropertyDescriptor(SwitchStatement.class, "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$ |
| 45 | |
| 46 | /** |
| 47 | * The "statements" structural property of this node type (element type: {@link Statement}). |
| 48 | * @since 3.0 |
| 49 | */ |
| 50 | public static final ChildListPropertyDescriptor STATEMENTS_PROPERTY = |
| 51 | new ChildListPropertyDescriptor(SwitchStatement.class, "statements", Statement.class, CYCLE_RISK); //$NON-NLS-1$ |
| 52 | |
| 53 | /** |
| 54 | * A list of property descriptors (element type: |
| 55 | * {@link StructuralPropertyDescriptor}), |
| 56 | * or null if uninitialized. |
| 57 | */ |
| 58 | private static final List PROPERTY_DESCRIPTORS; |
| 59 | |
| 60 | static { |
| 61 | List propertyList = new ArrayList(3); |
| 62 | createPropertyList(SwitchStatement.class, propertyList); |
| 63 | addProperty(EXPRESSION_PROPERTY, propertyList); |
| 64 | addProperty(STATEMENTS_PROPERTY, propertyList); |
| 65 | PROPERTY_DESCRIPTORS = reapPropertyList(propertyList); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns a list of structural property descriptors for this node type. |
| 70 | * Clients must not modify the result. |
| 71 | * |
| 72 | * @param apiLevel the API level; one of the |
| 73 | * <code>AST.JLS*</code> constants |
| 74 | * @return a list of property descriptors (element type: |
| 75 | * {@link StructuralPropertyDescriptor}) |
| 76 | * @since 3.0 |
| 77 | */ |
| 78 | public static List propertyDescriptors(int apiLevel) { |
| 79 | return PROPERTY_DESCRIPTORS; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * The expression; lazily initialized; defaults to a unspecified, but legal, |
| 84 | * expression. |
| 85 | */ |
| 86 | private Expression expression = null; |
| 87 | |
| 88 | /** |
| 89 | * The statements and SwitchCase nodes |
| 90 | * (element type: {@link Statement}). |
| 91 | * Defaults to an empty list. |
| 92 | */ |
| 93 | private ASTNode.NodeList statements = |
| 94 | new ASTNode.NodeList(STATEMENTS_PROPERTY); |
| 95 | |
| 96 | /** |
| 97 | * Creates a new unparented switch statement node owned by the given |
| 98 | * AST. By default, the swicth statement has an unspecified, but legal, |
| 99 | * expression, and an empty list of switch groups. |
| 100 | * <p> |
| 101 | * N.B. This constructor is package-private. |
| 102 | * </p> |
| 103 | * |
| 104 | * @param ast the AST that is to own this node |
| 105 | */ |
| 106 | SwitchStatement(AST ast) { |
| 107 | super(ast); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | final List internalStructuralPropertiesForType(int apiLevel) { |
| 112 | return propertyDescriptors(apiLevel); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { |
| 117 | if (property == EXPRESSION_PROPERTY) { |
| 118 | if (get) { |
| 119 | return getExpression(); |
| 120 | } else { |
| 121 | setExpression((Expression) child); |
| 122 | return null; |
| 123 | } |
| 124 | } |
| 125 | // allow default implementation to flag the error |
| 126 | return super.internalGetSetChildProperty(property, get, child); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | final List internalGetChildListProperty(ChildListPropertyDescriptor property) { |
| 131 | if (property == STATEMENTS_PROPERTY) { |
| 132 | return statements(); |
| 133 | } |
| 134 | // allow default implementation to flag the error |
| 135 | return super.internalGetChildListProperty(property); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | final int getNodeType0() { |
| 140 | return SWITCH_STATEMENT; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | ASTNode clone0(AST target) { |
| 145 | SwitchStatement result = new SwitchStatement(target); |
| 146 | result.setSourceRange(getStartPosition(), getLength()); |
| 147 | result.copyLeadingComment(this); |
| 148 | result.setExpression((Expression) getExpression().clone(target)); |
| 149 | result.statements().addAll(ASTNode.copySubtrees(target, statements())); |
| 150 | return result; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
| 155 | // dispatch to correct overloaded match method |
| 156 | return matcher.match(this, other); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | void accept0(ASTVisitor visitor) { |
| 161 | boolean visitChildren = visitor.visit(this); |
| 162 | if (visitChildren) { |
| 163 | // visit children in normal left to right reading order |
| 164 | acceptChild(visitor, getExpression()); |
| 165 | acceptChildren(visitor, this.statements); |
| 166 | } |
| 167 | visitor.endVisit(this); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Returns the expression of this switch statement. |
| 172 | * |
| 173 | * @return the expression node |
| 174 | */ |
| 175 | public Expression getExpression() { |
| 176 | if (this.expression == null) { |
| 177 | // lazy init must be thread-safe for readers |
| 178 | synchronized (this) { |
| 179 | if (this.expression == null) { |
| 180 | preLazyInit(); |
| 181 | this.expression = new SimpleName(this.ast); |
| 182 | postLazyInit(this.expression, EXPRESSION_PROPERTY); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | return this.expression; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Sets the expression of this switch statement. |
| 191 | * |
| 192 | * @param expression the new expression node |
| 193 | * @exception IllegalArgumentException if: |
| 194 | * <ul> |
| 195 | * <li>the node belongs to a different AST</li> |
| 196 | * <li>the node already has a parent</li> |
| 197 | * <li>a cycle in would be created</li> |
| 198 | * </ul> |
| 199 | */ |
| 200 | public void setExpression(Expression expression) { |
| 201 | if (expression == null) { |
| 202 | throw new IllegalArgumentException(); |
| 203 | } |
| 204 | ASTNode oldChild = this.expression; |
| 205 | preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY); |
| 206 | this.expression = expression; |
| 207 | postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Returns the live ordered list of statements for this switch statement. |
| 212 | * Within this list, <code>SwitchCase</code> nodes mark the start of |
| 213 | * the switch groups. |
| 214 | * |
| 215 | * @return the live list of statement nodes |
| 216 | * (element type: {@link Statement}) |
| 217 | */ |
| 218 | public List statements() { |
| 219 | return this.statements; |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | int memSize() { |
| 224 | return super.memSize() + 2 * 4; |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | int treeSize() { |
| 229 | return |
| 230 | memSize() |
| 231 | + (this.expression == null ? 0 : getExpression().treeSize()) |
| 232 | + this.statements.listSize(); |
| 233 | } |
| 234 | } |
| 235 |
Members