| 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 | * Simple or qualified "super" field access expression AST node type. |
| 22 | * |
| 23 | * <pre> |
| 24 | * SuperFieldAccess: |
| 25 | * [ ClassName <b>.</b> ] <b>super</b> <b>.</b> Identifier |
| 26 | * </pre> |
| 27 | * |
| 28 | * <p> |
| 29 | * See <code>FieldAccess</code> for guidelines on handling other expressions |
| 30 | * that resemble qualified names. |
| 31 | * </p> |
| 32 | * |
| 33 | * @see FieldAccess |
| 34 | * @since 2.0 |
| 35 | * @noinstantiate This class is not intended to be instantiated by clients. |
| 36 | */ |
| 37 | @SuppressWarnings("rawtypes") |
| 38 | public class SuperFieldAccess extends Expression { |
| 39 | |
| 40 | /** |
| 41 | * The "qualifier" structural property of this node type (child type: {@link Name}). |
| 42 | * @since 3.0 |
| 43 | */ |
| 44 | public static final ChildPropertyDescriptor QUALIFIER_PROPERTY = |
| 45 | new ChildPropertyDescriptor(SuperFieldAccess.class, "qualifier", Name.class, OPTIONAL, NO_CYCLE_RISK); //$NON-NLS-1$ |
| 46 | |
| 47 | /** |
| 48 | * The "name" structural property of this node type (child type: {@link SimpleName}). |
| 49 | * @since 3.0 |
| 50 | */ |
| 51 | public static final ChildPropertyDescriptor NAME_PROPERTY = |
| 52 | new ChildPropertyDescriptor(SuperFieldAccess.class, "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$ |
| 53 | |
| 54 | /** |
| 55 | * A list of property descriptors (element type: |
| 56 | * {@link StructuralPropertyDescriptor}), |
| 57 | * or null if uninitialized. |
| 58 | */ |
| 59 | private static final List PROPERTY_DESCRIPTORS; |
| 60 | |
| 61 | static { |
| 62 | List propertyList = new ArrayList(3); |
| 63 | createPropertyList(SuperFieldAccess.class, propertyList); |
| 64 | addProperty(QUALIFIER_PROPERTY, propertyList); |
| 65 | addProperty(NAME_PROPERTY, propertyList); |
| 66 | PROPERTY_DESCRIPTORS = reapPropertyList(propertyList); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns a list of structural property descriptors for this node type. |
| 71 | * Clients must not modify the result. |
| 72 | * |
| 73 | * @param apiLevel the API level; one of the |
| 74 | * <code>AST.JLS*</code> constants |
| 75 | * @return a list of property descriptors (element type: |
| 76 | * {@link StructuralPropertyDescriptor}) |
| 77 | * @since 3.0 |
| 78 | */ |
| 79 | public static List propertyDescriptors(int apiLevel) { |
| 80 | return PROPERTY_DESCRIPTORS; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * The optional qualifier; <code>null</code> for none; defaults to none. |
| 85 | */ |
| 86 | private Name optionalQualifier = null; |
| 87 | |
| 88 | /** |
| 89 | * The field; lazily initialized; defaults to an unspecified, |
| 90 | * but legal, simple field name. |
| 91 | */ |
| 92 | private SimpleName fieldName = null; |
| 93 | |
| 94 | /** |
| 95 | * Creates a new unparented node for a super field access expression owned |
| 96 | * by the given AST. By default, field name is an unspecified, but legal, |
| 97 | * name, and there is no qualifier. |
| 98 | * <p> |
| 99 | * N.B. This constructor is package-private. |
| 100 | * </p> |
| 101 | * |
| 102 | * @param ast the AST that is to own this node |
| 103 | */ |
| 104 | SuperFieldAccess(AST ast) { |
| 105 | super(ast); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | final List internalStructuralPropertiesForType(int apiLevel) { |
| 110 | return propertyDescriptors(apiLevel); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { |
| 115 | if (property == QUALIFIER_PROPERTY) { |
| 116 | if (get) { |
| 117 | return getQualifier(); |
| 118 | } else { |
| 119 | setQualifier((Name) child); |
| 120 | return null; |
| 121 | } |
| 122 | } |
| 123 | if (property == NAME_PROPERTY) { |
| 124 | if (get) { |
| 125 | return getName(); |
| 126 | } else { |
| 127 | setName((SimpleName) child); |
| 128 | return null; |
| 129 | } |
| 130 | } |
| 131 | // allow default implementation to flag the error |
| 132 | return super.internalGetSetChildProperty(property, get, child); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | final int getNodeType0() { |
| 137 | return SUPER_FIELD_ACCESS; |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | ASTNode clone0(AST target) { |
| 142 | SuperFieldAccess result = new SuperFieldAccess(target); |
| 143 | result.setSourceRange(getStartPosition(), getLength()); |
| 144 | result.setName((SimpleName) ASTNode.copySubtree(target, getName())); |
| 145 | result.setQualifier((Name) ASTNode.copySubtree(target, getQualifier())); |
| 146 | return result; |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
| 151 | // dispatch to correct overloaded match method |
| 152 | return matcher.match(this, other); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | void accept0(ASTVisitor visitor) { |
| 157 | boolean visitChildren = visitor.visit(this); |
| 158 | if (visitChildren) { |
| 159 | // visit children in normal left to right reading order |
| 160 | acceptChild(visitor, getQualifier()); |
| 161 | acceptChild(visitor, getName()); |
| 162 | } |
| 163 | visitor.endVisit(this); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Returns the qualifier of this "super" field access expression, or |
| 168 | * <code>null</code> if there is none. |
| 169 | * |
| 170 | * @return the qualifier name node, or <code>null</code> if there is none |
| 171 | */ |
| 172 | public Name getQualifier() { |
| 173 | return this.optionalQualifier; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Sets or clears the qualifier of this "super" field access expression. |
| 178 | * |
| 179 | * @param name the qualifier name node, or <code>null</code> if |
| 180 | * there is none |
| 181 | * @exception IllegalArgumentException if: |
| 182 | * <ul> |
| 183 | * <li>the node belongs to a different AST</li> |
| 184 | * <li>the node already has a parent</li> |
| 185 | * </ul> |
| 186 | */ |
| 187 | public void setQualifier(Name name) { |
| 188 | ASTNode oldChild = this.optionalQualifier; |
| 189 | preReplaceChild(oldChild, name, QUALIFIER_PROPERTY); |
| 190 | this.optionalQualifier = name; |
| 191 | postReplaceChild(oldChild, name, QUALIFIER_PROPERTY); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Returns the name of the field accessed in this "super" field access |
| 196 | * expression. |
| 197 | * |
| 198 | * @return the field name |
| 199 | */ |
| 200 | public SimpleName getName() { |
| 201 | if (this.fieldName == null) { |
| 202 | // lazy init must be thread-safe for readers |
| 203 | synchronized (this) { |
| 204 | if (this.fieldName == null) { |
| 205 | preLazyInit(); |
| 206 | this.fieldName = new SimpleName(this.ast); |
| 207 | postLazyInit(this.fieldName, NAME_PROPERTY); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | return this.fieldName; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Resolves and returns the binding for the field accessed by this |
| 216 | * expression. |
| 217 | * <p> |
| 218 | * Note that bindings are generally unavailable unless requested when the |
| 219 | * AST is being built. |
| 220 | * </p> |
| 221 | * |
| 222 | * @return the variable binding, or <code>null</code> if the binding cannot |
| 223 | * be resolved |
| 224 | * @since 3.0 |
| 225 | */ |
| 226 | public IVariableBinding resolveFieldBinding() { |
| 227 | return this.ast.getBindingResolver().resolveField(this); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Sets the name of the field accessed in this "super" field access |
| 232 | * expression. |
| 233 | * |
| 234 | * @param fieldName the field name |
| 235 | * @exception IllegalArgumentException if: |
| 236 | * <ul> |
| 237 | * <li>the node belongs to a different AST</li> |
| 238 | * <li>the node already has a parent</li> |
| 239 | * </ul> |
| 240 | */ |
| 241 | public void setName(SimpleName fieldName) { |
| 242 | if (fieldName == null) { |
| 243 | throw new IllegalArgumentException(); |
| 244 | } |
| 245 | ASTNode oldChild = this.fieldName; |
| 246 | preReplaceChild(oldChild, fieldName, NAME_PROPERTY); |
| 247 | this.fieldName = fieldName; |
| 248 | postReplaceChild(oldChild, fieldName, NAME_PROPERTY); |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | int memSize() { |
| 253 | // treat Code as free |
| 254 | return BASE_NODE_SIZE + 2 * 4; |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | int treeSize() { |
| 259 | return |
| 260 | memSize() |
| 261 | + (this.optionalQualifier == null ? 0 : getQualifier().treeSize()) |
| 262 | + (this.fieldName == null ? 0 : getName().treeSize()); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 |
Members