001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2025 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.CommonUtil; 026import com.puppycrawl.tools.checkstyle.utils.JavadocUtil; 027 028/** 029 * <div> 030 * Checks that the block tag is followed by description. 031 * </div> 032 * 033 * @since 6.0 034 */ 035@StatelessCheck 036public class NonEmptyAtclauseDescriptionCheck extends AbstractJavadocCheck { 037 038 /** 039 * A key is pointing to the warning message text in "messages.properties" 040 * file. 041 */ 042 public static final String MSG_KEY = "non.empty.atclause"; 043 044 @Override 045 public int[] getDefaultJavadocTokens() { 046 return new int[] { 047 JavadocTokenTypes.PARAM_LITERAL, 048 JavadocTokenTypes.RETURN_LITERAL, 049 JavadocTokenTypes.THROWS_LITERAL, 050 JavadocTokenTypes.EXCEPTION_LITERAL, 051 JavadocTokenTypes.DEPRECATED_LITERAL, 052 }; 053 } 054 055 /** 056 * Adds a set of tokens the check is interested in. 057 * 058 * @param strRep the string representation of the tokens interested in 059 * @propertySince 7.3 060 * @noinspection RedundantMethodOverride 061 * @noinspectionreason Display module's unique property version 062 */ 063 @Override 064 public void setJavadocTokens(String... strRep) { 065 super.setJavadocTokens(strRep); 066 } 067 068 @Override 069 public void visitJavadocToken(DetailNode ast) { 070 if (isEmptyTag(ast.getParent())) { 071 log(ast.getLineNumber(), MSG_KEY); 072 } 073 } 074 075 /** 076 * Tests if block tag is empty. 077 * 078 * @param tagNode block tag. 079 * @return true, if block tag is empty. 080 */ 081 private static boolean isEmptyTag(DetailNode tagNode) { 082 final DetailNode tagDescription = 083 JavadocUtil.findFirstToken(tagNode, JavadocTokenTypes.DESCRIPTION); 084 return tagDescription == null 085 || hasOnlyEmptyText(tagDescription); 086 } 087 088 /** 089 * Tests if description node is empty (has only new lines and blank strings). 090 * 091 * @param description description node. 092 * @return true, if description node has only new lines and blank strings. 093 */ 094 private static boolean hasOnlyEmptyText(DetailNode description) { 095 boolean result = true; 096 for (DetailNode child : description.getChildren()) { 097 if (child.getType() != JavadocTokenTypes.LEADING_ASTERISK 098 && !CommonUtil.isBlank(child.getText())) { 099 result = false; 100 break; 101 } 102 } 103 return result; 104 } 105 106}