| 1 | /******************************************************************************* |
|---|---|
| 2 | * Copyright (c) 2020 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 | /** |
| 22 | * AST node for a module qualified name. A module qualified name is defined as |
| 23 | * a qualified/simple name preceded by a module name, which qualifies it. Expressing it this |
| 24 | * way means that the module qualifier and the qualified name get their own AST nodes. |
| 25 | * <pre> |
| 26 | * ModuleQualifiedName: |
| 27 | * Name <b>.</b> Name |
| 28 | * </pre> |
| 29 | * |
| 30 | * |
| 31 | * |
| 32 | * @noinstantiate This class is not intended to be instantiated by clients. |
| 33 | * @since 3.24 |
| 34 | * |
| 35 | */ |
| 36 | @SuppressWarnings("rawtypes") |
| 37 | public class ModuleQualifiedName extends Name { |
| 38 | |
| 39 | /** |
| 40 | * The "qualifier" structural property of this node type (child type: {@link Name}). * |
| 41 | */ |
| 42 | public static final ChildPropertyDescriptor MODULE_QUALIFIER_PROPERTY = |
| 43 | new ChildPropertyDescriptor(ModuleQualifiedName.class, "moduleQualifier", SimpleName.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$ |
| 44 | |
| 45 | /** |
| 46 | * The "name" structural property of this node type (child type: {@link SimpleName}). * |
| 47 | */ |
| 48 | public static final ChildPropertyDescriptor NAME_PROPERTY = |
| 49 | new ChildPropertyDescriptor(ModuleQualifiedName.class, "name", QualifiedName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$ |
| 50 | |
| 51 | /** |
| 52 | * A list of property descriptors (element type: |
| 53 | * {@link StructuralPropertyDescriptor}), |
| 54 | * or null if uninitialized. |
| 55 | */ |
| 56 | private static final List PROPERTY_DESCRIPTORS; |
| 57 | |
| 58 | static { |
| 59 | List propertyList = new ArrayList(3); |
| 60 | createPropertyList(ModuleQualifiedName.class, propertyList); |
| 61 | addProperty(MODULE_QUALIFIER_PROPERTY, propertyList); |
| 62 | addProperty(NAME_PROPERTY, propertyList); |
| 63 | PROPERTY_DESCRIPTORS = reapPropertyList(propertyList); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns a list of structural property descriptors for this node type. |
| 68 | * Clients must not modify the result. |
| 69 | * |
| 70 | * @param apiLevel the API level; one of the |
| 71 | * <code>AST.JLS*</code> constants |
| 72 | * @return a list of property descriptors (element type: |
| 73 | * {@link StructuralPropertyDescriptor}) |
| 74 | */ |
| 75 | public static List propertyDescriptors(int apiLevel) { |
| 76 | return PROPERTY_DESCRIPTORS; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * The identifier; lazily initialized; defaults to a unspecified, legal |
| 81 | * Java identifier. |
| 82 | */ |
| 83 | private Name moduleQualifier = null; |
| 84 | |
| 85 | /** |
| 86 | * The name being module veysqualified; lazily initialized; defaults to a unspecified, |
| 87 | * legal Java identifier. |
| 88 | */ |
| 89 | private Name name = null; |
| 90 | |
| 91 | /** |
| 92 | * Creates a new AST node for a module qualified name owned by the given AST. |
| 93 | * <p> |
| 94 | * N.B. This constructor is package-private; all subclasses must be |
| 95 | * declared in the same package; clients are unable to declare |
| 96 | * additional subclasses. |
| 97 | * </p> |
| 98 | * |
| 99 | * @param ast the AST that is to own this node |
| 100 | */ |
| 101 | ModuleQualifiedName(AST ast) { |
| 102 | super(ast); |
| 103 | unsupportedBelow15(); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | final List internalStructuralPropertiesForType(int apiLevel) { |
| 108 | return propertyDescriptors(apiLevel); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { |
| 113 | if (property == MODULE_QUALIFIER_PROPERTY) { |
| 114 | if (get) { |
| 115 | return getModuleQualifier(); |
| 116 | } else { |
| 117 | setModuleQualifier((SimpleName) child); |
| 118 | return null; |
| 119 | } |
| 120 | } |
| 121 | if (property == NAME_PROPERTY) { |
| 122 | if (get) { |
| 123 | return getName(); |
| 124 | } else { |
| 125 | setName((QualifiedName) child); |
| 126 | return null; |
| 127 | } |
| 128 | } |
| 129 | // allow default implementation to flag the error |
| 130 | return super.internalGetSetChildProperty(property, get, child); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | final int getNodeType0() { |
| 135 | return MODULE_QUALIFIED_NAME; |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | ASTNode clone0(AST target) { |
| 140 | ModuleQualifiedName result = new ModuleQualifiedName(target); |
| 141 | result.setSourceRange(getStartPosition(), getLength()); |
| 142 | result.setModuleQualifier((SimpleName) getModuleQualifier().clone(target)); |
| 143 | result.setName((QualifiedName) getName().clone(target)); |
| 144 | return result; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
| 149 | // dispatch to correct overloaded match method |
| 150 | return matcher.match(this, other); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | void accept0(ASTVisitor visitor) { |
| 155 | boolean visitChildren = visitor.visit(this); |
| 156 | if (visitChildren) { |
| 157 | // visit children in normal left to right reading order |
| 158 | acceptChild(visitor, getModuleQualifier()); |
| 159 | acceptChild(visitor, getName()); |
| 160 | } |
| 161 | visitor.endVisit(this); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Returns the qualifier part of this qualified name. |
| 166 | * |
| 167 | * @return the qualifier part of this qualified name |
| 168 | */ |
| 169 | public Name getModuleQualifier() { |
| 170 | if (this.moduleQualifier == null) { |
| 171 | // lazy init must be thread-safe for readers |
| 172 | synchronized (this) { |
| 173 | if (this.moduleQualifier == null) { |
| 174 | preLazyInit(); |
| 175 | this.moduleQualifier = new SimpleName(this.ast); |
| 176 | postLazyInit(this.moduleQualifier, MODULE_QUALIFIER_PROPERTY); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | return this.moduleQualifier; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Sets the qualifier of this qualified name to the given name. |
| 185 | * |
| 186 | * @param moduleQualifier the qualifier of this qualified name |
| 187 | * @exception IllegalArgumentException if: |
| 188 | * <ul> |
| 189 | * <li>the node belongs to a different AST</li> |
| 190 | * <li>the node already has a parent</li> |
| 191 | * <li>a cycle in would be created</li> |
| 192 | * </ul> |
| 193 | */ |
| 194 | public void setModuleQualifier(Name moduleQualifier) { |
| 195 | if (moduleQualifier == null) { |
| 196 | throw new IllegalArgumentException(); |
| 197 | } |
| 198 | ASTNode oldChild = this.moduleQualifier; |
| 199 | preReplaceChild(oldChild, moduleQualifier, MODULE_QUALIFIER_PROPERTY); |
| 200 | this.moduleQualifier = moduleQualifier; |
| 201 | postReplaceChild(oldChild, moduleQualifier, MODULE_QUALIFIER_PROPERTY); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Returns the name part of this qualified name. |
| 206 | * |
| 207 | * @return the name being qualified |
| 208 | */ |
| 209 | public Name getName() { |
| 210 | return this.name; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Sets the name part of this qualified name to the given simple name. |
| 215 | * |
| 216 | * @param name the identifier of this qualified name |
| 217 | * @exception IllegalArgumentException if: |
| 218 | * <ul> |
| 219 | * <li>the node belongs to a different AST</li> |
| 220 | * <li>the node already has a parent</li> |
| 221 | * </ul> |
| 222 | */ |
| 223 | public void setName(Name name) { |
| 224 | ASTNode oldChild = this.name; |
| 225 | preReplaceChild(oldChild, name, NAME_PROPERTY); |
| 226 | this.name = name; |
| 227 | postReplaceChild(oldChild, name, NAME_PROPERTY); |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | void appendName(StringBuffer buffer) { |
| 232 | getModuleQualifier().appendName(buffer); |
| 233 | buffer.append('/'); |
| 234 | if (getName() != null) { |
| 235 | getName().appendName(buffer); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | int memSize() { |
| 241 | return BASE_NAME_NODE_SIZE + 3 * 4; |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | int treeSize() { |
| 246 | return |
| 247 | memSize() |
| 248 | + (this.name == null ? 0 : getName().treeSize()) |
| 249 | + (this.moduleQualifier == null ? 0 : getModuleQualifier().treeSize()); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 |
Members