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" file.
049     */
050    public static final String MSG_MISSING_ASTERISK = "multiline.comment.missing.asterisk";
051
052    /**
053     * Creates a new {@code MultilineCommentLeadingAsteriskPresenceCheck} instance.
054     */
055    public MultilineCommentLeadingAsteriskPresenceCheck() {
056        // no code by default
057    }
058
059    @Override
060    public int[] getRequiredTokens() {
061        return new int[] {
062            TokenTypes.BLOCK_COMMENT_BEGIN,
063        };
064    }
065
066    @Override
067    public int[] getAcceptableTokens() {
068        return getRequiredTokens();
069    }
070
071    @Override
072    public int[] getDefaultTokens() {
073        return getRequiredTokens();
074    }
075
076    @Override
077    public boolean isCommentNodesRequired() {
078        return true;
079    }
080
081    @Override
082    public void visitToken(DetailAST ast) {
083        if (!JavadocUtil.isJavadocComment(ast)) {
084            final String commentText = JavadocUtil.getBlockCommentContent(ast);
085            int lineNumber = ast.getLineNo();
086            final int commentEndLineNumber = ast.getLastChild().getLineNo();
087            final List<String> lines = commentText.lines().toList();
088            final List<Integer> nonAsteriskLineNumbers = new ArrayList<>();
089            for (int cur = 1; cur < lines.size(); cur++) {
090                lineNumber++;
091                final String line = lines.get(cur).trim();
092                // is block comment end
093                if (lineNumber == commentEndLineNumber && line.isEmpty()) {
094                    continue;
095                }
096                if (!line.startsWith("*")) {
097                    nonAsteriskLineNumbers.add(lineNumber);
098                }
099            }
100
101            if (!nonAsteriskLineNumbers.isEmpty()) {
102                final String allLineNumbers = nonAsteriskLineNumbers.toString();
103                log(ast, MSG_MISSING_ASTERISK, allLineNumbers.substring(1,
104                        allLineNumbers.length() - 1));
105            }
106        }
107    }
108
109}