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

Thu, 04 Oct 2012 13:04:53 +0100

author
mcimadamore
date
Thu, 04 Oct 2012 13:04:53 +0100
changeset 1347
1408af4cd8b0
parent 1326
30c36e23f154
child 1359
25e14ad23cef
permissions
-rw-r--r--

7177387: Add target-typing support in method context
Summary: Add support for deferred types and speculative attribution
Reviewed-by: jjg, dlsmith

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

mercurial