1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.javadoc;
21  
22  import com.puppycrawl.tools.checkstyle.StatelessCheck;
23  import com.puppycrawl.tools.checkstyle.api.DetailNode;
24  import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
25  import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
26  
27  /**
28   * Checks that the at-clause tag is followed by description .
29   * Default configuration that will check {@code @param}, {@code @return},
30   * {@code @throws}, {@code @deprecated} to:
31   * <pre>
32   * &lt;module name=&quot;NonEmptyAtclauseDescription&quot;/&gt;
33   * </pre>
34   *
35   *
36   */
37  @StatelessCheck
38  public class NonEmptyAtclauseDescriptionCheck extends AbstractJavadocCheck {
39  
40      /**
41       * A key is pointing to the warning message text in "messages.properties"
42       * file.
43       */
44      public static final String MSG_KEY = "non.empty.atclause";
45  
46      @Override
47      public int[] getDefaultJavadocTokens() {
48          return new int[] {
49              JavadocTokenTypes.PARAM_LITERAL,
50              JavadocTokenTypes.RETURN_LITERAL,
51              JavadocTokenTypes.THROWS_LITERAL,
52              JavadocTokenTypes.EXCEPTION_LITERAL,
53              JavadocTokenTypes.DEPRECATED_LITERAL,
54          };
55      }
56  
57      @Override
58      public void visitJavadocToken(DetailNode ast) {
59          if (isEmptyTag(ast.getParent())) {
60              log(ast.getLineNumber(), MSG_KEY, ast.getText());
61          }
62      }
63  
64      /**
65       * Tests if at-clause tag is empty.
66       * @param tagNode at-clause tag.
67       * @return true, if at-clause tag is empty.
68       */
69      private static boolean isEmptyTag(DetailNode tagNode) {
70          final DetailNode tagDescription =
71                  JavadocUtil.findFirstToken(tagNode, JavadocTokenTypes.DESCRIPTION);
72          return tagDescription == null;
73      }
74  
75  }