src/share/classes/com/sun/tools/javadoc/Comment.java

Mon, 27 Sep 2010 14:20:39 -0700

author
jjg
date
Mon, 27 Sep 2010 14:20:39 -0700
changeset 695
3c9b64e55c5d
parent 554
9d9f26857129
child 1065
e9f118c2bd3c
permissions
-rw-r--r--

6877202: Elements.getDocComment() is not getting JavaDocComments
6861094: javac -Xprint <file> does not print comments
6985205: access to tree positions and doc comments may be lost across annotation processing rounds
Reviewed-by: darcy

duke@1 1 /*
ohair@554 2 * Copyright (c) 1997, 2009, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javadoc;
duke@1 27
duke@1 28 import java.util.Locale;
duke@1 29
duke@1 30 import com.sun.javadoc.*;
duke@1 31
duke@1 32 import com.sun.tools.javac.util.ListBuffer;
duke@1 33
duke@1 34 /**
duke@1 35 * Comment contains all information in comment part.
duke@1 36 * It allows users to get first sentence of this comment, get
duke@1 37 * comment for different tags...
duke@1 38 *
duke@1 39 * @author Kaiyang Liu (original)
duke@1 40 * @author Robert Field (rewrite)
duke@1 41 * @author Atul M Dambalkar
duke@1 42 * @author Neal Gafter (rewrite)
duke@1 43 */
duke@1 44 class Comment {
duke@1 45
duke@1 46 /**
duke@1 47 * sorted comments with different tags.
duke@1 48 */
duke@1 49 private final ListBuffer<Tag> tagList = new ListBuffer<Tag>();
duke@1 50
duke@1 51 /**
duke@1 52 * text minus any tags.
duke@1 53 */
duke@1 54 private String text;
duke@1 55
duke@1 56 /**
duke@1 57 * Doc environment
duke@1 58 */
duke@1 59 private final DocEnv docenv;
duke@1 60
duke@1 61 /**
duke@1 62 * constructor of Comment.
duke@1 63 */
duke@1 64 Comment(final DocImpl holder, final String commentString) {
duke@1 65 this.docenv = holder.env;
duke@1 66
duke@1 67 /**
duke@1 68 * Separate the comment into the text part and zero to N tags.
duke@1 69 * Simple state machine is in one of three states:
duke@1 70 * <pre>
duke@1 71 * IN_TEXT: parsing the comment text or tag text.
duke@1 72 * TAG_NAME: parsing the name of a tag.
duke@1 73 * TAG_GAP: skipping through the gap between the tag name and
duke@1 74 * the tag text.
duke@1 75 * </pre>
duke@1 76 */
jjg@198 77 @SuppressWarnings("fallthrough")
duke@1 78 class CommentStringParser {
duke@1 79 /**
duke@1 80 * The entry point to the comment string parser
duke@1 81 */
duke@1 82 void parseCommentStateMachine() {
duke@1 83 final int IN_TEXT = 1;
duke@1 84 final int TAG_GAP = 2;
duke@1 85 final int TAG_NAME = 3;
duke@1 86 int state = TAG_GAP;
duke@1 87 boolean newLine = true;
duke@1 88 String tagName = null;
duke@1 89 int tagStart = 0;
duke@1 90 int textStart = 0;
duke@1 91 int lastNonWhite = -1;
duke@1 92 int len = commentString.length();
duke@1 93 for (int inx = 0; inx < len; ++inx) {
duke@1 94 char ch = commentString.charAt(inx);
duke@1 95 boolean isWhite = Character.isWhitespace(ch);
duke@1 96 switch (state) {
duke@1 97 case TAG_NAME:
duke@1 98 if (isWhite) {
duke@1 99 tagName = commentString.substring(tagStart, inx);
duke@1 100 state = TAG_GAP;
duke@1 101 }
duke@1 102 break;
duke@1 103 case TAG_GAP:
duke@1 104 if (isWhite) {
duke@1 105 break;
duke@1 106 }
duke@1 107 textStart = inx;
duke@1 108 state = IN_TEXT;
duke@1 109 /* fall thru */
duke@1 110 case IN_TEXT:
duke@1 111 if (newLine && ch == '@') {
duke@1 112 parseCommentComponent(tagName, textStart,
duke@1 113 lastNonWhite+1);
duke@1 114 tagStart = inx;
duke@1 115 state = TAG_NAME;
duke@1 116 }
duke@1 117 break;
duke@1 118 };
duke@1 119 if (ch == '\n') {
duke@1 120 newLine = true;
duke@1 121 } else if (!isWhite) {
duke@1 122 lastNonWhite = inx;
duke@1 123 newLine = false;
duke@1 124 }
duke@1 125 }
duke@1 126 // Finish what's currently being processed
duke@1 127 switch (state) {
duke@1 128 case TAG_NAME:
duke@1 129 tagName = commentString.substring(tagStart, len);
duke@1 130 /* fall thru */
duke@1 131 case TAG_GAP:
duke@1 132 textStart = len;
duke@1 133 /* fall thru */
duke@1 134 case IN_TEXT:
duke@1 135 parseCommentComponent(tagName, textStart, lastNonWhite+1);
duke@1 136 break;
duke@1 137 };
duke@1 138 }
duke@1 139
duke@1 140 /**
duke@1 141 * Save away the last parsed item.
duke@1 142 */
duke@1 143 void parseCommentComponent(String tagName,
duke@1 144 int from, int upto) {
duke@1 145 String tx = upto <= from ? "" : commentString.substring(from, upto);
duke@1 146 if (tagName == null) {
duke@1 147 text = tx;
duke@1 148 } else {
duke@1 149 TagImpl tag;
duke@1 150 if (tagName.equals("@exception") || tagName.equals("@throws")) {
duke@1 151 warnIfEmpty(tagName, tx);
duke@1 152 tag = new ThrowsTagImpl(holder, tagName, tx);
duke@1 153 } else if (tagName.equals("@param")) {
duke@1 154 warnIfEmpty(tagName, tx);
duke@1 155 tag = new ParamTagImpl(holder, tagName, tx);
duke@1 156 } else if (tagName.equals("@see")) {
duke@1 157 warnIfEmpty(tagName, tx);
duke@1 158 tag = new SeeTagImpl(holder, tagName, tx);
duke@1 159 } else if (tagName.equals("@serialField")) {
duke@1 160 warnIfEmpty(tagName, tx);
duke@1 161 tag = new SerialFieldTagImpl(holder, tagName, tx);
duke@1 162 } else if (tagName.equals("@return")) {
duke@1 163 warnIfEmpty(tagName, tx);
duke@1 164 tag = new TagImpl(holder, tagName, tx);
duke@1 165 } else if (tagName.equals("@author")) {
duke@1 166 warnIfEmpty(tagName, tx);
duke@1 167 tag = new TagImpl(holder, tagName, tx);
duke@1 168 } else if (tagName.equals("@version")) {
duke@1 169 warnIfEmpty(tagName, tx);
duke@1 170 tag = new TagImpl(holder, tagName, tx);
duke@1 171 } else {
duke@1 172 tag = new TagImpl(holder, tagName, tx);
duke@1 173 }
duke@1 174 tagList.append(tag);
duke@1 175 }
duke@1 176 }
duke@1 177
duke@1 178 void warnIfEmpty(String tagName, String tx) {
duke@1 179 if (tx.length() == 0) {
duke@1 180 docenv.warning(holder, "tag.tag_has_no_arguments", tagName);
duke@1 181 }
duke@1 182 }
duke@1 183
duke@1 184 }
duke@1 185
duke@1 186 new CommentStringParser().parseCommentStateMachine();
duke@1 187 }
duke@1 188
duke@1 189 /**
duke@1 190 * Return the text of the comment.
duke@1 191 */
duke@1 192 String commentText() {
duke@1 193 return text;
duke@1 194 }
duke@1 195
duke@1 196 /**
duke@1 197 * Return all tags in this comment.
duke@1 198 */
duke@1 199 Tag[] tags() {
duke@1 200 return tagList.toArray(new Tag[tagList.length()]);
duke@1 201 }
duke@1 202
duke@1 203 /**
duke@1 204 * Return tags of the specified kind in this comment.
duke@1 205 */
duke@1 206 Tag[] tags(String tagname) {
duke@1 207 ListBuffer<Tag> found = new ListBuffer<Tag>();
duke@1 208 String target = tagname;
duke@1 209 if (target.charAt(0) != '@') {
duke@1 210 target = "@" + target;
duke@1 211 }
duke@1 212 for (Tag tag : tagList) {
duke@1 213 if (tag.kind().equals(target)) {
duke@1 214 found.append(tag);
duke@1 215 }
duke@1 216 }
duke@1 217 return found.toArray(new Tag[found.length()]);
duke@1 218 }
duke@1 219
duke@1 220 /**
duke@1 221 * Return throws tags in this comment.
duke@1 222 */
duke@1 223 ThrowsTag[] throwsTags() {
duke@1 224 ListBuffer<ThrowsTag> found = new ListBuffer<ThrowsTag>();
duke@1 225 for (Tag next : tagList) {
duke@1 226 if (next instanceof ThrowsTag) {
duke@1 227 found.append((ThrowsTag)next);
duke@1 228 }
duke@1 229 }
duke@1 230 return found.toArray(new ThrowsTag[found.length()]);
duke@1 231 }
duke@1 232
duke@1 233 /**
duke@1 234 * Return param tags (excluding type param tags) in this comment.
duke@1 235 */
duke@1 236 ParamTag[] paramTags() {
duke@1 237 return paramTags(false);
duke@1 238 }
duke@1 239
duke@1 240 /**
duke@1 241 * Return type param tags in this comment.
duke@1 242 */
duke@1 243 ParamTag[] typeParamTags() {
duke@1 244 return paramTags(true);
duke@1 245 }
duke@1 246
duke@1 247 /**
duke@1 248 * Return param tags in this comment. If typeParams is true
duke@1 249 * include only type param tags, otherwise include only ordinary
duke@1 250 * param tags.
duke@1 251 */
duke@1 252 private ParamTag[] paramTags(boolean typeParams) {
duke@1 253 ListBuffer<ParamTag> found = new ListBuffer<ParamTag>();
duke@1 254 for (Tag next : tagList) {
duke@1 255 if (next instanceof ParamTag) {
duke@1 256 ParamTag p = (ParamTag)next;
duke@1 257 if (typeParams == p.isTypeParameter()) {
duke@1 258 found.append(p);
duke@1 259 }
duke@1 260 }
duke@1 261 }
duke@1 262 return found.toArray(new ParamTag[found.length()]);
duke@1 263 }
duke@1 264
duke@1 265 /**
duke@1 266 * Return see also tags in this comment.
duke@1 267 */
duke@1 268 SeeTag[] seeTags() {
duke@1 269 ListBuffer<SeeTag> found = new ListBuffer<SeeTag>();
duke@1 270 for (Tag next : tagList) {
duke@1 271 if (next instanceof SeeTag) {
duke@1 272 found.append((SeeTag)next);
duke@1 273 }
duke@1 274 }
duke@1 275 return found.toArray(new SeeTag[found.length()]);
duke@1 276 }
duke@1 277
duke@1 278 /**
duke@1 279 * Return serialField tags in this comment.
duke@1 280 */
duke@1 281 SerialFieldTag[] serialFieldTags() {
duke@1 282 ListBuffer<SerialFieldTag> found = new ListBuffer<SerialFieldTag>();
duke@1 283 for (Tag next : tagList) {
duke@1 284 if (next instanceof SerialFieldTag) {
duke@1 285 found.append((SerialFieldTag)next);
duke@1 286 }
duke@1 287 }
duke@1 288 return found.toArray(new SerialFieldTag[found.length()]);
duke@1 289 }
duke@1 290
duke@1 291 /**
duke@1 292 * Return array of tags with text and inline See Tags for a Doc comment.
duke@1 293 */
duke@1 294 static Tag[] getInlineTags(DocImpl holder, String inlinetext) {
duke@1 295 ListBuffer<Tag> taglist = new ListBuffer<Tag>();
duke@1 296 int delimend = 0, textstart = 0, len = inlinetext.length();
duke@1 297 DocEnv docenv = holder.env;
duke@1 298
duke@1 299 if (len == 0) {
duke@1 300 return taglist.toArray(new Tag[taglist.length()]);
duke@1 301 }
duke@1 302 while (true) {
duke@1 303 int linkstart;
duke@1 304 if ((linkstart = inlineTagFound(holder, inlinetext,
duke@1 305 textstart)) == -1) {
duke@1 306 taglist.append(new TagImpl(holder, "Text",
duke@1 307 inlinetext.substring(textstart)));
duke@1 308 break;
duke@1 309 } else {
duke@1 310 int seetextstart = linkstart;
duke@1 311 for (int i = linkstart; i < inlinetext.length(); i++) {
duke@1 312 char c = inlinetext.charAt(i);
duke@1 313 if (Character.isWhitespace(c) ||
duke@1 314 c == '}') {
duke@1 315 seetextstart = i;
duke@1 316 break;
duke@1 317 }
duke@1 318 }
duke@1 319 String linkName = inlinetext.substring(linkstart+2, seetextstart);
duke@1 320 //Move past the white space after the inline tag name.
duke@1 321 while (Character.isWhitespace(inlinetext.
duke@1 322 charAt(seetextstart))) {
duke@1 323 if (inlinetext.length() <= seetextstart) {
duke@1 324 taglist.append(new TagImpl(holder, "Text",
duke@1 325 inlinetext.substring(textstart, seetextstart)));
duke@1 326 docenv.warning(holder,
duke@1 327 "tag.Improper_Use_Of_Link_Tag",
duke@1 328 inlinetext);
duke@1 329 return taglist.toArray(new Tag[taglist.length()]);
duke@1 330 } else {
duke@1 331 seetextstart++;
duke@1 332 }
duke@1 333 }
duke@1 334 taglist.append(new TagImpl(holder, "Text",
duke@1 335 inlinetext.substring(textstart, linkstart)));
duke@1 336 textstart = seetextstart; // this text is actually seetag
duke@1 337 if ((delimend = findInlineTagDelim(inlinetext, textstart)) == -1) {
duke@1 338 //Missing closing '}' character.
duke@1 339 // store the text as it is with the {@link.
duke@1 340 taglist.append(new TagImpl(holder, "Text",
duke@1 341 inlinetext.substring(textstart)));
duke@1 342 docenv.warning(holder,
duke@1 343 "tag.End_delimiter_missing_for_possible_SeeTag",
duke@1 344 inlinetext);
duke@1 345 return taglist.toArray(new Tag[taglist.length()]);
duke@1 346 } else {
duke@1 347 //Found closing '}' character.
duke@1 348 if (linkName.equals("see")
duke@1 349 || linkName.equals("link")
duke@1 350 || linkName.equals("linkplain")) {
duke@1 351 taglist.append( new SeeTagImpl(holder, "@" + linkName,
duke@1 352 inlinetext.substring(textstart, delimend)));
duke@1 353 } else {
duke@1 354 taglist.append( new TagImpl(holder, "@" + linkName,
duke@1 355 inlinetext.substring(textstart, delimend)));
duke@1 356 }
duke@1 357 textstart = delimend + 1;
duke@1 358 }
duke@1 359 }
duke@1 360 if (textstart == inlinetext.length()) {
duke@1 361 break;
duke@1 362 }
duke@1 363 }
duke@1 364 return taglist.toArray(new Tag[taglist.length()]);
duke@1 365 }
duke@1 366
duke@1 367 /**
duke@1 368 * Recursively find the index of the closing '}' character for an inline tag
duke@1 369 * and return it. If it can't be found, return -1.
duke@1 370 * @param inlineText the text to search in.
duke@1 371 * @param searchStart the index of the place to start searching at.
duke@1 372 * @return the index of the closing '}' character for an inline tag.
duke@1 373 * If it can't be found, return -1.
duke@1 374 */
duke@1 375 private static int findInlineTagDelim(String inlineText, int searchStart) {
duke@1 376 int delimEnd, nestedOpenBrace;
duke@1 377 if ((delimEnd = inlineText.indexOf("}", searchStart)) == -1) {
duke@1 378 return -1;
duke@1 379 } else if (((nestedOpenBrace = inlineText.indexOf("{", searchStart)) != -1) &&
duke@1 380 nestedOpenBrace < delimEnd){
duke@1 381 //Found a nested open brace.
duke@1 382 int nestedCloseBrace = findInlineTagDelim(inlineText, nestedOpenBrace + 1);
duke@1 383 return (nestedCloseBrace != -1) ?
duke@1 384 findInlineTagDelim(inlineText, nestedCloseBrace + 1) :
duke@1 385 -1;
duke@1 386 } else {
duke@1 387 return delimEnd;
duke@1 388 }
duke@1 389 }
duke@1 390
duke@1 391 /**
duke@1 392 * Recursively search for the string "{@" followed by
duke@1 393 * name of inline tag and white space,
duke@1 394 * if found
duke@1 395 * return the index of the text following the white space.
duke@1 396 * else
duke@1 397 * return -1.
duke@1 398 */
duke@1 399 private static int inlineTagFound(DocImpl holder, String inlinetext, int start) {
duke@1 400 DocEnv docenv = holder.env;
duke@1 401 int linkstart;
duke@1 402 if (start == inlinetext.length() ||
duke@1 403 (linkstart = inlinetext.indexOf("{@", start)) == -1) {
duke@1 404 return -1;
duke@1 405 } else if(inlinetext.indexOf('}', start) == -1) {
duke@1 406 //Missing '}'.
duke@1 407 docenv.warning(holder, "tag.Improper_Use_Of_Link_Tag",
duke@1 408 inlinetext.substring(linkstart, inlinetext.length()));
duke@1 409 return -1;
duke@1 410 } else {
duke@1 411 return linkstart;
duke@1 412 }
duke@1 413 }
duke@1 414
duke@1 415
duke@1 416 /**
duke@1 417 * Return array of tags for the locale specific first sentence in the text.
duke@1 418 */
duke@1 419 static Tag[] firstSentenceTags(DocImpl holder, String text) {
duke@1 420 DocLocale doclocale = holder.env.doclocale;
duke@1 421 return getInlineTags(holder,
duke@1 422 doclocale.localeSpecificFirstSentence(holder, text));
duke@1 423 }
duke@1 424
duke@1 425 /**
duke@1 426 * Return text for this Doc comment.
duke@1 427 */
duke@1 428 public String toString() {
duke@1 429 return text;
duke@1 430 }
duke@1 431 }

mercurial