001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2016 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.List; 024 025import com.google.common.base.Splitter; 026import com.google.common.collect.Lists; 027import com.puppycrawl.tools.checkstyle.api.DetailAST; 028import com.puppycrawl.tools.checkstyle.api.DetailNode; 029import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes; 030import com.puppycrawl.tools.checkstyle.api.TokenTypes; 031import com.puppycrawl.tools.checkstyle.utils.JavadocUtils; 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 * @author baratali 058 * @author maxvetrenko 059 * @author vladlis 060 * 061 */ 062public class SingleLineJavadocCheck extends AbstractJavadocCheck { 063 064 /** 065 * A key is pointing to the warning message text in "messages.properties" 066 * file. 067 */ 068 public static final String MSG_KEY = "singleline.javadoc"; 069 070 /** 071 * Allows to specify a list of tags to be ignored by check. 072 */ 073 private List<String> ignoredTags = new ArrayList<>(); 074 075 /** Whether inline tags must be ignored. **/ 076 private boolean ignoreInlineTags = true; 077 078 /** 079 * Sets a list of tags to be ignored by check. 080 * 081 * @param tags to be ignored by check. 082 */ 083 public void setIgnoredTags(String tags) { 084 ignoredTags = 085 Lists.newArrayList(Splitter.on(",").omitEmptyStrings().trimResults().split(tags)); 086 } 087 088 /** 089 * Sets whether inline tags must be ignored. 090 * 091 * @param ignoreInlineTags whether inline tags must be ignored. 092 */ 093 public void setIgnoreInlineTags(boolean ignoreInlineTags) { 094 this.ignoreInlineTags = ignoreInlineTags; 095 } 096 097 @Override 098 public int[] getDefaultJavadocTokens() { 099 return new int[] { 100 JavadocTokenTypes.JAVADOC, 101 }; 102 } 103 104 @Override 105 public int[] getAcceptableTokens() { 106 return new int[] {TokenTypes.BLOCK_COMMENT_BEGIN }; 107 } 108 109 @Override 110 public int[] getRequiredTokens() { 111 return getAcceptableTokens(); 112 } 113 114 @Override 115 public void visitJavadocToken(DetailNode ast) { 116 if (isSingleLineJavadoc(getBlockCommentAst()) 117 && (hasJavadocTags(ast) || !ignoreInlineTags && hasJavadocInlineTags(ast))) { 118 log(ast.getLineNumber(), MSG_KEY); 119 } 120 } 121 122 /** 123 * Checks if comment is single line comment. 124 * 125 * @param blockCommentStart the AST tree in which a block comment starts 126 * @return true, if comment is single line comment. 127 */ 128 private static boolean isSingleLineJavadoc(DetailAST blockCommentStart) { 129 final DetailAST blockCommentEnd = blockCommentStart.getLastChild(); 130 return blockCommentStart.getLineNo() == blockCommentEnd.getLineNo(); 131 } 132 133 /** 134 * Checks if comment has javadoc tags which are not ignored. Also works 135 * on custom tags. As block tags can be interpreted only at the beginning of a line, 136 * only the first instance is checked. 137 * 138 * @param javadocRoot javadoc root node. 139 * @return true, if comment has javadoc tags which are not ignored. 140 * @see <a href= 141 * http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#blockandinlinetags> 142 * Block and inline tags</a> 143 */ 144 private boolean hasJavadocTags(DetailNode javadocRoot) { 145 final DetailNode javadocTagSection = 146 JavadocUtils.findFirstToken(javadocRoot, JavadocTokenTypes.JAVADOC_TAG); 147 return javadocTagSection != null && !isTagIgnored(javadocTagSection); 148 } 149 150 /** 151 * Checks if comment has in-line tags which are not ignored. 152 * 153 * @param javadocRoot javadoc root node. 154 * @return true, if comment has in-line tags which are not ignored. 155 * @see <a href= 156 * http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#javadoctags> 157 * JavadocTags</a> 158 */ 159 private boolean hasJavadocInlineTags(DetailNode javadocRoot) { 160 DetailNode javadocTagSection = 161 JavadocUtils.findFirstToken(javadocRoot, JavadocTokenTypes.JAVADOC_INLINE_TAG); 162 boolean foundTag = false; 163 while (javadocTagSection != null) { 164 if (!isTagIgnored(javadocTagSection)) { 165 foundTag = true; 166 break; 167 } 168 javadocTagSection = JavadocUtils.getNextSibling( 169 javadocTagSection, JavadocTokenTypes.JAVADOC_INLINE_TAG); 170 } 171 return foundTag; 172 } 173 174 /** 175 * Checks if list of ignored tags contains javadocTagSection's javadoc tag. 176 * 177 * @param javadocTagSection to check javadoc tag in. 178 * @return true, if ignoredTags contains javadocTagSection's javadoc tag. 179 */ 180 private boolean isTagIgnored(DetailNode javadocTagSection) { 181 return ignoredTags.contains(JavadocUtils.getTagName(javadocTagSection)); 182 } 183}