001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2025 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 java.util.Set; 023 024import com.puppycrawl.tools.checkstyle.StatelessCheck; 025import com.puppycrawl.tools.checkstyle.api.DetailAST; 026import com.puppycrawl.tools.checkstyle.api.DetailNode; 027import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes; 028import com.puppycrawl.tools.checkstyle.utils.JavadocUtil; 029import com.puppycrawl.tools.checkstyle.utils.TokenUtil; 030 031/** 032 * <div> 033 * Checks that a Javadoc block can fit in a single-line and doesn't contain block tags. 034 * Javadoc comment that contains at least one block tag should be formatted in a few lines. 035 * </div> 036 * 037 * @since 6.0 038 */ 039@StatelessCheck 040public class SingleLineJavadocCheck extends AbstractJavadocCheck { 041 042 /** 043 * A key is pointing to the warning message text in "messages.properties" 044 * file. 045 */ 046 public static final String MSG_KEY = "singleline.javadoc"; 047 048 /** 049 * Specify 050 * <a href="https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javadoc.html#CHDBEFIF"> 051 * block tags</a> which are ignored by the check. 052 */ 053 private Set<String> ignoredTags = Set.of(); 054 055 /** 056 * Control whether 057 * <a href="https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javadoc.html#CHDBEFIF"> 058 * inline tags</a> must be ignored. 059 */ 060 private boolean ignoreInlineTags = true; 061 062 /** 063 * Setter to specify 064 * <a href="https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javadoc.html#CHDBEFIF"> 065 * block tags</a> which are ignored by the check. 066 * 067 * @param tags to be ignored by check. 068 * @since 6.8 069 */ 070 public void setIgnoredTags(String... tags) { 071 ignoredTags = Set.of(tags); 072 } 073 074 /** 075 * Setter to control whether 076 * <a href="https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javadoc.html#CHDBEFIF"> 077 * inline tags</a> must be ignored. 078 * 079 * @param ignoreInlineTags whether inline tags must be ignored. 080 * @since 6.8 081 */ 082 public void setIgnoreInlineTags(boolean ignoreInlineTags) { 083 this.ignoreInlineTags = ignoreInlineTags; 084 } 085 086 @Override 087 public int[] getDefaultJavadocTokens() { 088 return new int[] { 089 JavadocTokenTypes.JAVADOC, 090 }; 091 } 092 093 @Override 094 public int[] getRequiredJavadocTokens() { 095 return getAcceptableJavadocTokens(); 096 } 097 098 @Override 099 public void visitJavadocToken(DetailNode ast) { 100 if (isSingleLineJavadoc(getBlockCommentAst()) 101 && (hasJavadocTags(ast) || !ignoreInlineTags && hasJavadocInlineTags(ast))) { 102 log(ast.getLineNumber(), MSG_KEY); 103 } 104 } 105 106 /** 107 * Checks if comment is single-line comment. 108 * 109 * @param blockCommentStart the AST tree in which a block comment starts 110 * @return true, if comment is single-line comment. 111 */ 112 private static boolean isSingleLineJavadoc(DetailAST blockCommentStart) { 113 final DetailAST blockCommentEnd = blockCommentStart.getLastChild(); 114 return TokenUtil.areOnSameLine(blockCommentStart, blockCommentEnd); 115 } 116 117 /** 118 * Checks if comment has javadoc tags which are not ignored. Also works 119 * on custom tags. As block tags can be interpreted only at the beginning of a line, 120 * only the first instance is checked. 121 * 122 * @param javadocRoot javadoc root node. 123 * @return true, if comment has javadoc tags which are not ignored. 124 * @see <a href= 125 * "https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#blockandinlinetags"> 126 * Block and inline tags</a> 127 */ 128 private boolean hasJavadocTags(DetailNode javadocRoot) { 129 final DetailNode javadocTagSection = 130 JavadocUtil.findFirstToken(javadocRoot, JavadocTokenTypes.JAVADOC_TAG); 131 return javadocTagSection != null && !isTagIgnored(javadocTagSection); 132 } 133 134 /** 135 * Checks if comment has in-line tags which are not ignored. 136 * 137 * @param javadocRoot javadoc root node. 138 * @return true, if comment has in-line tags which are not ignored. 139 * @see <a href= 140 * "https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#javadoctags"> 141 * JavadocTags</a> 142 */ 143 private boolean hasJavadocInlineTags(DetailNode javadocRoot) { 144 DetailNode javadocTagSection = 145 JavadocUtil.findFirstToken(javadocRoot, JavadocTokenTypes.JAVADOC_INLINE_TAG); 146 boolean foundTag = false; 147 while (javadocTagSection != null) { 148 if (!isTagIgnored(javadocTagSection)) { 149 foundTag = true; 150 break; 151 } 152 javadocTagSection = JavadocUtil.getNextSibling( 153 javadocTagSection, JavadocTokenTypes.JAVADOC_INLINE_TAG); 154 } 155 return foundTag; 156 } 157 158 /** 159 * Checks if list of ignored tags contains javadocTagSection's javadoc tag. 160 * 161 * @param javadocTagSection to check javadoc tag in. 162 * @return true, if ignoredTags contains javadocTagSection's javadoc tag. 163 */ 164 private boolean isTagIgnored(DetailNode javadocTagSection) { 165 return ignoredTags.contains(JavadocUtil.getTagName(javadocTagSection)); 166 } 167 168}