001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2017 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018//////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks.javadoc; 021 022import java.util.ArrayDeque; 023import java.util.Arrays; 024import java.util.Collections; 025import java.util.Deque; 026import java.util.List; 027import java.util.Locale; 028import java.util.Set; 029import java.util.TreeSet; 030import java.util.regex.Pattern; 031import java.util.stream.Collectors; 032 033import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 034import com.puppycrawl.tools.checkstyle.api.DetailAST; 035import com.puppycrawl.tools.checkstyle.api.FileContents; 036import com.puppycrawl.tools.checkstyle.api.Scope; 037import com.puppycrawl.tools.checkstyle.api.TextBlock; 038import com.puppycrawl.tools.checkstyle.api.TokenTypes; 039import com.puppycrawl.tools.checkstyle.utils.CheckUtils; 040import com.puppycrawl.tools.checkstyle.utils.CommonUtils; 041import com.puppycrawl.tools.checkstyle.utils.ScopeUtils; 042 043/** 044 * Custom Checkstyle Check to validate Javadoc. 045 * 046 * @author Chris Stillwell 047 * @author Daniel Grenner 048 * @author Travis Schneeberger 049 */ 050public class JavadocStyleCheck 051 extends AbstractCheck { 052 053 /** Message property key for the Unclosed HTML message. */ 054 public static final String MSG_JAVADOC_MISSING = "javadoc.missing"; 055 056 /** Message property key for the Unclosed HTML message. */ 057 public static final String MSG_EMPTY = "javadoc.empty"; 058 059 /** Message property key for the Unclosed HTML message. */ 060 public static final String MSG_NO_PERIOD = "javadoc.noPeriod"; 061 062 /** Message property key for the Unclosed HTML message. */ 063 public static final String MSG_INCOMPLETE_TAG = "javadoc.incompleteTag"; 064 065 /** Message property key for the Unclosed HTML message. */ 066 public static final String MSG_UNCLOSED_HTML = "javadoc.unclosedHtml"; 067 068 /** Message property key for the Extra HTML message. */ 069 public static final String MSG_EXTRA_HTML = "javadoc.extraHtml"; 070 071 /** HTML tags that do not require a close tag. */ 072 private static final Set<String> SINGLE_TAGS = Collections.unmodifiableSortedSet( 073 Arrays.stream(new String[] {"br", "li", "dt", "dd", "hr", "img", "p", "td", "tr", "th", }) 074 .collect(Collectors.toCollection(TreeSet::new))); 075 076 /** HTML tags that are allowed in java docs. 077 * From https://www.w3schools.com/tags/default.asp 078 * The forms and structure tags are not allowed 079 */ 080 private static final Set<String> ALLOWED_TAGS = Collections.unmodifiableSortedSet( 081 Arrays.stream(new String[] { 082 "a", "abbr", "acronym", "address", "area", "b", "bdo", "big", 083 "blockquote", "br", "caption", "cite", "code", "colgroup", "dd", 084 "del", "div", "dfn", "dl", "dt", "em", "fieldset", "font", "h1", 085 "h2", "h3", "h4", "h5", "h6", "hr", "i", "img", "ins", "kbd", 086 "li", "ol", "p", "pre", "q", "samp", "small", "span", "strong", 087 "style", "sub", "sup", "table", "tbody", "td", "tfoot", "th", 088 "thead", "tr", "tt", "u", "ul", "var", }) 089 .collect(Collectors.toCollection(TreeSet::new))); 090 091 /** The scope to check. */ 092 private Scope scope = Scope.PRIVATE; 093 094 /** The visibility scope where Javadoc comments shouldn't be checked. **/ 095 private Scope excludeScope; 096 097 /** Format for matching the end of a sentence. */ 098 private Pattern endOfSentenceFormat = Pattern.compile("([.?!][ \t\n\r\f<])|([.?!]$)"); 099 100 /** 101 * Indicates if the first sentence should be checked for proper end of 102 * sentence punctuation. 103 */ 104 private boolean checkFirstSentence = true; 105 106 /** 107 * Indicates if the HTML within the comment should be checked. 108 */ 109 private boolean checkHtml = true; 110 111 /** 112 * Indicates if empty javadoc statements should be checked. 113 */ 114 private boolean checkEmptyJavadoc; 115 116 @Override 117 public int[] getDefaultTokens() { 118 return getAcceptableTokens(); 119 } 120 121 @Override 122 public int[] getAcceptableTokens() { 123 return new int[] { 124 TokenTypes.ANNOTATION_DEF, 125 TokenTypes.ANNOTATION_FIELD_DEF, 126 TokenTypes.CLASS_DEF, 127 TokenTypes.CTOR_DEF, 128 TokenTypes.ENUM_CONSTANT_DEF, 129 TokenTypes.ENUM_DEF, 130 TokenTypes.INTERFACE_DEF, 131 TokenTypes.METHOD_DEF, 132 TokenTypes.PACKAGE_DEF, 133 TokenTypes.VARIABLE_DEF, 134 }; 135 } 136 137 @Override 138 public int[] getRequiredTokens() { 139 return CommonUtils.EMPTY_INT_ARRAY; 140 } 141 142 @Override 143 public void visitToken(DetailAST ast) { 144 if (shouldCheck(ast)) { 145 final FileContents contents = getFileContents(); 146 // Need to start searching for the comment before the annotations 147 // that may exist. Even if annotations are not defined on the 148 // package, the ANNOTATIONS AST is defined. 149 final TextBlock textBlock = 150 contents.getJavadocBefore(ast.getFirstChild().getLineNo()); 151 152 checkComment(ast, textBlock); 153 } 154 } 155 156 /** 157 * Whether we should check this node. 158 * @param ast a given node. 159 * @return whether we should check a given node. 160 */ 161 private boolean shouldCheck(final DetailAST ast) { 162 boolean check = false; 163 164 if (ast.getType() == TokenTypes.PACKAGE_DEF) { 165 check = getFileContents().inPackageInfo(); 166 } 167 else if (!ScopeUtils.isInCodeBlock(ast)) { 168 final Scope customScope; 169 170 if (ScopeUtils.isInInterfaceOrAnnotationBlock(ast) 171 || ast.getType() == TokenTypes.ENUM_CONSTANT_DEF) { 172 customScope = Scope.PUBLIC; 173 } 174 else { 175 customScope = ScopeUtils.getScopeFromMods(ast.findFirstToken(TokenTypes.MODIFIERS)); 176 } 177 final Scope surroundingScope = ScopeUtils.getSurroundingScope(ast); 178 179 check = customScope.isIn(scope) 180 && (surroundingScope == null || surroundingScope.isIn(scope)) 181 && (excludeScope == null 182 || !customScope.isIn(excludeScope) 183 || surroundingScope != null 184 && !surroundingScope.isIn(excludeScope)); 185 } 186 return check; 187 } 188 189 /** 190 * Performs the various checks against the Javadoc comment. 191 * 192 * @param ast the AST of the element being documented 193 * @param comment the source lines that make up the Javadoc comment. 194 * 195 * @see #checkFirstSentenceEnding(DetailAST, TextBlock) 196 * @see #checkHtmlTags(DetailAST, TextBlock) 197 */ 198 private void checkComment(final DetailAST ast, final TextBlock comment) { 199 if (comment == null) { 200 // checking for missing docs in JavadocStyleCheck is not consistent 201 // with the rest of CheckStyle... Even though, I didn't think it 202 // made sense to make another check just to ensure that the 203 // package-info.java file actually contains package Javadocs. 204 if (getFileContents().inPackageInfo()) { 205 log(ast.getLineNo(), MSG_JAVADOC_MISSING); 206 } 207 } 208 else { 209 if (checkFirstSentence) { 210 checkFirstSentenceEnding(ast, comment); 211 } 212 213 if (checkHtml) { 214 checkHtmlTags(ast, comment); 215 } 216 217 if (checkEmptyJavadoc) { 218 checkJavadocIsNotEmpty(comment); 219 } 220 } 221 } 222 223 /** 224 * Checks that the first sentence ends with proper punctuation. This method 225 * uses a regular expression that checks for the presence of a period, 226 * question mark, or exclamation mark followed either by whitespace, an 227 * HTML element, or the end of string. This method ignores {_AT_inheritDoc} 228 * comments for TokenTypes that are valid for {_AT_inheritDoc}. 229 * 230 * @param ast the current node 231 * @param comment the source lines that make up the Javadoc comment. 232 */ 233 private void checkFirstSentenceEnding(final DetailAST ast, TextBlock comment) { 234 final String commentText = getCommentText(comment.getText()); 235 236 if (!commentText.isEmpty() 237 && !endOfSentenceFormat.matcher(commentText).find() 238 && !(commentText.startsWith("{@inheritDoc}") 239 && JavadocTagInfo.INHERIT_DOC.isValidOn(ast))) { 240 log(comment.getStartLineNo(), MSG_NO_PERIOD); 241 } 242 } 243 244 /** 245 * Checks that the Javadoc is not empty. 246 * 247 * @param comment the source lines that make up the Javadoc comment. 248 */ 249 private void checkJavadocIsNotEmpty(TextBlock comment) { 250 final String commentText = getCommentText(comment.getText()); 251 252 if (commentText.isEmpty()) { 253 log(comment.getStartLineNo(), MSG_EMPTY); 254 } 255 } 256 257 /** 258 * Returns the comment text from the Javadoc. 259 * @param comments the lines of Javadoc. 260 * @return a comment text String. 261 */ 262 private static String getCommentText(String... comments) { 263 final StringBuilder builder = new StringBuilder(); 264 for (final String line : comments) { 265 final int textStart = findTextStart(line); 266 267 if (textStart != -1) { 268 if (line.charAt(textStart) == '@') { 269 //we have found the tag section 270 break; 271 } 272 builder.append(line.substring(textStart)); 273 trimTail(builder); 274 builder.append('\n'); 275 } 276 } 277 278 return builder.toString().trim(); 279 } 280 281 /** 282 * Finds the index of the first non-whitespace character ignoring the 283 * Javadoc comment start and end strings (/** and */) as well as any 284 * leading asterisk. 285 * @param line the Javadoc comment line of text to scan. 286 * @return the int index relative to 0 for the start of text 287 * or -1 if not found. 288 */ 289 private static int findTextStart(String line) { 290 int textStart = -1; 291 for (int i = 0; i < line.length();) { 292 if (!Character.isWhitespace(line.charAt(i))) { 293 if (line.regionMatches(i, "/**", 0, "/**".length())) { 294 i += 2; 295 } 296 else if (line.regionMatches(i, "*/", 0, 2)) { 297 i++; 298 } 299 else if (line.charAt(i) != '*') { 300 textStart = i; 301 break; 302 } 303 } 304 i++; 305 } 306 return textStart; 307 } 308 309 /** 310 * Trims any trailing whitespace or the end of Javadoc comment string. 311 * @param builder the StringBuilder to trim. 312 */ 313 private static void trimTail(StringBuilder builder) { 314 int index = builder.length() - 1; 315 while (true) { 316 if (Character.isWhitespace(builder.charAt(index))) { 317 builder.deleteCharAt(index); 318 } 319 else if (index > 0 && builder.charAt(index) == '/' 320 && builder.charAt(index - 1) == '*') { 321 builder.deleteCharAt(index); 322 builder.deleteCharAt(index - 1); 323 index--; 324 while (builder.charAt(index - 1) == '*') { 325 builder.deleteCharAt(index - 1); 326 index--; 327 } 328 } 329 else { 330 break; 331 } 332 index--; 333 } 334 } 335 336 /** 337 * Checks the comment for HTML tags that do not have a corresponding close 338 * tag or a close tag that has no previous open tag. This code was 339 * primarily copied from the DocCheck checkHtml method. 340 * 341 * @param ast the node with the Javadoc 342 * @param comment the {@code TextBlock} which represents 343 * the Javadoc comment. 344 */ 345 // -@cs[ReturnCount] Too complex to break apart. 346 private void checkHtmlTags(final DetailAST ast, final TextBlock comment) { 347 final int lineNo = comment.getStartLineNo(); 348 final Deque<HtmlTag> htmlStack = new ArrayDeque<>(); 349 final String[] text = comment.getText(); 350 351 final TagParser parser = new TagParser(text, lineNo); 352 353 while (parser.hasNextTag()) { 354 final HtmlTag tag = parser.nextTag(); 355 356 if (tag.isIncompleteTag()) { 357 log(tag.getLineNo(), MSG_INCOMPLETE_TAG, 358 text[tag.getLineNo() - lineNo]); 359 return; 360 } 361 if (tag.isClosedTag()) { 362 //do nothing 363 continue; 364 } 365 if (tag.isCloseTag()) { 366 // We have found a close tag. 367 if (isExtraHtml(tag.getId(), htmlStack)) { 368 // No corresponding open tag was found on the stack. 369 log(tag.getLineNo(), 370 tag.getPosition(), 371 MSG_EXTRA_HTML, 372 tag); 373 } 374 else { 375 // See if there are any unclosed tags that were opened 376 // after this one. 377 checkUnclosedTags(htmlStack, tag.getId()); 378 } 379 } 380 else { 381 //We only push html tags that are allowed 382 if (isAllowedTag(tag)) { 383 htmlStack.push(tag); 384 } 385 } 386 } 387 388 // Identify any tags left on the stack. 389 // Skip multiples, like <b>...<b> 390 String lastFound = ""; 391 final List<String> typeParameters = CheckUtils.getTypeParameterNames(ast); 392 for (final HtmlTag htmlTag : htmlStack) { 393 if (!isSingleTag(htmlTag) 394 && !htmlTag.getId().equals(lastFound) 395 && !typeParameters.contains(htmlTag.getId())) { 396 log(htmlTag.getLineNo(), htmlTag.getPosition(), MSG_UNCLOSED_HTML, htmlTag); 397 lastFound = htmlTag.getId(); 398 } 399 } 400 } 401 402 /** 403 * Checks to see if there are any unclosed tags on the stack. The token 404 * represents a html tag that has been closed and has a corresponding open 405 * tag on the stack. Any tags, except single tags, that were opened 406 * (pushed on the stack) after the token are missing a close. 407 * 408 * @param htmlStack the stack of opened HTML tags. 409 * @param token the current HTML tag name that has been closed. 410 */ 411 private void checkUnclosedTags(Deque<HtmlTag> htmlStack, String token) { 412 final Deque<HtmlTag> unclosedTags = new ArrayDeque<>(); 413 HtmlTag lastOpenTag = htmlStack.pop(); 414 while (!token.equalsIgnoreCase(lastOpenTag.getId())) { 415 // Find unclosed elements. Put them on a stack so the 416 // output order won't be back-to-front. 417 if (isSingleTag(lastOpenTag)) { 418 lastOpenTag = htmlStack.pop(); 419 } 420 else { 421 unclosedTags.push(lastOpenTag); 422 lastOpenTag = htmlStack.pop(); 423 } 424 } 425 426 // Output the unterminated tags, if any 427 // Skip multiples, like <b>..<b> 428 String lastFound = ""; 429 for (final HtmlTag htag : unclosedTags) { 430 lastOpenTag = htag; 431 if (lastOpenTag.getId().equals(lastFound)) { 432 continue; 433 } 434 lastFound = lastOpenTag.getId(); 435 log(lastOpenTag.getLineNo(), 436 lastOpenTag.getPosition(), 437 MSG_UNCLOSED_HTML, 438 lastOpenTag); 439 } 440 } 441 442 /** 443 * Determines if the HtmlTag is one which does not require a close tag. 444 * 445 * @param tag the HtmlTag to check. 446 * @return {@code true} if the HtmlTag is a single tag. 447 */ 448 private static boolean isSingleTag(HtmlTag tag) { 449 // If its a singleton tag (<p>, <br>, etc.), ignore it 450 // Can't simply not put them on the stack, since singletons 451 // like <dt> and <dd> (unhappily) may either be terminated 452 // or not terminated. Both options are legal. 453 return SINGLE_TAGS.contains(tag.getId().toLowerCase(Locale.ENGLISH)); 454 } 455 456 /** 457 * Determines if the HtmlTag is one which is allowed in a javadoc. 458 * 459 * @param tag the HtmlTag to check. 460 * @return {@code true} if the HtmlTag is an allowed html tag. 461 */ 462 private static boolean isAllowedTag(HtmlTag tag) { 463 return ALLOWED_TAGS.contains(tag.getId().toLowerCase(Locale.ENGLISH)); 464 } 465 466 /** 467 * Determines if the given token is an extra HTML tag. This indicates that 468 * a close tag was found that does not have a corresponding open tag. 469 * 470 * @param token an HTML tag id for which a close was found. 471 * @param htmlStack a Stack of previous open HTML tags. 472 * @return {@code false} if a previous open tag was found 473 * for the token. 474 */ 475 private static boolean isExtraHtml(String token, Deque<HtmlTag> htmlStack) { 476 boolean isExtra = true; 477 for (final HtmlTag tag : htmlStack) { 478 // Loop, looking for tags that are closed. 479 // The loop is needed in case there are unclosed 480 // tags on the stack. In that case, the stack would 481 // not be empty, but this tag would still be extra. 482 if (token.equalsIgnoreCase(tag.getId())) { 483 isExtra = false; 484 break; 485 } 486 } 487 488 return isExtra; 489 } 490 491 /** 492 * Sets the scope to check. 493 * @param scope a scope. 494 */ 495 public void setScope(Scope scope) { 496 this.scope = scope; 497 } 498 499 /** 500 * Set the excludeScope. 501 * @param excludeScope a scope. 502 */ 503 public void setExcludeScope(Scope excludeScope) { 504 this.excludeScope = excludeScope; 505 } 506 507 /** 508 * Set the format for matching the end of a sentence. 509 * @param pattern a pattern. 510 */ 511 public void setEndOfSentenceFormat(Pattern pattern) { 512 endOfSentenceFormat = pattern; 513 } 514 515 /** 516 * Sets the flag that determines if the first sentence is checked for 517 * proper end of sentence punctuation. 518 * @param flag {@code true} if the first sentence is to be checked 519 */ 520 public void setCheckFirstSentence(boolean flag) { 521 checkFirstSentence = flag; 522 } 523 524 /** 525 * Sets the flag that determines if HTML checking is to be performed. 526 * @param flag {@code true} if HTML checking is to be performed. 527 */ 528 public void setCheckHtml(boolean flag) { 529 checkHtml = flag; 530 } 531 532 /** 533 * Sets the flag that determines if empty Javadoc checking should be done. 534 * @param flag {@code true} if empty Javadoc checking should be done. 535 */ 536 public void setCheckEmptyJavadoc(boolean flag) { 537 checkEmptyJavadoc = flag; 538 } 539}