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;
021
022import java.util.ArrayList;
023import java.util.List;
024
025import com.puppycrawl.tools.checkstyle.StatelessCheck;
026import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
027import com.puppycrawl.tools.checkstyle.api.DetailAST;
028import com.puppycrawl.tools.checkstyle.api.TokenTypes;
029import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
030
031/**
032 * <div>
033 * Checks if the
034 * <a href="https://www.oracle.com/java/technologies/javase/codeconventions-comments.html">
035 * multi-line comments</a> have leading asterisks on each line.
036 * </div>
037 *
038 * <p>
039 * Every line in multi-line comment should have leading asterisk including blank line.
040 * </p>
041 *
042 * @since 13.9.0
043 */
044@StatelessCheck
045public class MultilineCommentLeadingAsteriskPresenceCheck extends AbstractCheck {
046
047    /**
048     * A key is pointing to the warning message text in "messages.properties"
049     * file.
050     */
051    public static final String MSG_MISSING_ASTERISK = "multiline.comment.missing.asterisk";
052
053    /**
054     * Creates a new {@code MultilineCommentLeadingAsteriskPresenceCheck} instance.
055     */
056    public MultilineCommentLeadingAsteriskPresenceCheck() {
057        // no code by default
058    }
059
060    @Override
061    public int[] getRequiredTokens() {
062        return new int[] {
063            TokenTypes.BLOCK_COMMENT_BEGIN,
064        };
065    }
066
067    @Override
068    public int[] getAcceptableTokens() {
069        return getRequiredTokens();
070    }
071
072    @Override
073    public int[] getDefaultTokens() {
074        return getRequiredTokens();
075    }
076
077    @Override
078    public boolean isCommentNodesRequired() {
079        return true;
080    }
081
082    @Override
083    public void visitToken(DetailAST ast) {
084        if (!JavadocUtil.isJavadocComment(ast)) {
085            final String commentText = JavadocUtil.getBlockCommentContent(ast);
086            int lineNumber = ast.getLineNo();
087            final int commentEndLineNumber = ast.getLastChild().getLineNo();
088            final List<String> lines = commentText.lines().toList();
089            final List<Integer> nonAsteriskLineNumbers = new ArrayList<>();
090            for (int cur = 1; cur < lines.size(); cur++) {
091                lineNumber++;
092                final String line = lines.get(cur).trim();
093                // is block comment end
094                if (lineNumber == commentEndLineNumber && line.isEmpty()) {
095                    continue;
096                }
097                if (!line.startsWith("*")) {
098                    nonAsteriskLineNumbers.add(lineNumber);
099                }
100            }
101
102            if (!nonAsteriskLineNumbers.isEmpty()) {
103                final String allLineNumbers = nonAsteriskLineNumbers.toString();
104                log(ast, MSG_MISSING_ASTERISK, allLineNumbers.substring(1,
105                        allLineNumbers.length() - 1));
106            }
107        }
108    }
109
110}