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