001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2020 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 com.puppycrawl.tools.checkstyle.StatelessCheck; 023import com.puppycrawl.tools.checkstyle.api.DetailNode; 024import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes; 025import com.puppycrawl.tools.checkstyle.utils.JavadocUtil; 026 027/** 028 * <p> 029 * Checks that there is at least one whitespace after the leading asterisk. 030 * Although spaces after asterisks are optional in the Javadoc comments, their absence 031 * makes the documentation difficult to read. It is the de facto standard to put at least 032 * one whitespace after the leading asterisk. 033 * </p> 034 * <ul> 035 * <li> 036 * Property {@code violateExecutionOnNonTightHtml} - Control when to print violations 037 * if the Javadoc being examined by this check violates the tight html rules defined at 038 * <a href="https://checkstyle.org/writingjavadocchecks.html#Tight-HTML_rules">Tight-HTML Rules</a>. 039 * Default value is {@code false}. 040 * </li> 041 * </ul> 042 * <p> 043 * To configure the default check: 044 * </p> 045 * <pre> 046 * <module name="JavadocMissingWhitespaceAfterAsterisk"/> 047 * </pre> 048 * <p> 049 * Code Example: 050 * </p> 051 * <pre> 052 * /** This is valid single-line Javadoc. */ 053 * class TestClass { 054 * /** 055 * *This is invalid Javadoc. 056 * */ 057 * int invalidJavaDoc; 058 * /** 059 * * This is valid Javadoc. 060 * */ 061 * void validJavaDocMethod() { 062 * } 063 * /**This is invalid single-line Javadoc. */ 064 * void invalidSingleLineJavaDocMethod() { 065 * } 066 * /** This is valid single-line Javadoc. */ 067 * void validSingleLineJavaDocMethod() { 068 * } 069 * } 070 * </pre> 071 * 072 * @since 8.32 073 */ 074@StatelessCheck 075public class JavadocMissingWhitespaceAfterAsteriskCheck extends AbstractJavadocCheck { 076 077 /** 078 * A key is pointing to the warning message text in "messages.properties" file. 079 */ 080 public static final String MSG_KEY = "javadoc.missing.whitespace"; 081 082 @Override 083 public int[] getDefaultJavadocTokens() { 084 return new int[] { 085 JavadocTokenTypes.JAVADOC, 086 JavadocTokenTypes.LEADING_ASTERISK, 087 }; 088 } 089 090 @Override 091 public int[] getRequiredJavadocTokens() { 092 return getAcceptableJavadocTokens(); 093 } 094 095 @Override 096 public void visitJavadocToken(DetailNode detailNode) { 097 final DetailNode textNode; 098 099 if (detailNode.getType() == JavadocTokenTypes.JAVADOC) { 100 textNode = JavadocUtil.getFirstChild(detailNode); 101 } 102 else { 103 textNode = JavadocUtil.getNextSibling(detailNode); 104 } 105 106 if (textNode != null && textNode.getType() != JavadocTokenTypes.EOF) { 107 final String text = textNode.getText(); 108 final int lastAsteriskPosition = getLastLeadingAsteriskPosition(text); 109 110 if (!isLast(lastAsteriskPosition, text) 111 && !Character.isWhitespace(text.charAt(lastAsteriskPosition + 1))) { 112 log(textNode.getLineNumber(), textNode.getColumnNumber(), MSG_KEY); 113 } 114 } 115 } 116 117 /** 118 * Checks if the character position is the last one of the string. 119 * 120 * @param position the position of the character 121 * @param text String literal. 122 * @return true if the character position is the last one of the string. 123 * 124 */ 125 private static boolean isLast(int position, String text) { 126 return position == text.length() - 1; 127 } 128 129 /** 130 * Finds the position of the last leading asterisk in the string. 131 * If {@code text} contains no leading asterisk, -1 will be returned. 132 * 133 * @param text String literal. 134 * @return the index of the last leading asterisk. 135 * 136 */ 137 private static int getLastLeadingAsteriskPosition(String text) { 138 int index = -1; 139 140 for (int i = 0; i < text.length(); i++) { 141 if (text.charAt(i) != '*') { 142 break; 143 } 144 index++; 145 } 146 147 return index; 148 } 149 150}