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 | * Provides directive AST node type (added in JLS9 API). |
20 | * <pre> |
21 | * ProvidesDirective: |
22 | * <b>provides</b> Name <b>with</b> Name {<b>,</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", "unchecked"}) |
31 | public class ProvidesDirective 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(ProvidesDirective.class, "name", Name.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$ |
38 | |
39 | /** |
40 | * The "implementations" structural property of this node type (element type: {@link Name}). |
41 | */ |
42 | public static final ChildListPropertyDescriptor IMPLEMENTATIONS_PROPERTY = |
43 | new ChildListPropertyDescriptor(ProvidesDirective.class, "implementations", Name.class, NO_CYCLE_RISK); //$NON-NLS-1$ |
44 | |
45 | /** |
46 | * A list of property descriptors (element type: |
47 | * {@link StructuralPropertyDescriptor}), |
48 | * or null if uninitialized. |
49 | */ |
50 | private static final List PROPERTY_DESCRIPTORS_9_0; |
51 | |
52 | static { |
53 | List properyList = new ArrayList(3); |
54 | createPropertyList(ProvidesDirective.class, properyList); |
55 | addProperty(NAME_PROPERTY, properyList); |
56 | addProperty(IMPLEMENTATIONS_PROPERTY, properyList); |
57 | PROPERTY_DESCRIPTORS_9_0 = reapPropertyList(properyList); |
58 | } |
59 | |
60 | /** |
61 | * Returns a list of structural property descriptors for this node type. |
62 | * Clients must not modify the result. |
63 | * |
64 | * @param apiLevel the API level; one of the |
65 | * <code>AST.JLS*</code> constants |
66 | |
67 | * @return a list of property descriptors (element type: |
68 | * {@link StructuralPropertyDescriptor}) |
69 | */ |
70 | public static List propertyDescriptors(int apiLevel) { |
71 | return PROPERTY_DESCRIPTORS_9_0; |
72 | } |
73 | |
74 | /** |
75 | * The interface name; lazily initialized; defaults to a unspecified, |
76 | * legal Java identifier. |
77 | */ |
78 | private Name name = null; |
79 | |
80 | /** |
81 | * The implementations names |
82 | * (element type: {@link Name}). |
83 | * Defaults to an empty list. |
84 | */ |
85 | private ASTNode.NodeList implementations = |
86 | new ASTNode.NodeList(IMPLEMENTATIONS_PROPERTY); |
87 | |
88 | /** |
89 | * Creates a new AST node for an provides directive owned by the |
90 | * given AST. The provides directive initially is |
91 | * for an unspecified, but legal, Java type name. |
92 | * <p> |
93 | * N.B. This constructor is package-private; all subclasses must be |
94 | * declared in the same package; clients are unable to declare |
95 | * additional subclasses. |
96 | * </p> |
97 | * |
98 | * @param ast the AST that is to own this node |
99 | */ |
100 | ProvidesDirective(AST ast) { |
101 | super(ast); |
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((Name) child); |
116 | return null; |
117 | } |
118 | } |
119 | |
120 | // allow default implementation to flag the error |
121 | return super.internalGetSetChildProperty(property, get, child); |
122 | } |
123 | |
124 | @Override |
125 | final List internalGetChildListProperty(ChildListPropertyDescriptor property) { |
126 | if (property == IMPLEMENTATIONS_PROPERTY) { |
127 | return implementations(); |
128 | } |
129 | // allow default implementation to flag the error |
130 | return super.internalGetChildListProperty(property); |
131 | } |
132 | |
133 | @Override |
134 | final int getNodeType0() { |
135 | return PROVIDES_DIRECTIVE; |
136 | } |
137 | |
138 | @Override |
139 | ASTNode clone0(AST target) { |
140 | ProvidesDirective result = new ProvidesDirective(target); |
141 | result.setSourceRange(getStartPosition(), getLength()); |
142 | result.setName((Name) getName().clone(target)); |
143 | result.implementations().addAll(ASTNode.copySubtrees(target, implementations())); |
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 | acceptChild(visitor, getName()); |
158 | acceptChildren(visitor, this.implementations); |
159 | } |
160 | visitor.endVisit(this); |
161 | } |
162 | |
163 | |
164 | /** |
165 | * Returns the name of the service in this directive. |
166 | * |
167 | * @return the services name |
168 | */ |
169 | public Name getName() { |
170 | if (this.name == null) { |
171 | // lazy init must be thread-safe for readers |
172 | synchronized (this) { |
173 | if (this.name == null) { |
174 | preLazyInit(); |
175 | this.name = this.ast.newQualifiedName( |
176 | new SimpleName(this.ast), new SimpleName(this.ast)); |
177 | postLazyInit(this.name, NAME_PROPERTY); |
178 | } |
179 | } |
180 | } |
181 | return this.name; |
182 | } |
183 | |
184 | /** |
185 | * Sets the name of the service. |
186 | * |
187 | * @param name the new service name |
188 | * @exception IllegalArgumentException if: |
189 | * <ul> |
190 | * <li>the node belongs to a different AST</li> |
191 | * <li>the node already has a parent</li> |
192 | * </ul> |
193 | */ |
194 | public void setName(Name name) { |
195 | if (name == null) { |
196 | throw new IllegalArgumentException(); |
197 | } |
198 | ASTNode oldChild = this.name; |
199 | preReplaceChild(oldChild, name, NAME_PROPERTY); |
200 | this.name = name; |
201 | postReplaceChild(oldChild, name, NAME_PROPERTY); |
202 | } |
203 | |
204 | /** |
205 | * Returns the live ordered list of implementations for the interface in this provides directive. |
206 | * |
207 | * @return the live list of implementations for the interface |
208 | * (element type: {@link Name}) |
209 | */ |
210 | public List implementations() { |
211 | return this.implementations; |
212 | } |
213 | |
214 | @Override |
215 | int memSize() { |
216 | return BASE_NODE_SIZE + 2 * 4; |
217 | } |
218 | |
219 | @Override |
220 | int treeSize() { |
221 | return |
222 | memSize() |
223 | + (this.name == null ? 0 : getName().treeSize()) |
224 | + this.implementations.listSize(); |
225 | } |
226 | } |
227 |
Members