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 | import org.eclipse.jdt.core.compiler.InvalidInputException; |
21 | import org.eclipse.jdt.internal.compiler.parser.Scanner; |
22 | import org.eclipse.jdt.internal.compiler.parser.TerminalTokens; |
23 | |
24 | /** |
25 | * AST node for a Javadoc-style doc comment. |
26 | * <pre> |
27 | * Javadoc: |
28 | * <b>/** </b> { TagElement } <b>*</b><b>/</b> |
29 | * </pre> |
30 | * |
31 | * @since 2.0 |
32 | * @noinstantiate This class is not intended to be instantiated by clients. |
33 | */ |
34 | @SuppressWarnings({"rawtypes", "unchecked"}) |
35 | public class Javadoc extends Comment { |
36 | |
37 | /** |
38 | * The "comment" structural property of this node type (type: {@link String}) (JLS2 API only). |
39 | * @since 3.0 |
40 | * @deprecated Replaced by {@link #TAGS_PROPERTY} in the JLS3 API. |
41 | */ |
42 | public static final SimplePropertyDescriptor COMMENT_PROPERTY = |
43 | new SimplePropertyDescriptor(Javadoc.class, "comment", String.class, MANDATORY); //$NON-NLS-1$ |
44 | |
45 | /** |
46 | * The "tags" structural property of this node type (element type: {@link TagElement}). |
47 | * @since 3.1 |
48 | */ |
49 | public static final ChildListPropertyDescriptor TAGS_PROPERTY = |
50 | new ChildListPropertyDescriptor(Javadoc.class, "tags", TagElement.class, CYCLE_RISK); //$NON-NLS-1$ |
51 | |
52 | |
53 | /** |
54 | * A list of property descriptors (element type: |
55 | * {@link StructuralPropertyDescriptor}), |
56 | * or null if uninitialized. |
57 | * @since 3.0 |
58 | */ |
59 | private static final List PROPERTY_DESCRIPTORS_2_0; |
60 | |
61 | /** |
62 | * A list of property descriptors (element type: |
63 | * {@link StructuralPropertyDescriptor}), |
64 | * or null if uninitialized. |
65 | * @since 3.1 |
66 | */ |
67 | private static final List PROPERTY_DESCRIPTORS_3_0; |
68 | |
69 | static { |
70 | List properyList = new ArrayList(3); |
71 | createPropertyList(Javadoc.class, properyList); |
72 | addProperty(COMMENT_PROPERTY, properyList); |
73 | addProperty(TAGS_PROPERTY, properyList); |
74 | PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(properyList); |
75 | |
76 | properyList = new ArrayList(2); |
77 | createPropertyList(Javadoc.class, properyList); |
78 | addProperty(TAGS_PROPERTY, properyList); |
79 | PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(properyList); |
80 | } |
81 | |
82 | /** |
83 | * Returns a list of structural property descriptors for this node type. |
84 | * Clients must not modify the result. |
85 | * |
86 | * @param apiLevel the API level; one of the |
87 | * <code>AST.JLS*</code> constants |
88 | * @return a list of property descriptors (element type: |
89 | * {@link StructuralPropertyDescriptor}) |
90 | * @since 3.0 |
91 | */ |
92 | public static List propertyDescriptors(int apiLevel) { |
93 | if (apiLevel == AST.JLS2_INTERNAL) { |
94 | return PROPERTY_DESCRIPTORS_2_0; |
95 | } else { |
96 | return PROPERTY_DESCRIPTORS_3_0; |
97 | } |
98 | } |
99 | |
100 | /** |
101 | * Canonical minimal doc comment. |
102 | * @since 3.0 |
103 | */ |
104 | private static final String MINIMAL_DOC_COMMENT = "/** */";//$NON-NLS-1$ |
105 | |
106 | /** |
107 | * The doc comment string, including opening and closing comment |
108 | * delimiters; defaults to a minimal Javadoc comment. |
109 | * @deprecated The comment string was replaced in the 3.0 release |
110 | * by a representation of the structure of the doc comment. |
111 | * For backwards compatibility, it is still funcational as before. |
112 | */ |
113 | private String comment = MINIMAL_DOC_COMMENT; |
114 | |
115 | /** |
116 | * The list of tag elements (element type: {@link TagElement}). |
117 | * Defaults to an empty list. |
118 | * @since 3.0 |
119 | */ |
120 | private ASTNode.NodeList tags = |
121 | new ASTNode.NodeList(TAGS_PROPERTY); |
122 | |
123 | /** |
124 | * Creates a new AST node for a doc comment owned by the given AST. |
125 | * The new node has an empty list of tag elements (and, for backwards |
126 | * compatability, an unspecified, but legal, doc comment string). |
127 | * <p> |
128 | * N.B. This constructor is package-private; all subclasses must be |
129 | * declared in the same package; clients are unable to declare |
130 | * additional subclasses. |
131 | * </p> |
132 | * |
133 | * @param ast the AST that is to own this node |
134 | */ |
135 | Javadoc(AST ast) { |
136 | super(ast); |
137 | } |
138 | |
139 | @Override |
140 | final List internalStructuralPropertiesForType(int apiLevel) { |
141 | return propertyDescriptors(apiLevel); |
142 | } |
143 | |
144 | @Override |
145 | final Object internalGetSetObjectProperty(SimplePropertyDescriptor property, boolean get, Object value) { |
146 | if (property == COMMENT_PROPERTY) { |
147 | if (get) { |
148 | return getComment(); |
149 | } else { |
150 | setComment((String) value); |
151 | return null; |
152 | } |
153 | } |
154 | // allow default implementation to flag the error |
155 | return super.internalGetSetObjectProperty(property, get, value); |
156 | } |
157 | |
158 | @Override |
159 | final List internalGetChildListProperty(ChildListPropertyDescriptor property) { |
160 | if (property == TAGS_PROPERTY) { |
161 | return tags(); |
162 | } |
163 | // allow default implementation to flag the error |
164 | return super.internalGetChildListProperty(property); |
165 | } |
166 | |
167 | @Override |
168 | final int getNodeType0() { |
169 | return JAVADOC; |
170 | } |
171 | |
172 | @Override |
173 | ASTNode clone0(AST target) { |
174 | Javadoc result = new Javadoc(target); |
175 | result.setSourceRange(getStartPosition(), getLength()); |
176 | if (this.ast.apiLevel == AST.JLS2_INTERNAL) { |
177 | result.setComment(getComment()); |
178 | } |
179 | result.tags().addAll(ASTNode.copySubtrees(target, tags())); |
180 | return result; |
181 | } |
182 | |
183 | @Override |
184 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
185 | // dispatch to correct overloaded match method |
186 | return matcher.match(this, other); |
187 | } |
188 | |
189 | @Override |
190 | void accept0(ASTVisitor visitor) { |
191 | boolean visitChildren = visitor.visit(this); |
192 | if (visitChildren) { |
193 | // visit children in normal left to right reading order |
194 | acceptChildren(visitor, this.tags); |
195 | } |
196 | visitor.endVisit(this); |
197 | } |
198 | |
199 | /** |
200 | * Returns the doc comment string, including the starting |
201 | * and ending comment delimiters, and any embedded line breaks. |
202 | * |
203 | * @return the doc comment string |
204 | * @exception UnsupportedOperationException if this operation is used in |
205 | * an AST later than JLS2 |
206 | * @deprecated The comment string was replaced in the 3.0 release |
207 | * by a representation of the structure of the doc comment. |
208 | * See {@link #tags() tags}. |
209 | */ |
210 | public String getComment() { |
211 | supportedOnlyIn2(); |
212 | return this.comment; |
213 | } |
214 | |
215 | /** |
216 | * Sets or clears the doc comment string. The documentation |
217 | * string must include the starting and ending comment delimiters, |
218 | * and any embedded line breaks. |
219 | * |
220 | * @param docComment the doc comment string |
221 | * @exception IllegalArgumentException if the Java comment string is invalid |
222 | * @exception UnsupportedOperationException if this operation is used in |
223 | * an AST later than JLS2 |
224 | * @deprecated The comment string was replaced in the 3.0 release |
225 | * by a representation of the structure of the doc comment. |
226 | * See {@link #tags() tags}. |
227 | */ |
228 | public void setComment(String docComment) { |
229 | supportedOnlyIn2(); |
230 | if (docComment == null) { |
231 | throw new IllegalArgumentException(); |
232 | } |
233 | char[] source = docComment.toCharArray(); |
234 | Scanner scanner = this.ast.scanner; |
235 | scanner.resetTo(0, source.length); |
236 | scanner.setSource(source); |
237 | try { |
238 | int token; |
239 | boolean onlyOneComment = false; |
240 | while ((token = scanner.getNextToken()) != TerminalTokens.TokenNameEOF) { |
241 | switch(token) { |
242 | case TerminalTokens.TokenNameCOMMENT_JAVADOC : |
243 | if (onlyOneComment) { |
244 | throw new IllegalArgumentException(); |
245 | } |
246 | onlyOneComment = true; |
247 | break; |
248 | default: |
249 | onlyOneComment = false; |
250 | } |
251 | } |
252 | if (!onlyOneComment) { |
253 | throw new IllegalArgumentException(); |
254 | } |
255 | } catch (InvalidInputException e) { |
256 | throw new IllegalArgumentException(e); |
257 | } |
258 | preValueChange(COMMENT_PROPERTY); |
259 | this.comment = docComment; |
260 | postValueChange(COMMENT_PROPERTY); |
261 | } |
262 | |
263 | /** |
264 | * Returns the live list of tag elements that make up this doc |
265 | * comment. |
266 | * <p> |
267 | * The tag elements cover everything except the starting and ending |
268 | * comment delimiters, and generally omit leading whitespace |
269 | * (including a leading "*") and embedded line breaks. |
270 | * The first tag element of a typical doc comment represents |
271 | * all the material before the first explicit doc tag; this |
272 | * first tag element has a <code>null</code> tag name and |
273 | * generally contains 1 or more {@link TextElement}s, |
274 | * and possibly interspersed with tag elements for nested tags |
275 | * like "{@link String String}". |
276 | * Subsequent tag elements represent successive top-level doc |
277 | * tag (e.g., "@param", "@return", "@see"). |
278 | * </p> |
279 | * <p> |
280 | * Adding and removing nodes from this list affects this node |
281 | * dynamically. |
282 | * </p> |
283 | * |
284 | * @return the live list of tag elements in this doc comment |
285 | * (element type: {@link TagElement}) |
286 | * @since 3.0 |
287 | */ |
288 | public List tags() { |
289 | return this.tags; |
290 | } |
291 | |
292 | @Override |
293 | int memSize() { |
294 | int size = super.memSize() + 2 * 4; |
295 | if (this.comment != MINIMAL_DOC_COMMENT) { |
296 | // anything other than the default string takes space |
297 | size += stringSize(this.comment); |
298 | } |
299 | return size; |
300 | } |
301 | |
302 | @Override |
303 | int treeSize() { |
304 | return memSize() + this.tags.listSize(); |
305 | } |
306 | } |
307 |
Members