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