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