| 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 | * Super constructor invocation statement AST node type. |
| 22 | * <pre> |
| 23 | * SuperConstructorInvocation: |
| 24 | * [ Expression <b>.</b> ] |
| 25 | * [ <b><</b> Type { <b>,</b> Type } <b>></b> ] |
| 26 | * <b>super</b> <b>(</b> [ Expression { <b>,</b> Expression } ] <b>)</b> <b>;</b> |
| 27 | * </pre> |
| 28 | * |
| 29 | * @since 2.0 |
| 30 | * @noinstantiate This class is not intended to be instantiated by clients. |
| 31 | */ |
| 32 | @SuppressWarnings({"rawtypes", "unchecked"}) |
| 33 | public class SuperConstructorInvocation extends Statement { |
| 34 | |
| 35 | /** |
| 36 | * The "expression" structural property of this node type (child type: {@link Expression}). |
| 37 | * @since 3.0 |
| 38 | */ |
| 39 | public static final ChildPropertyDescriptor EXPRESSION_PROPERTY = |
| 40 | new ChildPropertyDescriptor(SuperConstructorInvocation.class, "expression", Expression.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$ |
| 41 | |
| 42 | /** |
| 43 | * The "typeArguments" structural property of this node type (element type: {@link Type}) (added in JLS3 API). |
| 44 | * @since 3.1 |
| 45 | */ |
| 46 | public static final ChildListPropertyDescriptor TYPE_ARGUMENTS_PROPERTY = |
| 47 | new ChildListPropertyDescriptor(SuperConstructorInvocation.class, "typeArguments", Type.class, NO_CYCLE_RISK); //$NON-NLS-1$ |
| 48 | |
| 49 | /** |
| 50 | * The "arguments" structural property of this node type (element type: {@link Expression}). |
| 51 | * @since 3.0 |
| 52 | */ |
| 53 | public static final ChildListPropertyDescriptor ARGUMENTS_PROPERTY = |
| 54 | new ChildListPropertyDescriptor(SuperConstructorInvocation.class, "arguments", Expression.class, CYCLE_RISK); //$NON-NLS-1$ |
| 55 | |
| 56 | /** |
| 57 | * A list of property descriptors (element type: |
| 58 | * {@link StructuralPropertyDescriptor}), |
| 59 | * or null if uninitialized. |
| 60 | * @since 3.0 |
| 61 | */ |
| 62 | private static final List PROPERTY_DESCRIPTORS_2_0; |
| 63 | |
| 64 | /** |
| 65 | * A list of property descriptors (element type: |
| 66 | * {@link StructuralPropertyDescriptor}), |
| 67 | * or null if uninitialized. |
| 68 | * @since 3.1 |
| 69 | */ |
| 70 | private static final List PROPERTY_DESCRIPTORS_3_0; |
| 71 | |
| 72 | static { |
| 73 | List propertyList = new ArrayList(3); |
| 74 | createPropertyList(SuperConstructorInvocation.class, propertyList); |
| 75 | addProperty(EXPRESSION_PROPERTY, propertyList); |
| 76 | addProperty(ARGUMENTS_PROPERTY, propertyList); |
| 77 | PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(propertyList); |
| 78 | |
| 79 | propertyList = new ArrayList(4); |
| 80 | createPropertyList(SuperConstructorInvocation.class, propertyList); |
| 81 | addProperty(EXPRESSION_PROPERTY, propertyList); |
| 82 | addProperty(TYPE_ARGUMENTS_PROPERTY, propertyList); |
| 83 | addProperty(ARGUMENTS_PROPERTY, propertyList); |
| 84 | PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(propertyList); |
| 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 | |
| 94 | * @return a list of property descriptors (element type: |
| 95 | * {@link StructuralPropertyDescriptor}) |
| 96 | * @since 3.0 |
| 97 | */ |
| 98 | public static List propertyDescriptors(int apiLevel) { |
| 99 | if (apiLevel == AST.JLS2_INTERNAL) { |
| 100 | return PROPERTY_DESCRIPTORS_2_0; |
| 101 | } else { |
| 102 | return PROPERTY_DESCRIPTORS_3_0; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * The expression; <code>null</code> for none; defaults to none. |
| 108 | */ |
| 109 | private Expression optionalExpression = null; |
| 110 | |
| 111 | /** |
| 112 | * The type arguments (element type: {@link Type}). |
| 113 | * Null in JLS2. Added in JLS3; defaults to an empty list |
| 114 | * (see constructor). |
| 115 | * @since 3.1 |
| 116 | */ |
| 117 | private ASTNode.NodeList typeArguments = null; |
| 118 | |
| 119 | /** |
| 120 | * The list of argument expressions (element type: |
| 121 | * {@link Expression}). Defaults to an empty list. |
| 122 | */ |
| 123 | private ASTNode.NodeList arguments = |
| 124 | new ASTNode.NodeList(ARGUMENTS_PROPERTY); |
| 125 | |
| 126 | /** |
| 127 | * Creates a new AST node for an super constructor invocation statement |
| 128 | * owned by the given AST. By default, no type arguments, and an empty list |
| 129 | * of arguments. |
| 130 | * |
| 131 | * @param ast the AST that is to own this node |
| 132 | */ |
| 133 | SuperConstructorInvocation(AST ast) { |
| 134 | super(ast); |
| 135 | if (ast.apiLevel >= AST.JLS3_INTERNAL) { |
| 136 | this.typeArguments = new ASTNode.NodeList(TYPE_ARGUMENTS_PROPERTY); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | final List internalStructuralPropertiesForType(int apiLevel) { |
| 142 | return propertyDescriptors(apiLevel); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { |
| 147 | if (property == EXPRESSION_PROPERTY) { |
| 148 | if (get) { |
| 149 | return getExpression(); |
| 150 | } else { |
| 151 | setExpression((Expression) child); |
| 152 | return null; |
| 153 | } |
| 154 | } |
| 155 | // allow default implementation to flag the error |
| 156 | return super.internalGetSetChildProperty(property, get, child); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | final List internalGetChildListProperty(ChildListPropertyDescriptor property) { |
| 161 | if (property == ARGUMENTS_PROPERTY) { |
| 162 | return arguments(); |
| 163 | } |
| 164 | if (property == TYPE_ARGUMENTS_PROPERTY) { |
| 165 | return typeArguments(); |
| 166 | } |
| 167 | // allow default implementation to flag the error |
| 168 | return super.internalGetChildListProperty(property); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | final int getNodeType0() { |
| 173 | return SUPER_CONSTRUCTOR_INVOCATION; |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | ASTNode clone0(AST target) { |
| 178 | SuperConstructorInvocation result = new SuperConstructorInvocation(target); |
| 179 | result.setSourceRange(getStartPosition(), getLength()); |
| 180 | result.copyLeadingComment(this); |
| 181 | result.setExpression( |
| 182 | (Expression) ASTNode.copySubtree(target, getExpression())); |
| 183 | if (this.ast.apiLevel >= AST.JLS3_INTERNAL) { |
| 184 | result.typeArguments().addAll(ASTNode.copySubtrees(target, typeArguments())); |
| 185 | } |
| 186 | result.arguments().addAll(ASTNode.copySubtrees(target, arguments())); |
| 187 | return result; |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
| 192 | // dispatch to correct overloaded match method |
| 193 | return matcher.match(this, other); |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | void accept0(ASTVisitor visitor) { |
| 198 | boolean visitChildren = visitor.visit(this); |
| 199 | if (visitChildren) { |
| 200 | // visit children in normal left to right reading order |
| 201 | acceptChild(visitor, getExpression()); |
| 202 | if (this.ast.apiLevel >= AST.JLS3_INTERNAL) { |
| 203 | acceptChildren(visitor, this.typeArguments); |
| 204 | } |
| 205 | acceptChildren(visitor, this.arguments); |
| 206 | } |
| 207 | visitor.endVisit(this); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Returns the expression of this super constructor invocation statement, |
| 212 | * or <code>null</code> if there is none. |
| 213 | * |
| 214 | * @return the expression node, or <code>null</code> if there is none |
| 215 | */ |
| 216 | public Expression getExpression() { |
| 217 | return this.optionalExpression; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Sets or clears the expression of this super constructor invocation |
| 222 | * statement. |
| 223 | * |
| 224 | * @param expression the expression node, or <code>null</code> if |
| 225 | * there is none |
| 226 | * @exception IllegalArgumentException if: |
| 227 | * <ul> |
| 228 | * <li>the node belongs to a different AST</li> |
| 229 | * <li>the node already has a parent</li> |
| 230 | * <li>a cycle in would be created</li> |
| 231 | * </ul> |
| 232 | */ |
| 233 | public void setExpression(Expression expression) { |
| 234 | ASTNode oldChild = this.optionalExpression; |
| 235 | preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY); |
| 236 | this.optionalExpression = expression; |
| 237 | postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Returns the live ordered list of type arguments of this constructor |
| 242 | * invocation (added in JLS3 API). |
| 243 | * |
| 244 | * @return the live list of type arguments |
| 245 | * (element type: {@link Type}) |
| 246 | * @exception UnsupportedOperationException if this operation is used in |
| 247 | * a JLS2 AST |
| 248 | * @since 3.1 |
| 249 | */ |
| 250 | public List typeArguments() { |
| 251 | // more efficient than just calling unsupportedIn2() to check |
| 252 | if (this.typeArguments == null) { |
| 253 | unsupportedIn2(); |
| 254 | } |
| 255 | return this.typeArguments; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Returns the live ordered list of argument expressions in this super |
| 260 | * constructor invocation statement. |
| 261 | * |
| 262 | * @return the live list of argument expressions |
| 263 | * (element type: {@link Expression}) |
| 264 | */ |
| 265 | public List arguments() { |
| 266 | return this.arguments; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Resolves and returns the binding for the constructor invoked by this |
| 271 | * expression. |
| 272 | * <p> |
| 273 | * Note that bindings are generally unavailable unless requested when the |
| 274 | * AST is being built. |
| 275 | * </p> |
| 276 | * |
| 277 | * @return the constructor binding, or <code>null</code> if the binding |
| 278 | * cannot be resolved |
| 279 | */ |
| 280 | public IMethodBinding resolveConstructorBinding() { |
| 281 | return this.ast.getBindingResolver().resolveConstructor(this); |
| 282 | } |
| 283 | |
| 284 | @Override |
| 285 | int memSize() { |
| 286 | // treat Code as free |
| 287 | return BASE_NODE_SIZE + 3 * 4; |
| 288 | } |
| 289 | |
| 290 | @Override |
| 291 | int treeSize() { |
| 292 | return memSize() |
| 293 | + (this.optionalExpression == null ? 0 : getExpression().treeSize()) |
| 294 | + (this.typeArguments == null ? 0 : this.typeArguments.listSize()) |
| 295 | + (this.arguments == null ? 0 : this.arguments.listSize()); |
| 296 | } |
| 297 | } |
| 298 |
Members