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.Arrays;
023import java.util.regex.Pattern;
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.CommonUtil;
030import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
031
032/**
033 * <div>
034 * The check to ensure that lines with code do not end with comment.
035 * For the case of {@code //} comments that means that the only thing that should precede
036 * it is whitespace. It doesn't check comments if they do not end a line; for example,
037 * it accepts the following: <code>Thread.sleep( 10 /*some comment here&#42;/ );</code>
038 * Format property is intended to deal with the <code>} // while</code> example.
039 * </div>
040 *
041 * <p>
042 * Rationale: Steve McConnell in <cite>Code Complete</cite> suggests that endline
043 * comments are a bad practice. An end line comment would be one that is on
044 * the same line as actual code. For example:
045 * </p>
046 * <div class="wrapper"><pre class="prettyprint"><code class="language-java">
047 * a = b + c;      // Some insightful comment
048 * d = e / f;      /* Some insightful block comment &#42;/
049 * </code></pre></div>
050 *
051 * <p>
052 * Quoting <cite>Code Complete</cite> for the justification:
053 * </p>
054 * <ul>
055 * <li>
056 * "The comments have to be aligned so that they do not interfere with the visual
057 * structure of the code. If you don't align them neatly, they'll make your listing
058 * look like it's been through a washing machine."
059 * </li>
060 * <li>
061 * "Endline comments tend to be hard to format...It takes time to align them.
062 * Such time is not spent learning more about the code; it's dedicated solely
063 * to the tedious task of pressing the spacebar or tab key."
064 * </li>
065 * <li>
066 * "Endline comments are also hard to maintain. If the code on any line containing
067 * an endline comment grows, it bumps the comment farther out, and all the other
068 * endline comments will have to bumped out to match. Styles that are hard to
069 * maintain aren't maintained...."
070 * </li>
071 * <li>
072 * "Endline comments also tend to be cryptic. The right side of the line doesn't
073 * offer much room and the desire to keep the comment on one line means the comment
074 * must be short. Work then goes into making the line as short as possible instead
075 * of as clear as possible. The comment usually ends up as cryptic as possible...."
076 * </li>
077 * <li>
078 * "A systemic problem with endline comments is that it's hard to write a meaningful
079 * comment for one line of code. Most endline comments just repeat the line of code,
080 * which hurts more than it helps."
081 * </li>
082 * </ul>
083 *
084 * <p>
085 * McConnell's comments on being hard to maintain when the size of the line changes
086 * are even more important in the age of automated refactorings.
087 * </p>
088 *
089 * @noinspection HtmlTagCanBeJavadocTag
090 * @noinspectionreason HtmlTagCanBeJavadocTag - encoded symbols were not decoded
091 *      when replaced with Javadoc tag
092 * @since 3.4
093 */
094@StatelessCheck
095public class TrailingCommentCheck extends AbstractCheck {
096
097    /**
098     * A key is pointing to the warning message text in "messages.properties"
099     * file.
100     */
101    public static final String MSG_KEY = "trailing.comments";
102
103    /** Specify pattern for strings to be formatted without comment specifiers. */
104    private static final Pattern FORMAT_LINE = Pattern.compile("/");
105
106    /**
107     * Define pattern for text allowed in trailing comments.
108     * This pattern will not be applied to multiline comments.
109     */
110    private Pattern legalComment;
111
112    /** Specify pattern for strings allowed before the comment. */
113    private Pattern format = Pattern.compile("^[\\s});]*$");
114
115    /**
116     * Creates a new {@code TrailingCommentCheck} instance.
117     */
118    public TrailingCommentCheck() {
119        // no code by default
120    }
121
122    /**
123     * Setter to define pattern for text allowed in trailing comments.
124     * This pattern will not be applied to multiline comments.
125     *
126     * @param legalComment pattern to set.
127     * @since 4.2
128     */
129    public void setLegalComment(final Pattern legalComment) {
130        this.legalComment = legalComment;
131    }
132
133    /**
134     * Setter to specify pattern for strings allowed before the comment.
135     *
136     * @param pattern a pattern
137     * @since 3.4
138     */
139    public final void setFormat(Pattern pattern) {
140        format = pattern;
141    }
142
143    @Override
144    public boolean isCommentNodesRequired() {
145        return true;
146    }
147
148    @Override
149    public int[] getDefaultTokens() {
150        return getRequiredTokens();
151    }
152
153    @Override
154    public int[] getAcceptableTokens() {
155        return getRequiredTokens();
156    }
157
158    @Override
159    public int[] getRequiredTokens() {
160        return new int[] {
161            TokenTypes.SINGLE_LINE_COMMENT,
162            TokenTypes.BLOCK_COMMENT_BEGIN,
163        };
164    }
165
166    @Override
167    public void visitToken(DetailAST ast) {
168        if (ast.getType() == TokenTypes.SINGLE_LINE_COMMENT) {
169            checkSingleLineComment(ast);
170        }
171        else {
172            checkBlockComment(ast);
173        }
174    }
175
176    /**
177     * Checks if single-line comment is legal.
178     *
179     * @param ast Detail ast element to be checked.
180     */
181    private void checkSingleLineComment(DetailAST ast) {
182        final int lineNo = ast.getLineNo();
183        final String comment = ast.getFirstChild().getText();
184        final int[] lineBeforeCodePoints =
185                Arrays.copyOfRange(getLineCodePoints(lineNo - 1), 0, ast.getColumnNo());
186        final String lineBefore = new String(lineBeforeCodePoints, 0, lineBeforeCodePoints.length);
187
188        if (!format.matcher(lineBefore).find() && !isLegalCommentContent(comment)) {
189            log(ast, MSG_KEY);
190        }
191    }
192
193    /**
194     * Method to check if block comment is in correct format.
195     *
196     * @param ast Detail ast element to be checked.
197     */
198    private void checkBlockComment(DetailAST ast) {
199        final int lineNo = ast.getLineNo();
200        final DetailAST firstChild = ast.getFirstChild();
201        final DetailAST lastChild = ast.getLastChild();
202        final String comment = firstChild.getText();
203        int[] lineCodePoints = getLineCodePoints(lineNo - 1);
204
205        if (lineCodePoints.length > lastChild.getColumnNo() + 1) {
206            lineCodePoints = Arrays.copyOfRange(lineCodePoints,
207                    lastChild.getColumnNo() + 2, lineCodePoints.length);
208        }
209
210        String line = new String(lineCodePoints, 0, lineCodePoints.length);
211        line = FORMAT_LINE.matcher(line).replaceAll("");
212
213        final int[] lineBeforeCodePoints =
214                Arrays.copyOfRange(getLineCodePoints(lineNo - 1), 0, ast.getColumnNo());
215        final String lineBefore = new String(lineBeforeCodePoints, 0, lineBeforeCodePoints.length);
216        final boolean isCommentAtEndOfLine = ast.getLineNo() != lastChild.getLineNo()
217                || CommonUtil.isBlank(line);
218        final boolean isLegalBlockComment = isLegalCommentContent(comment)
219                && TokenUtil.areOnSameLine(firstChild, lastChild)
220                || format.matcher(lineBefore).find();
221
222        if (isCommentAtEndOfLine && !isLegalBlockComment) {
223            log(ast, MSG_KEY);
224        }
225    }
226
227    /**
228     * Checks if given comment content is legal.
229     *
230     * @param commentContent comment content to check.
231     * @return true if the content is legal.
232     */
233    private boolean isLegalCommentContent(String commentContent) {
234        return legalComment != null && legalComment.matcher(commentContent).find();
235    }
236
237}