| 1 | /******************************************************************************* |
|---|---|
| 2 | * Copyright (c) 2021 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 | * IBM Corporation - initial API and implementation |
| 12 | *******************************************************************************/ |
| 13 | |
| 14 | package org.eclipse.jdt.core.dom; |
| 15 | |
| 16 | import java.util.ArrayList; |
| 17 | import java.util.List; |
| 18 | |
| 19 | /** |
| 20 | * PatternInstanceof expression AST node type. |
| 21 | * <pre> |
| 22 | * PatternInstanceofExpression: |
| 23 | * Expression <b>instanceof</b> Variable |
| 24 | * </pre> |
| 25 | * |
| 26 | * @noinstantiate This class is not intended to be instantiated by clients. |
| 27 | */ |
| 28 | @SuppressWarnings("rawtypes") |
| 29 | public class PatternInstanceofExpression extends Expression { |
| 30 | |
| 31 | /** |
| 32 | * The "leftOperand" structural property of this node type (child type: {@link Expression}). |
| 33 | */ |
| 34 | public static final ChildPropertyDescriptor LEFT_OPERAND_PROPERTY = |
| 35 | new ChildPropertyDescriptor(PatternInstanceofExpression.class, "leftOperand", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$ |
| 36 | |
| 37 | /** |
| 38 | * The "rightOperand" structural property of this node type (child type: {@link SingleVariableDeclaration}). |
| 39 | */ |
| 40 | public static final ChildPropertyDescriptor RIGHT_OPERAND_PROPERTY = |
| 41 | new ChildPropertyDescriptor(PatternInstanceofExpression.class, "rightOperand", SingleVariableDeclaration.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$ |
| 42 | |
| 43 | /** |
| 44 | * A list of property descriptors (element type: |
| 45 | * {@link StructuralPropertyDescriptor}), |
| 46 | * or null if uninitialized. |
| 47 | */ |
| 48 | private static final List PROPERTY_DESCRIPTORS; |
| 49 | |
| 50 | static { |
| 51 | List properyList = new ArrayList(3); |
| 52 | createPropertyList(PatternInstanceofExpression.class, properyList); |
| 53 | addProperty(LEFT_OPERAND_PROPERTY, properyList); |
| 54 | addProperty(RIGHT_OPERAND_PROPERTY, properyList); |
| 55 | PROPERTY_DESCRIPTORS = reapPropertyList(properyList); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Returns a list of structural property descriptors for this node type. |
| 60 | * Clients must not modify the result. |
| 61 | * |
| 62 | * @param apiLevel the API level; one of the |
| 63 | * <code>AST.JLS*</code> constants |
| 64 | |
| 65 | * @return a list of property descriptors (element type: |
| 66 | * {@link StructuralPropertyDescriptor}) |
| 67 | * @since 3.27 |
| 68 | */ |
| 69 | public static List propertyDescriptors(int apiLevel) { |
| 70 | return PROPERTY_DESCRIPTORS; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * The left operand; lazily initialized; defaults to an unspecified, |
| 75 | * but legal, simple name. |
| 76 | */ |
| 77 | private Expression leftOperand = null; |
| 78 | |
| 79 | /** |
| 80 | * The right operand; lazily initialized; defaults to an unspecified, |
| 81 | * but legal, simple variable decalaration. |
| 82 | */ |
| 83 | private SingleVariableDeclaration rightOperand = null; |
| 84 | |
| 85 | |
| 86 | /** |
| 87 | * Creates a new AST node for an instanceof expression owned by the given |
| 88 | * AST. By default, the node has unspecified (but legal) operator, |
| 89 | * left and right operands. |
| 90 | * |
| 91 | * @param ast the AST that is to own this node |
| 92 | */ |
| 93 | PatternInstanceofExpression(AST ast) { |
| 94 | super(ast); |
| 95 | unsupportedBelow16(); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | final List internalStructuralPropertiesForType(int apiLevel) { |
| 100 | return propertyDescriptors(apiLevel); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { |
| 105 | if (property == LEFT_OPERAND_PROPERTY) { |
| 106 | if (get) { |
| 107 | return getLeftOperand(); |
| 108 | } else { |
| 109 | setLeftOperand((Expression) child); |
| 110 | return null; |
| 111 | } |
| 112 | } |
| 113 | if (property == RIGHT_OPERAND_PROPERTY) { |
| 114 | if (get) { |
| 115 | return getRightOperand(); |
| 116 | } else { |
| 117 | setRightOperand((SingleVariableDeclaration) child); |
| 118 | return null; |
| 119 | } |
| 120 | } |
| 121 | // allow default implementation to flag the error |
| 122 | return super.internalGetSetChildProperty(property, get, child); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | final int getNodeType0() { |
| 127 | return PATTERN_INSTANCEOF_EXPRESSION; |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | ASTNode clone0(AST target) { |
| 132 | PatternInstanceofExpression result = new PatternInstanceofExpression(target); |
| 133 | result.setSourceRange(getStartPosition(), getLength()); |
| 134 | result.setLeftOperand((Expression) getLeftOperand().clone(target)); |
| 135 | result.setRightOperand((SingleVariableDeclaration) getRightOperand().clone(target)); |
| 136 | return result; |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
| 141 | // dispatch to correct overloaded match method |
| 142 | return matcher.match(this, other); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | void accept0(ASTVisitor visitor) { |
| 147 | boolean visitChildren = visitor.visit(this); |
| 148 | if (visitChildren) { |
| 149 | // visit children in normal left to right reading order |
| 150 | acceptChild(visitor, getLeftOperand()); |
| 151 | acceptChild(visitor, getRightOperand()); |
| 152 | } |
| 153 | visitor.endVisit(this); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Returns the left operand of this Patterninstanceof expression. |
| 158 | * |
| 159 | * @return the left operand node |
| 160 | * @since 3.27 |
| 161 | */ |
| 162 | public Expression getLeftOperand() { |
| 163 | if (this.leftOperand == null) { |
| 164 | // lazy init must be thread-safe for readers |
| 165 | synchronized (this) { |
| 166 | if (this.leftOperand == null) { |
| 167 | preLazyInit(); |
| 168 | this.leftOperand= new SimpleName(this.ast); |
| 169 | postLazyInit(this.leftOperand, LEFT_OPERAND_PROPERTY); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | return this.leftOperand; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Sets the left operand of this instanceof expression. |
| 178 | * |
| 179 | * @param expression the left operand node |
| 180 | * @exception IllegalArgumentException if: |
| 181 | * <ul> |
| 182 | * <li>the node belongs to a different AST</li> |
| 183 | * <li>the node already has a parent</li> |
| 184 | * <li>a cycle in would be created</li> |
| 185 | * </ul> |
| 186 | * @since 3.27 |
| 187 | */ |
| 188 | public void setLeftOperand(Expression expression) { |
| 189 | if (expression == null) { |
| 190 | throw new IllegalArgumentException(); |
| 191 | } |
| 192 | ASTNode oldChild = this.leftOperand; |
| 193 | preReplaceChild(oldChild, expression, LEFT_OPERAND_PROPERTY); |
| 194 | this.leftOperand = expression; |
| 195 | postReplaceChild(oldChild, expression, LEFT_OPERAND_PROPERTY); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Returns the right operand of this instanceof expression. |
| 200 | * |
| 201 | * @return the right operand node |
| 202 | * @since 3.27 |
| 203 | */ |
| 204 | public SingleVariableDeclaration getRightOperand() { |
| 205 | if (this.rightOperand == null) { |
| 206 | // lazy init must be thread-safe for readers |
| 207 | synchronized (this) { |
| 208 | if (this.rightOperand == null) { |
| 209 | preLazyInit(); |
| 210 | this.rightOperand= new SingleVariableDeclaration(this.ast); |
| 211 | postLazyInit(this.rightOperand, RIGHT_OPERAND_PROPERTY); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | return this.rightOperand; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Sets the right operand of this instanceof expression. |
| 220 | * |
| 221 | * @param referenceDeclaration the right operand node |
| 222 | * @exception IllegalArgumentException if: |
| 223 | * <ul> |
| 224 | * <li>the node belongs to a different AST</li> |
| 225 | * <li>the node already has a parent</li> |
| 226 | * <li>a cycle in would be created</li> |
| 227 | * </ul> |
| 228 | * @since 3.27 |
| 229 | */ |
| 230 | public void setRightOperand(SingleVariableDeclaration referenceDeclaration) { |
| 231 | if (referenceDeclaration == null) { |
| 232 | throw new IllegalArgumentException(); |
| 233 | } |
| 234 | ASTNode oldChild = this.rightOperand; |
| 235 | preReplaceChild(oldChild, referenceDeclaration, RIGHT_OPERAND_PROPERTY); |
| 236 | this.rightOperand = referenceDeclaration; |
| 237 | postReplaceChild(oldChild, referenceDeclaration, RIGHT_OPERAND_PROPERTY); |
| 238 | } |
| 239 | |
| 240 | |
| 241 | @Override |
| 242 | int memSize() { |
| 243 | // treat Operator as free |
| 244 | return BASE_NODE_SIZE + 2 * 4; |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | int treeSize() { |
| 249 | return |
| 250 | memSize() |
| 251 | + (this.leftOperand == null ? 0 : getLeftOperand().treeSize()) |
| 252 | + (this.rightOperand == null ? 0 : getRightOperand().treeSize()); |
| 253 | } |
| 254 | } |
| 255 |
Members