| 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 | * Variable declaration fragment AST node type, used in field declarations, |
| 22 | * local variable declarations, <code>ForStatement</code> initializers, |
| 23 | * and <code>LambdaExpression</code> parameters. |
| 24 | * In contrast to <code>SingleVariableDeclaration</code>, fragments are |
| 25 | * missing the modifiers and the type; these are either located in the fragment's |
| 26 | * parent node, or inferred (for lambda parameters). |
| 27 | * |
| 28 | * <pre> |
| 29 | * VariableDeclarationFragment: |
| 30 | * Identifier { Dimension } [ <b>=</b> Expression ] |
| 31 | * </pre> |
| 32 | * @since 2.0 |
| 33 | * @noinstantiate This class is not intended to be instantiated by clients. |
| 34 | */ |
| 35 | @SuppressWarnings("rawtypes") |
| 36 | public class VariableDeclarationFragment extends VariableDeclaration { |
| 37 | |
| 38 | /** |
| 39 | * The "name" structural property of this node type (child type: {@link SimpleName}). |
| 40 | * @since 3.0 |
| 41 | */ |
| 42 | public static final ChildPropertyDescriptor NAME_PROPERTY = |
| 43 | internalNamePropertyFactory(VariableDeclarationFragment.class); |
| 44 | |
| 45 | /** |
| 46 | * The "extraDimensions" structural property of this node type (type: {@link Integer}) (below JLS8 only). |
| 47 | * |
| 48 | * @since 3.0 |
| 49 | * @deprecated in JLS8 and later, use {@link VariableDeclarationFragment#EXTRA_DIMENSIONS2_PROPERTY} instead. |
| 50 | */ |
| 51 | public static final SimplePropertyDescriptor EXTRA_DIMENSIONS_PROPERTY = |
| 52 | internalExtraDimensionsPropertyFactory(VariableDeclarationFragment.class); |
| 53 | |
| 54 | /** |
| 55 | * The "extraDimensions2" structural property of this node type (element type: {@link Dimension}) (added in JLS8 API). |
| 56 | * @since 3.10 |
| 57 | */ |
| 58 | public static final ChildListPropertyDescriptor EXTRA_DIMENSIONS2_PROPERTY = |
| 59 | internalExtraDimensions2PropertyFactory(VariableDeclarationFragment.class); |
| 60 | |
| 61 | /** |
| 62 | * The "initializer" structural property of this node type (child type: {@link Expression}). |
| 63 | * @since 3.0 |
| 64 | */ |
| 65 | public static final ChildPropertyDescriptor INITIALIZER_PROPERTY = |
| 66 | internalInitializerPropertyFactory(VariableDeclarationFragment.class); |
| 67 | |
| 68 | /** |
| 69 | * A list of property descriptors (element type: |
| 70 | * {@link StructuralPropertyDescriptor}), |
| 71 | * or null if uninitialized. |
| 72 | * @since 3.0 |
| 73 | */ |
| 74 | private static final List PROPERTY_DESCRIPTORS; |
| 75 | |
| 76 | /** |
| 77 | * A list of property descriptors (element type: |
| 78 | * {@link StructuralPropertyDescriptor}), |
| 79 | * or null if uninitialized. |
| 80 | * @since 3.10 |
| 81 | */ |
| 82 | private static final List PROPERTY_DESCRIPTORS_8_0; |
| 83 | |
| 84 | static { |
| 85 | List propertyList = new ArrayList(4); |
| 86 | createPropertyList(VariableDeclarationFragment.class, propertyList); |
| 87 | addProperty(NAME_PROPERTY, propertyList); |
| 88 | addProperty(EXTRA_DIMENSIONS_PROPERTY, propertyList); |
| 89 | addProperty(INITIALIZER_PROPERTY, propertyList); |
| 90 | PROPERTY_DESCRIPTORS = reapPropertyList(propertyList); |
| 91 | |
| 92 | propertyList = new ArrayList(4); |
| 93 | createPropertyList(VariableDeclarationFragment.class, propertyList); |
| 94 | addProperty(NAME_PROPERTY, propertyList); |
| 95 | addProperty(EXTRA_DIMENSIONS2_PROPERTY, propertyList); |
| 96 | addProperty(INITIALIZER_PROPERTY, propertyList); |
| 97 | PROPERTY_DESCRIPTORS_8_0 = reapPropertyList(propertyList); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Returns a list of structural property descriptors for this node type. |
| 102 | * Clients must not modify the result. |
| 103 | * |
| 104 | * @param apiLevel the API level; one of the |
| 105 | * <code>AST.JLS*</code> constants |
| 106 | * @return a list of property descriptors (element type: |
| 107 | * {@link StructuralPropertyDescriptor}) |
| 108 | * @since 3.0 |
| 109 | */ |
| 110 | public static List propertyDescriptors(int apiLevel) { |
| 111 | if (apiLevel >= AST.JLS8_INTERNAL) { |
| 112 | return PROPERTY_DESCRIPTORS_8_0; |
| 113 | } else { |
| 114 | return PROPERTY_DESCRIPTORS; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Creates a new AST node for a variable declaration fragment owned by the |
| 120 | * given AST. By default, the variable declaration has: an unspecified |
| 121 | * (but legal) variable name, no initializer, and no extra array dimensions. |
| 122 | * <p> |
| 123 | * N.B. This constructor is package-private. |
| 124 | * </p> |
| 125 | * |
| 126 | * @param ast the AST that is to own this node |
| 127 | */ |
| 128 | VariableDeclarationFragment(AST ast) { |
| 129 | super(ast); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | final ChildPropertyDescriptor internalNameProperty() { |
| 134 | return NAME_PROPERTY; |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | final SimplePropertyDescriptor internalExtraDimensionsProperty() { |
| 139 | return EXTRA_DIMENSIONS_PROPERTY; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | final ChildListPropertyDescriptor internalExtraDimensions2Property() { |
| 144 | return EXTRA_DIMENSIONS2_PROPERTY; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | final ChildPropertyDescriptor internalInitializerProperty() { |
| 149 | return INITIALIZER_PROPERTY; |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | final List internalStructuralPropertiesForType(int apiLevel) { |
| 154 | return propertyDescriptors(apiLevel); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | final int internalGetSetIntProperty(SimplePropertyDescriptor property, boolean get, int value) { |
| 159 | if (property == EXTRA_DIMENSIONS_PROPERTY) { |
| 160 | if (get) { |
| 161 | return getExtraDimensions(); |
| 162 | } else { |
| 163 | internalSetExtraDimensions(value); |
| 164 | return 0; |
| 165 | } |
| 166 | } |
| 167 | // allow default implementation to flag the error |
| 168 | return super.internalGetSetIntProperty(property, get, value); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { |
| 173 | if (property == NAME_PROPERTY) { |
| 174 | if (get) { |
| 175 | return getName(); |
| 176 | } else { |
| 177 | setName((SimpleName) child); |
| 178 | return null; |
| 179 | } |
| 180 | } |
| 181 | if (property == INITIALIZER_PROPERTY) { |
| 182 | if (get) { |
| 183 | return getInitializer(); |
| 184 | } else { |
| 185 | setInitializer((Expression) child); |
| 186 | return null; |
| 187 | } |
| 188 | } |
| 189 | // allow default implementation to flag the error |
| 190 | return super.internalGetSetChildProperty(property, get, child); |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | final List internalGetChildListProperty(ChildListPropertyDescriptor property) { |
| 195 | if (property == EXTRA_DIMENSIONS2_PROPERTY) { |
| 196 | return extraDimensions(); |
| 197 | } |
| 198 | // allow default implementation to flag the error |
| 199 | return super.internalGetChildListProperty(property); |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | final int getNodeType0() { |
| 204 | return VARIABLE_DECLARATION_FRAGMENT; |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | ASTNode clone0(AST target) { |
| 209 | VariableDeclarationFragment result = new VariableDeclarationFragment(target); |
| 210 | result.setSourceRange(getStartPosition(), getLength()); |
| 211 | result.setName((SimpleName) getName().clone(target)); |
| 212 | if (this.ast.apiLevel >= AST.JLS8_INTERNAL) { |
| 213 | result.extraDimensions().addAll( |
| 214 | ASTNode.copySubtrees(target, extraDimensions())); |
| 215 | } else { |
| 216 | result.internalSetExtraDimensions(getExtraDimensions()); |
| 217 | } |
| 218 | result.setInitializer( |
| 219 | (Expression) ASTNode.copySubtree(target, getInitializer())); |
| 220 | return result; |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
| 225 | // dispatch to correct overloaded match method |
| 226 | return matcher.match(this, other); |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | void accept0(ASTVisitor visitor) { |
| 231 | boolean visitChildren = visitor.visit(this); |
| 232 | if (visitChildren) { |
| 233 | // visit children in normal left to right reading order |
| 234 | acceptChild(visitor, getName()); |
| 235 | if (this.ast.apiLevel >= AST.JLS8_INTERNAL) { |
| 236 | acceptChildren(visitor, this.extraDimensions); |
| 237 | } |
| 238 | acceptChild(visitor, getInitializer()); |
| 239 | } |
| 240 | visitor.endVisit(this); |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | int memSize() { |
| 245 | // treat Operator as free |
| 246 | return BASE_NODE_SIZE + 4 * 4; |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | int treeSize() { |
| 251 | return |
| 252 | memSize() |
| 253 | + (this.variableName == null ? 0 : getName().treeSize()) |
| 254 | + (this.extraDimensions == null ? 0 : this.extraDimensions.listSize()) |
| 255 | + (this.optionalInitializer == null ? 0 : getInitializer().treeSize()); |
| 256 | } |
| 257 | } |
| 258 |
Members