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 * <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 * <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 *
044 * <p>
045 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
046 * </p>
047 *
048 * <p>
049 * Violation Message Keys:
050 * </p>
051 * <ul>
052 * <li>
053 * {@code javadoc.missed.html.close}
054 * </li>
055 * <li>
056 * {@code javadoc.missing.whitespace}
057 * </li>
058 * <li>
059 * {@code javadoc.parse.rule.error}
060 * </li>
061 * <li>
062 * {@code javadoc.unclosedHtml}
063 * </li>
064 * <li>
065 * {@code javadoc.wrong.singleton.html.tag}
066 * </li>
067 * </ul>
068 *
069 * @since 8.32
070 */
071@StatelessCheck
072public class JavadocMissingWhitespaceAfterAsteriskCheck extends AbstractJavadocCheck {
073
074    /**
075     * A key is pointing to the warning message text in "messages.properties" file.
076     */
077    public static final String MSG_KEY = "javadoc.missing.whitespace";
078
079    @Override
080    public int[] getDefaultJavadocTokens() {
081        return new int[] {
082            JavadocTokenTypes.JAVADOC,
083            JavadocTokenTypes.LEADING_ASTERISK,
084        };
085    }
086
087    @Override
088    public int[] getRequiredJavadocTokens() {
089        return getAcceptableJavadocTokens();
090    }
091
092    @Override
093    public void visitJavadocToken(DetailNode detailNode) {
094        final DetailNode textNode;
095
096        if (detailNode.getType() == JavadocTokenTypes.JAVADOC) {
097            textNode = JavadocUtil.getFirstChild(detailNode);
098        }
099        else {
100            textNode = JavadocUtil.getNextSibling(detailNode);
101        }
102
103        if (textNode != null && textNode.getType() != JavadocTokenTypes.EOF) {
104            final String text = textNode.getText();
105            final int lastAsteriskPosition = getLastLeadingAsteriskPosition(text);
106
107            if (!isLast(lastAsteriskPosition, text)
108                    && !Character.isWhitespace(text.charAt(lastAsteriskPosition + 1))) {
109                log(textNode.getLineNumber(), textNode.getColumnNumber(), MSG_KEY);
110            }
111        }
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}