001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2015 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.puppycrawl.tools.checkstyle.api.DetailAST; 026import com.puppycrawl.tools.checkstyle.api.DetailNode; 027import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes; 028import com.puppycrawl.tools.checkstyle.api.TokenTypes; 029import com.puppycrawl.tools.checkstyle.utils.JavadocUtils; 030 031/** 032 * Checks that a JavaDoc block can fit on a single line and doesn't 033 * contain at-clauses. Javadoc comment that contains at least one at-clause 034 * should be formatted in a few lines.<br> 035 * All inline at-clauses are ignored by default. 036 * 037 * <p>The Check has the following properties: 038 * <br><b>ignoredTags</b> - allows to specify a list of at-clauses 039 * (including custom at-clauses) to be ignored by the check. 040 * <br><b>ignoreInlineTags</b> - whether inline at-clauses must be ignored. 041 * </p> 042 * 043 * <p>Default configuration: 044 * <pre> 045 * <module name="SingleLineJavadoc"/> 046 * </pre> 047 * To specify a list of ignored at-clauses and make inline at-clauses not ignored in general: 048 * <pre> 049 * <module name="SingleLineJavadoc"> 050 * <property name="ignoredTags" value="@inheritDoc, @link"/> 051 * <property name="ignoreInlineTags" value="false"/> 052 * </module> 053 * </pre> 054 * 055 * @author baratali 056 * @author maxvetrenko 057 * @author vladlis 058 * 059 */ 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 final List<String> tagList = new ArrayList<>(); 083 final String[] sTags = tags.split(","); 084 for (String sTag : sTags) { 085 tagList.add(sTag.trim()); 086 } 087 ignoredTags = tagList; 088 } 089 090 /** 091 * Sets whether inline tags must be ignored. 092 * 093 * @param ignoreInlineTags whether inline tags must be ignored. 094 */ 095 public void setIgnoreInlineTags(boolean ignoreInlineTags) { 096 this.ignoreInlineTags = ignoreInlineTags; 097 } 098 099 @Override 100 public int[] getDefaultJavadocTokens() { 101 return new int[] { 102 JavadocTokenTypes.JAVADOC, 103 }; 104 } 105 106 @Override 107 public int[] getAcceptableTokens() { 108 return new int[] {TokenTypes.BLOCK_COMMENT_BEGIN }; 109 } 110 111 @Override 112 public int[] getRequiredTokens() { 113 return getAcceptableTokens(); 114 } 115 116 @Override 117 public void visitJavadocToken(DetailNode ast) { 118 if (isSingleLineJavadoc(getBlockCommentAst()) 119 && (hasJavadocTags(ast) || !ignoreInlineTags && hasJavadocInlineTags(ast))) { 120 log(ast.getLineNumber(), MSG_KEY); 121 } 122 } 123 124 /** 125 * Checks if comment is single line comment. 126 * 127 * @param blockCommentStart the AST tree in which a block comment starts 128 * @return true, if comment is single line comment. 129 */ 130 private static boolean isSingleLineJavadoc(DetailAST blockCommentStart) { 131 final DetailAST blockCommentEnd = blockCommentStart.getLastChild(); 132 return blockCommentStart.getLineNo() == blockCommentEnd.getLineNo(); 133 } 134 135 /** 136 * Checks if comment has javadoc tags which are not ignored. Also works 137 * on custom tags. As block tags can be interpreted only at the beginning of a line, 138 * only the first instance is checked. 139 * 140 * @param javadocRoot javadoc root node. 141 * @return true, if comment has javadoc tags which are not ignored. 142 * @see <a href= 143 * http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#blockandinlinetags> 144 * Block and inline tags</a> 145 */ 146 private boolean hasJavadocTags(DetailNode javadocRoot) { 147 final DetailNode javadocTagSection = 148 JavadocUtils.findFirstToken(javadocRoot, JavadocTokenTypes.JAVADOC_TAG); 149 return javadocTagSection != null && !isTagIgnored(javadocTagSection); 150 } 151 152 /** 153 * Checks if comment has in-line tags which are not ignored. 154 * 155 * @param javadocRoot javadoc root node. 156 * @return true, if comment has in-line tags which are not ignored. 157 * @see <a href= 158 * http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#javadoctags> 159 * JavadocTags</a> 160 */ 161 private boolean hasJavadocInlineTags(DetailNode javadocRoot) { 162 DetailNode javadocTagSection = 163 JavadocUtils.findFirstToken(javadocRoot, JavadocTokenTypes.JAVADOC_INLINE_TAG); 164 boolean foundTag = false; 165 while (javadocTagSection != null) { 166 if (!isTagIgnored(javadocTagSection)) { 167 foundTag = true; 168 break; 169 } 170 javadocTagSection = JavadocUtils.getNextSibling( 171 javadocTagSection, JavadocTokenTypes.JAVADOC_INLINE_TAG); 172 } 173 return foundTag; 174 } 175 176 /** 177 * Checks if list of ignored tags contains javadocTagSection's javadoc tag. 178 * 179 * @param javadocTagSection to check javadoc tag in. 180 * @return true, if ignoredTags contains javadocTagSection's javadoc tag. 181 */ 182 private boolean isTagIgnored(DetailNode javadocTagSection) { 183 return ignoredTags.contains(JavadocUtils.getTagName(javadocTagSection)); 184 } 185}