| 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 | * Break statement AST node type. |
| 22 | * |
| 23 | * <pre> |
| 24 | * BreakStatement: |
| 25 | * <b>break</b> [ Identifier ] <b>;</b> |
| 26 | * |
| 27 | * Break statement allows expression as part of Java 12 preview feature (JEP 325) |
| 28 | * <b>break</b> <b>{ Identifier | Expression }</b> |
| 29 | * </pre> |
| 30 | * |
| 31 | * @since 2.0 |
| 32 | * @noinstantiate This class is not intended to be instantiated by clients. |
| 33 | */ |
| 34 | @SuppressWarnings("rawtypes") |
| 35 | public class BreakStatement extends Statement { |
| 36 | |
| 37 | /** |
| 38 | * The "label" structural property of this node type (child type: {@link SimpleName}). |
| 39 | * @since 3.0 |
| 40 | */ |
| 41 | public static final ChildPropertyDescriptor LABEL_PROPERTY = |
| 42 | new ChildPropertyDescriptor(BreakStatement.class, "label", SimpleName.class, OPTIONAL, NO_CYCLE_RISK); //$NON-NLS-1$ |
| 43 | |
| 44 | /** |
| 45 | * The "expression" structural property of this node type (child type: {@link Expression}). (added in JEP 325). |
| 46 | * @noreference This property is not intended to be referenced by clients as it is a part of Java preview feature. |
| 47 | * @deprecated |
| 48 | * @since 3.18 |
| 49 | */ |
| 50 | public static final ChildPropertyDescriptor EXPRESSION_PROPERTY = |
| 51 | new ChildPropertyDescriptor(BreakStatement.class, "expression", Expression.class, OPTIONAL, NO_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 | /** |
| 61 | * <code>true</code> indicates implicit and <code>false</code> indicates not implicit. |
| 62 | */ |
| 63 | private boolean isImplicit = false; |
| 64 | |
| 65 | static { |
| 66 | List properyList = new ArrayList(2); |
| 67 | createPropertyList(BreakStatement.class, properyList); |
| 68 | addProperty(LABEL_PROPERTY, properyList); |
| 69 | PROPERTY_DESCRIPTORS = reapPropertyList(properyList); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns a list of structural property descriptors for this node type. |
| 74 | * Clients must not modify the result. |
| 75 | * |
| 76 | * @param apiLevel the API level; one of the |
| 77 | * <code>AST.JLS*</code> constants |
| 78 | |
| 79 | * @return a list of property descriptors (element type: |
| 80 | * {@link StructuralPropertyDescriptor}) |
| 81 | * @since 3.0 |
| 82 | */ |
| 83 | public static List propertyDescriptors(int apiLevel) { |
| 84 | return PROPERTY_DESCRIPTORS; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Returns a list of structural property descriptors for this node type. |
| 89 | * Clients must not modify the result. |
| 90 | * |
| 91 | * @param apiLevel the API level; one of the |
| 92 | * <code>AST.JLS*</code> constants |
| 93 | * @param previewEnabled the previewEnabled flag |
| 94 | |
| 95 | * @return a list of property descriptors (element type: |
| 96 | * {@link StructuralPropertyDescriptor}) |
| 97 | * @noreference This method is not intended to be referenced by clients as it is a part of Java preview feature. |
| 98 | * @deprecated |
| 99 | * @since 3.20 |
| 100 | */ |
| 101 | public static List propertyDescriptors(int apiLevel, boolean previewEnabled) { |
| 102 | return PROPERTY_DESCRIPTORS; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * The label, or <code>null</code> if none; none by default. |
| 107 | */ |
| 108 | private SimpleName optionalLabel = null; |
| 109 | |
| 110 | /** |
| 111 | * The expression; <code>null</code> for none |
| 112 | */ |
| 113 | private Expression optionalExpression = null; |
| 114 | |
| 115 | /** |
| 116 | * Creates a new unparented break statement node owned by the given |
| 117 | * AST. By default, the break statement has no label/identifier/expression and is not implicit. |
| 118 | * <p> |
| 119 | * N.B. This constructor is package-private. |
| 120 | * </p> |
| 121 | * |
| 122 | * @param ast the AST that is to own this node |
| 123 | */ |
| 124 | BreakStatement(AST ast) { |
| 125 | super(ast); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | final List internalStructuralPropertiesForType(int apiLevel) { |
| 130 | return propertyDescriptors(apiLevel); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { |
| 135 | if (property == LABEL_PROPERTY) { |
| 136 | if (get) { |
| 137 | return getLabel(); |
| 138 | } else { |
| 139 | setLabel((SimpleName) child); |
| 140 | return null; |
| 141 | } |
| 142 | } |
| 143 | if (property == EXPRESSION_PROPERTY) { |
| 144 | if (get) { |
| 145 | return getExpression(); |
| 146 | } else { |
| 147 | setExpression((Expression) child); |
| 148 | return null; |
| 149 | } |
| 150 | } |
| 151 | // allow default implementation to flag the error |
| 152 | return super.internalGetSetChildProperty(property, get, child); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | final int getNodeType0() { |
| 157 | return BREAK_STATEMENT; |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | ASTNode clone0(AST target) { |
| 162 | BreakStatement result = new BreakStatement(target); |
| 163 | result.setSourceRange(getStartPosition(), getLength()); |
| 164 | result.copyLeadingComment(this); |
| 165 | result.setLabel((SimpleName) ASTNode.copySubtree(target, getLabel())); |
| 166 | return result; |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
| 171 | // dispatch to correct overloaded match method |
| 172 | return matcher.match(this, other); |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | void accept0(ASTVisitor visitor) { |
| 177 | boolean visitChildren = visitor.visit(this); |
| 178 | if (visitChildren) { |
| 179 | acceptChild(visitor, getLabel()); |
| 180 | } |
| 181 | visitor.endVisit(this); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Returns the label of this break statement, or <code>null</code> if |
| 186 | * there is none. |
| 187 | * |
| 188 | * @return the label, or <code>null</code> if there is none |
| 189 | */ |
| 190 | public SimpleName getLabel() { |
| 191 | return this.optionalLabel; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Sets or clears the label of this break statement. |
| 196 | * |
| 197 | * @param label the label, or <code>null</code> if |
| 198 | * there is none |
| 199 | * @exception IllegalArgumentException if: |
| 200 | * <ul> |
| 201 | * <li>the node belongs to a different AST</li> |
| 202 | * <li>the node already has a parent</li> |
| 203 | * </ul> |
| 204 | */ |
| 205 | public void setLabel(SimpleName label) { |
| 206 | ASTNode oldChild = this.optionalLabel; |
| 207 | preReplaceChild(oldChild, label, LABEL_PROPERTY); |
| 208 | this.optionalLabel = label; |
| 209 | postReplaceChild(oldChild, label, LABEL_PROPERTY); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Returns the expression of this break statement, or <code>null</code> if |
| 214 | * there is none. |
| 215 | * |
| 216 | * @return the expression, or <code>null</code> if there is none |
| 217 | * @exception UnsupportedOperationException if this operation is used other than JLS12 |
| 218 | * @noreference This method is not intended to be referenced by clients as it is a part of Java preview feature. |
| 219 | * @nooverride This method is not intended to be re-implemented or extended by clients as it is a part of Java preview feature. |
| 220 | * @deprecated |
| 221 | * @since 3.18 |
| 222 | */ |
| 223 | public Expression getExpression() { |
| 224 | // optionalExpression can be null |
| 225 | supportedOnlyIn12(); |
| 226 | return this.optionalExpression; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Sets or clears the expression of this break statement. |
| 231 | * |
| 232 | * @param expression the expression, or <code>null</code> if |
| 233 | * there is none |
| 234 | * @exception IllegalArgumentException if: |
| 235 | * <ul> |
| 236 | * <li>the node belongs to a different AST</li> |
| 237 | * <li>the node already has a parent</li> |
| 238 | * </ul> |
| 239 | * @exception UnsupportedOperationException if this operation is used other than JLS12 |
| 240 | * @noreference This method is not intended to be referenced by clients as it is a part of Java preview feature. |
| 241 | * @nooverride This method is not intended to be re-implemented or extended by clients as it is a part of Java preview feature. |
| 242 | * @deprecated |
| 243 | * @since 3.18 |
| 244 | */ |
| 245 | public void setExpression(Expression expression) { |
| 246 | supportedOnlyIn12(); |
| 247 | ASTNode oldChild = this.optionalExpression; |
| 248 | preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY); |
| 249 | this.optionalExpression = expression; |
| 250 | postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Gets the isImplicit of this break statement as <code>true</code> or <code>false</code>. |
| 255 | *<code>true</code> indicates implicit and <code>false</code> indicates not implicit. |
| 256 | * |
| 257 | * @return isImplicit <code>true</code> or <code>false</code> |
| 258 | * @exception UnsupportedOperationException if this operation is used other than JLS12 |
| 259 | * @noreference This method is not intended to be referenced by clients as it is a part of Java preview feature. |
| 260 | * @nooverride This method is not intended to be re-implemented or extended by clients as it is a part of Java preview feature. |
| 261 | * @deprecated |
| 262 | * @since 3.18 |
| 263 | */ |
| 264 | public boolean isImplicit() { |
| 265 | supportedOnlyIn12(); |
| 266 | return this.isImplicit; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Sets the isImplicit of this break statement as <code>true</code> or <code>false</code>. |
| 271 | * <code>true</code> indicates implicit and <code>false</code> indicates not implicit. This flag is |
| 272 | * generated by compiler and is not expected to be set by client. |
| 273 | |
| 274 | * @param isImplicit <code>true</code> or <code>false</code> |
| 275 | * @exception UnsupportedOperationException if this operation is used other than JLS12 |
| 276 | * @deprecated |
| 277 | * @since 3.18 |
| 278 | */ |
| 279 | void setImplicit(boolean isImplicit) { |
| 280 | supportedOnlyIn12(); |
| 281 | this.isImplicit = isImplicit; |
| 282 | } |
| 283 | |
| 284 | @Override |
| 285 | int memSize() { |
| 286 | return super.memSize() + 2 * 4; |
| 287 | } |
| 288 | |
| 289 | @Override |
| 290 | int treeSize() { |
| 291 | return |
| 292 | memSize() |
| 293 | + (this.optionalLabel == null ? 0 : getLabel().treeSize()) |
| 294 | + (this.optionalExpression == null ? 0 : getExpression().treeSize()); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 |
Members