1 /*******************************************************************************
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpdt.core.dom;
14 import java.util.ArrayList;
15 import java.util.List;
17 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
18 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
19 import net.sourceforge.phpdt.internal.compiler.parser.TerminalTokens;
22 * AST node for a Javadoc-style doc comment.
25 * <b>/** </b> { TagElement } <b>*</b><b>/</b>
29 * @noinstantiate This class is not intended to be instantiated by clients.
31 public class Javadoc extends Comment {
34 * The "comment" structural property of this node type (JLS2 API only).
36 * @deprecated Replaced by {@link #TAGS_PROPERTY} in the JLS3 API.
38 public static final SimplePropertyDescriptor COMMENT_PROPERTY =
39 new SimplePropertyDescriptor(Javadoc.class, "comment", String.class, MANDATORY); //$NON-NLS-1$
42 * The "tags" structural property of this node type.
45 public static final ChildListPropertyDescriptor TAGS_PROPERTY =
46 new ChildListPropertyDescriptor(Javadoc.class, "tags", TagElement.class, CYCLE_RISK); //$NON-NLS-1$
50 * A list of property descriptors (element type:
51 * {@link StructuralPropertyDescriptor}),
52 * or null if uninitialized.
55 private static final List PROPERTY_DESCRIPTORS_2_0;
58 * A list of property descriptors (element type:
59 * {@link StructuralPropertyDescriptor}),
60 * or null if uninitialized.
63 private static final List PROPERTY_DESCRIPTORS_3_0;
66 List properyList = new ArrayList(3);
67 createPropertyList(Javadoc.class, properyList);
68 addProperty(COMMENT_PROPERTY, properyList);
69 addProperty(TAGS_PROPERTY, properyList);
70 PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(properyList);
72 properyList = new ArrayList(2);
73 createPropertyList(Javadoc.class, properyList);
74 addProperty(TAGS_PROPERTY, properyList);
75 PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(properyList);
79 * Returns a list of structural property descriptors for this node type.
80 * Clients must not modify the result.
82 * @param apiLevel the API level; one of the
83 * <code>AST.JLS*</code> constants
84 * @return a list of property descriptors (element type:
85 * {@link StructuralPropertyDescriptor})
88 public static List propertyDescriptors(int apiLevel) {
89 if (apiLevel == AST.JLS2_INTERNAL) {
90 return PROPERTY_DESCRIPTORS_2_0;
92 return PROPERTY_DESCRIPTORS_3_0;
97 * Canonical minimal doc comment.
100 private static final String MINIMAL_DOC_COMMENT = "/** */";//$NON-NLS-1$
103 * The doc comment string, including opening and closing comment
104 * delimiters; defaults to a minimal Javadoc comment.
105 * @deprecated The comment string was replaced in the 3.0 release
106 * by a representation of the structure of the doc comment.
107 * For backwards compatibility, it is still funcational as before.
109 private String comment = MINIMAL_DOC_COMMENT;
112 * The list of tag elements (element type: <code>TagElement</code>).
113 * Defaults to an empty list.
116 private ASTNode.NodeList tags =
117 new ASTNode.NodeList(TAGS_PROPERTY);
120 * Creates a new AST node for a doc comment owned by the given AST.
121 * The new node has an empty list of tag elements (and, for backwards
122 * compatability, an unspecified, but legal, doc comment string).
124 * N.B. This constructor is package-private; all subclasses must be
125 * declared in the same package; clients are unable to declare
126 * additional subclasses.
129 * @param ast the AST that is to own this node
135 /* (omit javadoc for this method)
136 * Method declared on ASTNode.
138 final List internalStructuralPropertiesForType(int apiLevel) {
139 return propertyDescriptors(apiLevel);
142 /* (omit javadoc for this method)
143 * Method declared on ASTNode.
145 final Object internalGetSetObjectProperty(SimplePropertyDescriptor property, boolean get, Object value) {
146 if (property == COMMENT_PROPERTY) {
150 setComment((String) value);
154 // allow default implementation to flag the error
155 return super.internalGetSetObjectProperty(property, get, value);
158 /* (omit javadoc for this method)
159 * Method declared on ASTNode.
161 final List internalGetChildListProperty(ChildListPropertyDescriptor property) {
162 if (property == TAGS_PROPERTY) {
165 // allow default implementation to flag the error
166 return super.internalGetChildListProperty(property);
169 /* (omit javadoc for this method)
170 * Method declared on ASTNode.
172 final int getNodeType0() {
176 /* (omit javadoc for this method)
177 * Method declared on ASTNode.
179 ASTNode clone0(AST target) {
180 Javadoc result = new Javadoc(target);
181 result.setSourceRange(this.getStartPosition(), this.getLength());
182 if (this.ast.apiLevel == AST.JLS2_INTERNAL) {
183 result.setComment(getComment());
185 result.tags().addAll(ASTNode.copySubtrees(target, tags()));
189 /* (omit javadoc for this method)
190 * Method declared on ASTNode.
192 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
193 // dispatch to correct overloaded match method
194 return matcher.match(this, other);
197 /* (omit javadoc for this method)
198 * Method declared on ASTNode.
200 void accept0(ASTVisitor visitor) {
201 boolean visitChildren = visitor.visit(this);
203 // visit children in normal left to right reading order
204 acceptChildren(visitor, this.tags);
206 visitor.endVisit(this);
210 * Returns the doc comment string, including the starting
211 * and ending comment delimiters, and any embedded line breaks.
213 * @return the doc comment string
214 * @exception UnsupportedOperationException if this operation is used in
215 * an AST later than JLS2
216 * @deprecated The comment string was replaced in the 3.0 release
217 * by a representation of the structure of the doc comment.
218 * See {@link #tags() tags}.
220 public String getComment() {
226 * Sets or clears the doc comment string. The documentation
227 * string must include the starting and ending comment delimiters,
228 * and any embedded line breaks.
230 * @param docComment the doc comment string
231 * @exception IllegalArgumentException if the Java comment string is invalid
232 * @exception UnsupportedOperationException if this operation is used in
233 * an AST later than JLS2
234 * @deprecated The comment string was replaced in the 3.0 release
235 * by a representation of the structure of the doc comment.
236 * See {@link #tags() tags}.
238 public void setComment(String docComment) {
240 if (docComment == null) {
241 throw new IllegalArgumentException();
243 char[] source = docComment.toCharArray();
244 Scanner scanner = this.ast.scanner;
245 scanner.resetTo(0, source.length);
246 scanner.setSource(source);
249 boolean onlyOneComment = false;
250 while ((token = scanner.getNextToken()) != TerminalTokens.TokenNameEOF) {
252 case TerminalTokens.TokenNameCOMMENT_JAVADOC :
253 if (onlyOneComment) {
254 throw new IllegalArgumentException();
256 onlyOneComment = true;
259 onlyOneComment = false;
262 if (!onlyOneComment) {
263 throw new IllegalArgumentException();
265 } catch (InvalidInputException e) {
266 throw new IllegalArgumentException();
268 preValueChange(COMMENT_PROPERTY);
269 this.comment = docComment;
270 postValueChange(COMMENT_PROPERTY);
274 * Returns the live list of tag elements that make up this doc
277 * The tag elements cover everything except the starting and ending
278 * comment delimiters, and generally omit leading whitespace
279 * (including a leading "*") and embedded line breaks.
280 * The first tag element of a typical doc comment represents
281 * all the material before the first explicit doc tag; this
282 * first tag element has a <code>null</code> tag name and
283 * generally contains 1 or more {@link TextElement}s,
284 * and possibly interspersed with tag elements for nested tags
285 * like "{@link String String}".
286 * Subsequent tag elements represent successive top-level doc
287 * tag (e.g., "@param", "@return", "@see").
290 * Adding and removing nodes from this list affects this node
294 * @return the live list of tag elements in this doc comment
295 * (element type: <code>TagElement</code>)
302 /* (omit javadoc for this method)
303 * Method declared on ASTNode.
306 int size = super.memSize() + 2 * 4;
307 if (this.comment != MINIMAL_DOC_COMMENT) {
308 // anything other than the default string takes space
309 size += stringSize(this.comment);
314 /* (omit javadoc for this method)
315 * Method declared on ASTNode.
318 return memSize() + this.tags.listSize();