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 com.puppycrawl.tools.checkstyle.StatelessCheck; 023import com.puppycrawl.tools.checkstyle.api.DetailNode; 024import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes; 025import com.puppycrawl.tools.checkstyle.utils.JavadocUtil; 026 027/** 028 * <p> 029 * Checks that the at-clause tag is followed by description. 030 * </p> 031 * <ul> 032 * <li> 033 * Property {@code violateExecutionOnNonTightHtml} - Control when to print violations 034 * if the Javadoc being examined by this check violates the tight html rules defined at 035 * <a href="https://checkstyle.org/writingjavadocchecks.html#Tight-HTML_rules">Tight-HTML Rules</a>. 036 * Type is {@code boolean}. 037 * Default value is {@code false}. 038 * </li> 039 * <li> 040 * Property {@code javadocTokens} - javadoc tokens to check 041 * Type is {@code int[]}. 042 * Default value is 043 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/JavadocTokenTypes.html#PARAM_LITERAL"> 044 * PARAM_LITERAL</a>, 045 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/JavadocTokenTypes.html#RETURN_LITERAL"> 046 * RETURN_LITERAL</a>, 047 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/JavadocTokenTypes.html#THROWS_LITERAL"> 048 * THROWS_LITERAL</a>, 049 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/JavadocTokenTypes.html#EXCEPTION_LITERAL"> 050 * EXCEPTION_LITERAL</a>, 051 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/JavadocTokenTypes.html#DEPRECATED_LITERAL"> 052 * DEPRECATED_LITERAL</a>. 053 * </li> 054 * </ul> 055 * <p> 056 * Default configuration that will check {@code @param}, {@code @return}, 057 * {@code @throws}, {@code @deprecated}: 058 * </p> 059 * <pre> 060 * <module name="NonEmptyAtclauseDescription"/> 061 * </pre> 062 * <p> 063 * To configure the check to validate only {@code @param} and {@code @return} tags: 064 * </p> 065 * <pre> 066 * <module name="NonEmptyAtclauseDescription"> 067 * <property name="javadocTokens" value="PARAM_LITERAL,RETURN_LITERAL"/> 068 * </module> 069 * </pre> 070 * <p> 071 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 072 * </p> 073 * <p> 074 * Violation Message Keys: 075 * </p> 076 * <ul> 077 * <li> 078 * {@code javadoc.missed.html.close} 079 * </li> 080 * <li> 081 * {@code javadoc.parse.rule.error} 082 * </li> 083 * <li> 084 * {@code javadoc.wrong.singleton.html.tag} 085 * </li> 086 * <li> 087 * {@code non.empty.atclause} 088 * </li> 089 * </ul> 090 * 091 * @since 6.0 092 */ 093@StatelessCheck 094public class NonEmptyAtclauseDescriptionCheck extends AbstractJavadocCheck { 095 096 /** 097 * A key is pointing to the warning message text in "messages.properties" 098 * file. 099 */ 100 public static final String MSG_KEY = "non.empty.atclause"; 101 102 @Override 103 public int[] getDefaultJavadocTokens() { 104 return new int[] { 105 JavadocTokenTypes.PARAM_LITERAL, 106 JavadocTokenTypes.RETURN_LITERAL, 107 JavadocTokenTypes.THROWS_LITERAL, 108 JavadocTokenTypes.EXCEPTION_LITERAL, 109 JavadocTokenTypes.DEPRECATED_LITERAL, 110 }; 111 } 112 113 @Override 114 public void visitJavadocToken(DetailNode ast) { 115 if (isEmptyTag(ast.getParent())) { 116 log(ast.getLineNumber(), MSG_KEY, ast.getText()); 117 } 118 } 119 120 /** 121 * Tests if at-clause tag is empty. 122 * 123 * @param tagNode at-clause tag. 124 * @return true, if at-clause tag is empty. 125 */ 126 private static boolean isEmptyTag(DetailNode tagNode) { 127 final DetailNode tagDescription = 128 JavadocUtil.findFirstToken(tagNode, JavadocTokenTypes.DESCRIPTION); 129 return tagDescription == null; 130 } 131 132}