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 * Type is {@code boolean}. 040 * Default value is {@code false}. 041 * </li> 042 * </ul> 043 * <p> 044 * To configure the default check: 045 * </p> 046 * <pre> 047 * <module name="JavadocMissingWhitespaceAfterAsterisk"/> 048 * </pre> 049 * <p> 050 * Code Example: 051 * </p> 052 * <pre> 053 * /** This is valid single-line Javadoc. */ 054 * class TestClass { 055 * /** 056 * *This is invalid Javadoc. 057 * */ 058 * int invalidJavaDoc; 059 * /** 060 * * This is valid Javadoc. 061 * */ 062 * void validJavaDocMethod() { 063 * } 064 * /**This is invalid single-line Javadoc. */ 065 * void invalidSingleLineJavaDocMethod() { 066 * } 067 * /** This is valid single-line Javadoc. */ 068 * void validSingleLineJavaDocMethod() { 069 * } 070 * } 071 * </pre> 072 * <p> 073 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 074 * </p> 075 * <p> 076 * Violation Message Keys: 077 * </p> 078 * <ul> 079 * <li> 080 * {@code javadoc.missed.html.close} 081 * </li> 082 * <li> 083 * {@code javadoc.missing.whitespace} 084 * </li> 085 * <li> 086 * {@code javadoc.parse.rule.error} 087 * </li> 088 * <li> 089 * {@code javadoc.wrong.singleton.html.tag} 090 * </li> 091 * </ul> 092 * 093 * @since 8.32 094 */ 095@StatelessCheck 096public class JavadocMissingWhitespaceAfterAsteriskCheck extends AbstractJavadocCheck { 097 098 /** 099 * A key is pointing to the warning message text in "messages.properties" file. 100 */ 101 public static final String MSG_KEY = "javadoc.missing.whitespace"; 102 103 @Override 104 public int[] getDefaultJavadocTokens() { 105 return new int[] { 106 JavadocTokenTypes.JAVADOC, 107 JavadocTokenTypes.LEADING_ASTERISK, 108 }; 109 } 110 111 @Override 112 public int[] getRequiredJavadocTokens() { 113 return getAcceptableJavadocTokens(); 114 } 115 116 @Override 117 public void visitJavadocToken(DetailNode detailNode) { 118 final DetailNode textNode; 119 120 if (detailNode.getType() == JavadocTokenTypes.JAVADOC) { 121 textNode = JavadocUtil.getFirstChild(detailNode); 122 } 123 else { 124 textNode = JavadocUtil.getNextSibling(detailNode); 125 } 126 127 if (textNode != null && textNode.getType() != JavadocTokenTypes.EOF) { 128 final String text = textNode.getText(); 129 final int lastAsteriskPosition = getLastLeadingAsteriskPosition(text); 130 131 if (!isLast(lastAsteriskPosition, text) 132 && !Character.isWhitespace(text.charAt(lastAsteriskPosition + 1))) { 133 log(textNode.getLineNumber(), textNode.getColumnNumber(), MSG_KEY); 134 } 135 } 136 } 137 138 /** 139 * Checks if the character position is the last one of the string. 140 * 141 * @param position the position of the character 142 * @param text String literal. 143 * @return true if the character position is the last one of the string. 144 * 145 */ 146 private static boolean isLast(int position, String text) { 147 return position == text.length() - 1; 148 } 149 150 /** 151 * Finds the position of the last leading asterisk in the string. 152 * If {@code text} contains no leading asterisk, -1 will be returned. 153 * 154 * @param text String literal. 155 * @return the index of the last leading asterisk. 156 * 157 */ 158 private static int getLastLeadingAsteriskPosition(String text) { 159 int index = -1; 160 161 for (int i = 0; i < text.length(); i++) { 162 if (text.charAt(i) != '*') { 163 break; 164 } 165 index++; 166 } 167 168 return index; 169 } 170 171}