1 | /******************************************************************************* |
---|---|
2 | * Copyright (c) 2016, 2017 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 | * IBM Corporation - initial API and implementation |
12 | *******************************************************************************/ |
13 | package org.eclipse.jdt.core.dom; |
14 | |
15 | import java.util.ArrayList; |
16 | import java.util.List; |
17 | |
18 | /** |
19 | * Uses directive AST node type (added in JLS9 API). |
20 | * <pre> |
21 | * UsesDirective: |
22 | * <b>uses</b> Name <b>;</b> |
23 | * </pre> |
24 | * |
25 | * @since 3.14 |
26 | * |
27 | * @noextend This class is not intended to be subclassed by clients. |
28 | * @noinstantiate This class is not intended to be instantiated by clients. |
29 | */ |
30 | @SuppressWarnings("rawtypes") |
31 | public class UsesDirective extends ModuleDirective { |
32 | |
33 | /** |
34 | * The "name" structural property of this node type (child type: {@link Name}). |
35 | */ |
36 | public static final ChildPropertyDescriptor NAME_PROPERTY = |
37 | new ChildPropertyDescriptor(UsesDirective.class, "name", Name.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$ |
38 | |
39 | /** |
40 | * A list of property descriptors (element type: |
41 | * {@link StructuralPropertyDescriptor}), |
42 | * or null if uninitialized. |
43 | */ |
44 | private static final List PROPERTY_DESCRIPTORS_9_0; |
45 | |
46 | static { |
47 | List properyList = new ArrayList(2); |
48 | createPropertyList(UsesDirective.class, properyList); |
49 | addProperty(NAME_PROPERTY, properyList); |
50 | PROPERTY_DESCRIPTORS_9_0 = reapPropertyList(properyList); |
51 | } |
52 | |
53 | /** |
54 | * Returns a list of structural property descriptors for this node type. |
55 | * Clients must not modify the result. |
56 | * |
57 | * @param apiLevel the API level; one of the |
58 | * <code>AST.JLS*</code> constants |
59 | |
60 | * @return a list of property descriptors (element type: |
61 | * {@link StructuralPropertyDescriptor}) |
62 | */ |
63 | public static List propertyDescriptors(int apiLevel) { |
64 | return PROPERTY_DESCRIPTORS_9_0; |
65 | } |
66 | |
67 | /** |
68 | * The module name; lazily initialized; defaults to a unspecified, |
69 | * legal Java identifier. |
70 | */ |
71 | private Name name = null; |
72 | |
73 | /** |
74 | * Creates a new AST node for an uses directive owned by the |
75 | * given AST. The uses directive initially is |
76 | * for an unspecified, but legal, Java type name. |
77 | * <p> |
78 | * N.B. This constructor is package-private; all subclasses must be |
79 | * declared in the same package; clients are unable to declare |
80 | * additional subclasses. |
81 | * </p> |
82 | * |
83 | * @param ast the AST that is to own this node |
84 | */ |
85 | UsesDirective(AST ast) { |
86 | super(ast); |
87 | } |
88 | |
89 | @Override |
90 | final List internalStructuralPropertiesForType(int apiLevel) { |
91 | return propertyDescriptors(apiLevel); |
92 | } |
93 | |
94 | @Override |
95 | final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { |
96 | if (property == NAME_PROPERTY) { |
97 | if (get) { |
98 | return getName(); |
99 | } else { |
100 | setName((Name) child); |
101 | return null; |
102 | } |
103 | } |
104 | |
105 | // allow default implementation to flag the error |
106 | return super.internalGetSetChildProperty(property, get, child); |
107 | } |
108 | |
109 | @Override |
110 | final int getNodeType0() { |
111 | return USES_DIRECTIVE; |
112 | } |
113 | |
114 | @Override |
115 | ASTNode clone0(AST target) { |
116 | UsesDirective result = new UsesDirective(target); |
117 | result.setSourceRange(getStartPosition(), getLength()); |
118 | result.setName((Name) getName().clone(target)); |
119 | return result; |
120 | } |
121 | |
122 | @Override |
123 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
124 | // dispatch to correct overloaded match method |
125 | return matcher.match(this, other); |
126 | } |
127 | |
128 | @Override |
129 | void accept0(ASTVisitor visitor) { |
130 | boolean visitChildren = visitor.visit(this); |
131 | if (visitChildren) { |
132 | acceptChild(visitor, getName()); |
133 | } |
134 | visitor.endVisit(this); |
135 | } |
136 | |
137 | |
138 | /** |
139 | * Returns the name of the service in this directive. |
140 | * |
141 | * @return the name of the service |
142 | */ |
143 | public Name getName() { |
144 | if (this.name == null) { |
145 | // lazy init must be thread-safe for readers |
146 | synchronized (this) { |
147 | if (this.name == null) { |
148 | preLazyInit(); |
149 | this.name = this.ast.newQualifiedName( |
150 | new SimpleName(this.ast), new SimpleName(this.ast)); |
151 | postLazyInit(this.name, NAME_PROPERTY); |
152 | } |
153 | } |
154 | } |
155 | return this.name; |
156 | } |
157 | |
158 | /** |
159 | * Sets the name of the service in this directive. |
160 | * |
161 | * @param name the new name of the service |
162 | * @exception IllegalArgumentException if: |
163 | * <ul> |
164 | * <li>the node belongs to a different AST</li> |
165 | * <li>the node already has a parent</li> |
166 | * </ul> |
167 | */ |
168 | public void setName(Name name) { |
169 | if (name == null) { |
170 | throw new IllegalArgumentException(); |
171 | } |
172 | ASTNode oldChild = this.name; |
173 | preReplaceChild(oldChild, name, NAME_PROPERTY); |
174 | this.name = name; |
175 | postReplaceChild(oldChild, name, NAME_PROPERTY); |
176 | } |
177 | |
178 | @Override |
179 | int memSize() { |
180 | return BASE_NODE_SIZE + 1 * 4; |
181 | } |
182 | |
183 | @Override |
184 | int treeSize() { |
185 | return |
186 | memSize() |
187 | + (this.name == null ? 0 : getName().treeSize()); |
188 | } |
189 | |
190 | } |
191 |
Members