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