001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2025 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 @Override 045 public int[] getDefaultJavadocTokens() { 046 return new int[] { 047 JavadocCommentsTokenTypes.JAVADOC_CONTENT, 048 JavadocCommentsTokenTypes.LEADING_ASTERISK, 049 }; 050 } 051 052 @Override 053 public int[] getRequiredJavadocTokens() { 054 return getAcceptableJavadocTokens(); 055 } 056 057 @Override 058 public void visitJavadocToken(DetailNode detailNode) { 059 final DetailNode nextNode = resolveNextNode(detailNode); 060 061 if (nextNode != null) { 062 final String text = nextNode.getText(); 063 final int lastAsteriskPosition = getLastLeadingAsteriskPosition(text); 064 065 if (!isLast(lastAsteriskPosition, text) 066 && !Character.isWhitespace(text.charAt(lastAsteriskPosition + 1))) { 067 log(nextNode.getLineNumber(), nextNode.getColumnNumber(), MSG_KEY); 068 } 069 } 070 } 071 072 /** 073 * Resolves the first child node related to the given Javadoc {@link DetailNode}. 074 * 075 * <p> 076 * The resolution works in two steps: 077 * <ul> 078 * <li>If the current node is of type {@code JAVADOC_CONTENT}, use its first child; 079 * otherwise use its next sibling.</li> 080 * <li>If that base node has a first child, return it regardless of its type.</li> 081 * </ul> 082 * 083 * <p> 084 * The returned node may or may not be of type {@code TEXT}. If it is not, 085 * the violation logic will treat it as a violation later. 086 * 087 * @param detailNode the Javadoc node to resolve from 088 * @return the first child node if available; otherwise {@code null} 089 */ 090 private static DetailNode resolveNextNode(DetailNode detailNode) { 091 final DetailNode baseNode; 092 if (detailNode.getType() == JavadocCommentsTokenTypes.JAVADOC_CONTENT) { 093 baseNode = detailNode.getFirstChild(); 094 } 095 else { 096 baseNode = detailNode.getNextSibling(); 097 } 098 099 DetailNode nextNode = baseNode; 100 if (baseNode != null && baseNode.getFirstChild() != null) { 101 nextNode = baseNode.getFirstChild(); 102 } 103 104 return nextNode; 105 } 106 107 /** 108 * Checks if the character position is the last one of the string. 109 * 110 * @param position the position of the character 111 * @param text String literal. 112 * @return true if the character position is the last one of the string. 113 * 114 */ 115 private static boolean isLast(int position, String text) { 116 return position == text.length() - 1; 117 } 118 119 /** 120 * Finds the position of the last leading asterisk in the string. 121 * If {@code text} contains no leading asterisk, -1 will be returned. 122 * 123 * @param text String literal. 124 * @return the index of the last leading asterisk. 125 * 126 */ 127 private static int getLastLeadingAsteriskPosition(String text) { 128 int index = -1; 129 130 for (int i = 0; i < text.length(); i++) { 131 if (text.charAt(i) != '*') { 132 break; 133 } 134 index++; 135 } 136 137 return index; 138 } 139 140}