001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2024 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 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 045 * </p> 046 * <p> 047 * Violation Message Keys: 048 * </p> 049 * <ul> 050 * <li> 051 * {@code javadoc.missed.html.close} 052 * </li> 053 * <li> 054 * {@code javadoc.missing.whitespace} 055 * </li> 056 * <li> 057 * {@code javadoc.parse.rule.error} 058 * </li> 059 * <li> 060 * {@code javadoc.unclosedHtml} 061 * </li> 062 * <li> 063 * {@code javadoc.wrong.singleton.html.tag} 064 * </li> 065 * </ul> 066 * 067 * @since 8.32 068 */ 069@StatelessCheck 070public class JavadocMissingWhitespaceAfterAsteriskCheck extends AbstractJavadocCheck { 071 072 /** 073 * A key is pointing to the warning message text in "messages.properties" file. 074 */ 075 public static final String MSG_KEY = "javadoc.missing.whitespace"; 076 077 @Override 078 public int[] getDefaultJavadocTokens() { 079 return new int[] { 080 JavadocTokenTypes.JAVADOC, 081 JavadocTokenTypes.LEADING_ASTERISK, 082 }; 083 } 084 085 @Override 086 public int[] getRequiredJavadocTokens() { 087 return getAcceptableJavadocTokens(); 088 } 089 090 @Override 091 public void visitJavadocToken(DetailNode detailNode) { 092 final DetailNode textNode; 093 094 if (detailNode.getType() == JavadocTokenTypes.JAVADOC) { 095 textNode = JavadocUtil.getFirstChild(detailNode); 096 } 097 else { 098 textNode = JavadocUtil.getNextSibling(detailNode); 099 } 100 101 if (textNode != null && textNode.getType() != JavadocTokenTypes.EOF) { 102 final String text = textNode.getText(); 103 final int lastAsteriskPosition = getLastLeadingAsteriskPosition(text); 104 105 if (!isLast(lastAsteriskPosition, text) 106 && !Character.isWhitespace(text.charAt(lastAsteriskPosition + 1))) { 107 log(textNode.getLineNumber(), textNode.getColumnNumber(), MSG_KEY); 108 } 109 } 110 } 111 112 /** 113 * Checks if the character position is the last one of the string. 114 * 115 * @param position the position of the character 116 * @param text String literal. 117 * @return true if the character position is the last one of the string. 118 * 119 */ 120 private static boolean isLast(int position, String text) { 121 return position == text.length() - 1; 122 } 123 124 /** 125 * Finds the position of the last leading asterisk in the string. 126 * If {@code text} contains no leading asterisk, -1 will be returned. 127 * 128 * @param text String literal. 129 * @return the index of the last leading asterisk. 130 * 131 */ 132 private static int getLastLeadingAsteriskPosition(String text) { 133 int index = -1; 134 135 for (int i = 0; i < text.length(); i++) { 136 if (text.charAt(i) != '*') { 137 break; 138 } 139 index++; 140 } 141 142 return index; 143 } 144 145}