| 1 | /******************************************************************************* |
|---|---|
| 2 | * Copyright (c) 2000, 2013 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 | * Conditional expression AST node type. |
| 22 | * |
| 23 | * <pre> |
| 24 | * ConditionalExpression: |
| 25 | * Expression <b>?</b> Expression <b>:</b> Expression |
| 26 | * </pre> |
| 27 | * |
| 28 | * @since 2.0 |
| 29 | * @noinstantiate This class is not intended to be instantiated by clients. |
| 30 | */ |
| 31 | @SuppressWarnings("rawtypes") |
| 32 | public class ConditionalExpression extends Expression { |
| 33 | |
| 34 | /** |
| 35 | * The "expression" structural property of this node type (child type: {@link Expression}). |
| 36 | * @since 3.0 |
| 37 | */ |
| 38 | public static final ChildPropertyDescriptor EXPRESSION_PROPERTY = |
| 39 | new ChildPropertyDescriptor(ConditionalExpression.class, "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$ |
| 40 | |
| 41 | /** |
| 42 | * The "thenExpression" structural property of this node type (child type: {@link Expression}). |
| 43 | * @since 3.0 |
| 44 | */ |
| 45 | public static final ChildPropertyDescriptor THEN_EXPRESSION_PROPERTY = |
| 46 | new ChildPropertyDescriptor(ConditionalExpression.class, "thenExpression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$ |
| 47 | |
| 48 | /** |
| 49 | * The "elseExpression" structural property of this node type (child type: {@link Expression}). |
| 50 | * @since 3.0 |
| 51 | */ |
| 52 | public static final ChildPropertyDescriptor ELSE_EXPRESSION_PROPERTY = |
| 53 | new ChildPropertyDescriptor(ConditionalExpression.class, "elseExpression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$ |
| 54 | |
| 55 | /** |
| 56 | * A list of property descriptors (element type: |
| 57 | * {@link StructuralPropertyDescriptor}), |
| 58 | * or null if uninitialized. |
| 59 | */ |
| 60 | private static final List PROPERTY_DESCRIPTORS; |
| 61 | |
| 62 | static { |
| 63 | List properyList = new ArrayList(4); |
| 64 | createPropertyList(ConditionalExpression.class, properyList); |
| 65 | addProperty(EXPRESSION_PROPERTY, properyList); |
| 66 | addProperty(THEN_EXPRESSION_PROPERTY, properyList); |
| 67 | addProperty(ELSE_EXPRESSION_PROPERTY, properyList); |
| 68 | PROPERTY_DESCRIPTORS = reapPropertyList(properyList); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns a list of structural property descriptors for this node type. |
| 73 | * Clients must not modify the result. |
| 74 | * |
| 75 | * @param apiLevel the API level; one of the |
| 76 | * <code>AST.JLS*</code> constants |
| 77 | |
| 78 | * @return a list of property descriptors (element type: |
| 79 | * {@link StructuralPropertyDescriptor}) |
| 80 | * @since 3.0 |
| 81 | */ |
| 82 | public static List propertyDescriptors(int apiLevel) { |
| 83 | return PROPERTY_DESCRIPTORS; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * The condition expression; lazily initialized; defaults to an unspecified, |
| 88 | * but legal, expression. |
| 89 | */ |
| 90 | private Expression conditionExpression = null; |
| 91 | |
| 92 | /** |
| 93 | * The "then" expression; lazily initialized; defaults to an unspecified, |
| 94 | * but legal, expression. |
| 95 | */ |
| 96 | private Expression thenExpression = null; |
| 97 | |
| 98 | /** |
| 99 | * The "else" expression; lazily initialized; defaults to an unspecified, |
| 100 | * but legal, expression. |
| 101 | */ |
| 102 | private Expression elseExpression = null; |
| 103 | |
| 104 | /** |
| 105 | * Creates a new unparented conditional expression node owned by the given |
| 106 | * AST. By default, the condition, "then", and "else" expresssions are |
| 107 | * unspecified, but legal. |
| 108 | * <p> |
| 109 | * N.B. This constructor is package-private. |
| 110 | * </p> |
| 111 | * |
| 112 | * @param ast the AST that is to own this node |
| 113 | */ |
| 114 | ConditionalExpression(AST ast) { |
| 115 | super(ast); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | final List internalStructuralPropertiesForType(int apiLevel) { |
| 120 | return propertyDescriptors(apiLevel); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { |
| 125 | if (property == EXPRESSION_PROPERTY) { |
| 126 | if (get) { |
| 127 | return getExpression(); |
| 128 | } else { |
| 129 | setExpression((Expression) child); |
| 130 | return null; |
| 131 | } |
| 132 | } |
| 133 | if (property == THEN_EXPRESSION_PROPERTY) { |
| 134 | if (get) { |
| 135 | return getThenExpression(); |
| 136 | } else { |
| 137 | setThenExpression((Expression) child); |
| 138 | return null; |
| 139 | } |
| 140 | } |
| 141 | if (property == ELSE_EXPRESSION_PROPERTY) { |
| 142 | if (get) { |
| 143 | return getElseExpression(); |
| 144 | } else { |
| 145 | setElseExpression((Expression) child); |
| 146 | return null; |
| 147 | } |
| 148 | } |
| 149 | // allow default implementation to flag the error |
| 150 | return super.internalGetSetChildProperty(property, get, child); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | final int getNodeType0() { |
| 155 | return CONDITIONAL_EXPRESSION; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | ASTNode clone0(AST target) { |
| 160 | ConditionalExpression result = new ConditionalExpression(target); |
| 161 | result.setSourceRange(getStartPosition(), getLength()); |
| 162 | result.setExpression((Expression) getExpression().clone(target)); |
| 163 | result.setThenExpression( |
| 164 | (Expression) getThenExpression().clone(target)); |
| 165 | result.setElseExpression( |
| 166 | (Expression) getElseExpression().clone(target)); |
| 167 | return result; |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
| 172 | // dispatch to correct overloaded match method |
| 173 | return matcher.match(this, other); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | void accept0(ASTVisitor visitor) { |
| 178 | boolean visitChildren = visitor.visit(this); |
| 179 | if (visitChildren) { |
| 180 | // visit children in normal left to right reading order |
| 181 | acceptChild(visitor, getExpression()); |
| 182 | acceptChild(visitor, getThenExpression()); |
| 183 | acceptChild(visitor, getElseExpression()); |
| 184 | } |
| 185 | visitor.endVisit(this); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Returns the condition of this conditional expression. |
| 190 | * |
| 191 | * @return the condition node |
| 192 | */ |
| 193 | public Expression getExpression() { |
| 194 | if (this.conditionExpression == null) { |
| 195 | // lazy init must be thread-safe for readers |
| 196 | synchronized (this) { |
| 197 | if (this.conditionExpression == null) { |
| 198 | preLazyInit(); |
| 199 | this.conditionExpression = new SimpleName(this.ast); |
| 200 | postLazyInit(this.conditionExpression, EXPRESSION_PROPERTY); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | return this.conditionExpression; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Sets the condition of this conditional expression. |
| 209 | * |
| 210 | * @param expression the condition node |
| 211 | * @exception IllegalArgumentException if: |
| 212 | * <ul> |
| 213 | * <li>the node belongs to a different AST</li> |
| 214 | * <li>the node already has a parent</li> |
| 215 | * <li>a cycle in would be created</li> |
| 216 | * </ul> |
| 217 | */ |
| 218 | public void setExpression(Expression expression) { |
| 219 | if (expression == null) { |
| 220 | throw new IllegalArgumentException(); |
| 221 | } |
| 222 | ASTNode oldChild = this.conditionExpression; |
| 223 | preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY); |
| 224 | this.conditionExpression = expression; |
| 225 | postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Returns the "then" part of this conditional expression. |
| 230 | * |
| 231 | * @return the "then" expression node |
| 232 | */ |
| 233 | public Expression getThenExpression() { |
| 234 | if (this.thenExpression == null) { |
| 235 | // lazy init must be thread-safe for readers |
| 236 | synchronized (this) { |
| 237 | if (this.thenExpression == null) { |
| 238 | preLazyInit(); |
| 239 | this.thenExpression = new SimpleName(this.ast); |
| 240 | postLazyInit(this.thenExpression, THEN_EXPRESSION_PROPERTY); |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | return this.thenExpression; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Sets the "then" part of this conditional expression. |
| 249 | * |
| 250 | * @param expression the "then" expression node |
| 251 | * @exception IllegalArgumentException if: |
| 252 | * <ul> |
| 253 | * <li>the node belongs to a different AST</li> |
| 254 | * <li>the node already has a parent</li> |
| 255 | * <li>a cycle in would be created</li> |
| 256 | * </ul> |
| 257 | */ |
| 258 | public void setThenExpression(Expression expression) { |
| 259 | if (expression == null) { |
| 260 | throw new IllegalArgumentException(); |
| 261 | } |
| 262 | ASTNode oldChild = this.thenExpression; |
| 263 | preReplaceChild(oldChild, expression, THEN_EXPRESSION_PROPERTY); |
| 264 | this.thenExpression = expression; |
| 265 | postReplaceChild(oldChild, expression, THEN_EXPRESSION_PROPERTY); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Returns the "else" part of this conditional expression. |
| 270 | * |
| 271 | * @return the "else" expression node |
| 272 | */ |
| 273 | public Expression getElseExpression() { |
| 274 | if (this.elseExpression == null) { |
| 275 | // lazy init must be thread-safe for readers |
| 276 | synchronized (this) { |
| 277 | if (this.elseExpression == null) { |
| 278 | preLazyInit(); |
| 279 | this.elseExpression = new SimpleName(this.ast); |
| 280 | postLazyInit(this.elseExpression, ELSE_EXPRESSION_PROPERTY); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | return this.elseExpression; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Sets the "else" part of this conditional expression. |
| 289 | * |
| 290 | * @param expression the "else" expression node |
| 291 | * @exception IllegalArgumentException if: |
| 292 | * <ul> |
| 293 | * <li>the node belongs to a different AST</li> |
| 294 | * <li>the node already has a parent</li> |
| 295 | * <li>a cycle in would be created</li> |
| 296 | * </ul> |
| 297 | */ |
| 298 | public void setElseExpression(Expression expression) { |
| 299 | if (expression == null) { |
| 300 | throw new IllegalArgumentException(); |
| 301 | } |
| 302 | ASTNode oldChild = this.elseExpression; |
| 303 | preReplaceChild(oldChild, expression, ELSE_EXPRESSION_PROPERTY); |
| 304 | this.elseExpression = expression; |
| 305 | postReplaceChild(oldChild, expression, ELSE_EXPRESSION_PROPERTY); |
| 306 | } |
| 307 | |
| 308 | @Override |
| 309 | int memSize() { |
| 310 | // treat Code as free |
| 311 | return BASE_NODE_SIZE + 3 * 4; |
| 312 | } |
| 313 | |
| 314 | @Override |
| 315 | int treeSize() { |
| 316 | return |
| 317 | memSize() |
| 318 | + (this.conditionExpression == null ? 0 : getExpression().treeSize()) |
| 319 | + (this.thenExpression == null ? 0 : getThenExpression().treeSize()) |
| 320 | + (this.elseExpression == null ? 0 : getElseExpression().treeSize()); |
| 321 | } |
| 322 | } |
| 323 |
Members