001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 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.JavadocCommentsTokenTypes; 025 026/** 027 * <div> 028 * Checks that there is at least one whitespace after the leading asterisk. 029 * Although spaces after asterisks are optional in the Javadoc comments, their absence 030 * makes the documentation difficult to read. It is the de facto standard to put at least 031 * one whitespace after the leading asterisk. 032 * </div> 033 * 034 * @since 8.32 035 */ 036@StatelessCheck 037public class JavadocMissingWhitespaceAfterAsteriskCheck extends AbstractJavadocCheck { 038 039 /** 040 * A key is pointing to the warning message text in "messages.properties" file. 041 */ 042 public static final String MSG_KEY = "javadoc.missing.whitespace"; 043 044 /** 045 * Creates a new {@code JavadocMissingWhitespaceAfterAsteriskCheck} instance. 046 */ 047 public JavadocMissingWhitespaceAfterAsteriskCheck() { 048 // no code by default 049 } 050 051 @Override 052 public int[] getDefaultJavadocTokens() { 053 return new int[] { 054 JavadocCommentsTokenTypes.JAVADOC_CONTENT, 055 JavadocCommentsTokenTypes.LEADING_ASTERISK, 056 }; 057 } 058 059 @Override 060 public int[] getRequiredJavadocTokens() { 061 return getAcceptableJavadocTokens(); 062 } 063 064 @Override 065 public void visitJavadocToken(DetailNode detailNode) { 066 final DetailNode nextNode = resolveNextNode(detailNode); 067 068 if (nextNode != null) { 069 final String text = nextNode.getText(); 070 final int lastAsteriskPosition = getLastLeadingAsteriskPosition(text); 071 072 if (!isLast(lastAsteriskPosition, text) 073 && !Character.isWhitespace(text.charAt(lastAsteriskPosition + 1))) { 074 log(nextNode, MSG_KEY); 075 } 076 } 077 } 078 079 /** 080 * Resolves the first child node related to the given Javadoc {@link DetailNode}. 081 * 082 * <p> 083 * The resolution works in two steps: 084 * <ul> 085 * <li>If the current node is of type {@code JAVADOC_CONTENT}, use its first child; 086 * otherwise use its next sibling.</li> 087 * <li>If that base node has a first child, return it regardless of its type.</li> 088 * </ul> 089 * 090 * <p> 091 * The returned node may or may not be of type {@code TEXT}. If it is not, 092 * the violation logic will treat it as a violation later. 093 * 094 * @param detailNode the Javadoc node to resolve from 095 * @return the first child node if available; otherwise {@code null} 096 */ 097 private static DetailNode resolveNextNode(DetailNode detailNode) { 098 final DetailNode baseNode; 099 if (detailNode.getType() == JavadocCommentsTokenTypes.JAVADOC_CONTENT) { 100 baseNode = detailNode.getFirstChild(); 101 } 102 else { 103 baseNode = detailNode.getNextSibling(); 104 } 105 106 DetailNode nextNode = baseNode; 107 if (baseNode != null && baseNode.getFirstChild() != null) { 108 nextNode = baseNode.getFirstChild(); 109 } 110 111 return nextNode; 112 } 113 114 /** 115 * Checks if the character position is the last one of the string. 116 * 117 * @param position the position of the character 118 * @param text String literal. 119 * @return true if the character position is the last one of the string. 120 * 121 */ 122 private static boolean isLast(int position, String text) { 123 return position == text.length() - 1; 124 } 125 126 /** 127 * Finds the position of the last leading asterisk in the string. 128 * If {@code text} contains no leading asterisk, -1 will be returned. 129 * 130 * @param text String literal. 131 * @return the index of the last leading asterisk. 132 * 133 */ 134 private static int getLastLeadingAsteriskPosition(String text) { 135 int index = -1; 136 137 for (int i = 0; i < text.length(); i++) { 138 if (text.charAt(i) != '*') { 139 break; 140 } 141 index++; 142 } 143 144 return index; 145 } 146 147}