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.JavadocTokenTypes;
025import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
026
027/**
028 * <div>
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 * </div>
034 *
035 * @since 8.32
036 */
037@StatelessCheck
038public class JavadocMissingWhitespaceAfterAsteriskCheck extends AbstractJavadocCheck {
039
040    /**
041     * A key is pointing to the warning message text in "messages.properties" file.
042     */
043    public static final String MSG_KEY = "javadoc.missing.whitespace";
044
045    @Override
046    public int[] getDefaultJavadocTokens() {
047        return new int[] {
048            JavadocTokenTypes.JAVADOC,
049            JavadocTokenTypes.LEADING_ASTERISK,
050        };
051    }
052
053    @Override
054    public int[] getRequiredJavadocTokens() {
055        return getAcceptableJavadocTokens();
056    }
057
058    @Override
059    public void visitJavadocToken(DetailNode detailNode) {
060        final DetailNode textNode;
061
062        if (detailNode.getType() == JavadocTokenTypes.JAVADOC) {
063            textNode = JavadocUtil.getFirstChild(detailNode);
064        }
065        else {
066            textNode = JavadocUtil.getNextSibling(detailNode);
067        }
068
069        if (textNode != null && textNode.getType() != JavadocTokenTypes.EOF) {
070            final String text = textNode.getText();
071            final int lastAsteriskPosition = getLastLeadingAsteriskPosition(text);
072
073            if (!isLast(lastAsteriskPosition, text)
074                    && !Character.isWhitespace(text.charAt(lastAsteriskPosition + 1))) {
075                log(textNode.getLineNumber(), textNode.getColumnNumber(), MSG_KEY);
076            }
077        }
078    }
079
080    /**
081     * Checks if the character position is the last one of the string.
082     *
083     * @param position the position of the character
084     * @param text String literal.
085     * @return true if the character position is the last one of the string.
086     *
087     */
088    private static boolean isLast(int position, String text) {
089        return position == text.length() - 1;
090    }
091
092    /**
093     * Finds the position of the last leading asterisk in the string.
094     * If {@code text} contains no leading asterisk, -1 will be returned.
095     *
096     * @param text String literal.
097     * @return the index of the last leading asterisk.
098     *
099     */
100    private static int getLastLeadingAsteriskPosition(String text) {
101        int index = -1;
102
103        for (int i = 0; i < text.length(); i++) {
104            if (text.charAt(i) != '*') {
105                break;
106            }
107            index++;
108        }
109
110        return index;
111    }
112
113}