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 java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.List;
25  import java.util.stream.Collectors;
26  
27  import com.puppycrawl.tools.checkstyle.StatelessCheck;
28  import com.puppycrawl.tools.checkstyle.api.DetailAST;
29  import com.puppycrawl.tools.checkstyle.api.DetailNode;
30  import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
31  import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
32  
33  /**
34   * Checks that a JavaDoc block can fit on a single line and doesn't
35   * contain at-clauses. Javadoc comment that contains at least one at-clause
36   * should be formatted in a few lines.<br>
37   * All inline at-clauses are ignored by default.
38   *
39   * <p>The Check has the following properties:
40   * <br><b>ignoredTags</b> - allows to specify a list of at-clauses
41   * (including custom at-clauses) to be ignored by the check.
42   * <br><b>ignoreInlineTags</b> - whether inline at-clauses must be ignored.
43   * </p>
44   *
45   * <p>Default configuration:
46   * <pre>
47   * &lt;module name=&quot;SingleLineJavadoc&quot;/&gt;
48   * </pre>
49   * To specify a list of ignored at-clauses and make inline at-clauses not ignored in general:
50   * <pre>
51   * &lt;module name=&quot;SingleLineJavadoc&quot;&gt;
52   *     &lt;property name=&quot;ignoredTags&quot; value=&quot;&#64;inheritDoc, &#64;link&quot;/&gt;
53   *     &lt;property name=&quot;ignoreInlineTags&quot; value=&quot;false&quot;/&gt;
54   * &lt;/module&gt;
55   * </pre>
56   *
57   *
58   */
59  @StatelessCheck
60  public class SingleLineJavadocCheck extends AbstractJavadocCheck {
61  
62      /**
63       * A key is pointing to the warning message text in "messages.properties"
64       * file.
65       */
66      public static final String MSG_KEY = "singleline.javadoc";
67  
68      /**
69       * Allows to specify a list of tags to be ignored by check.
70       */
71      private List<String> ignoredTags = new ArrayList<>();
72  
73      /** Whether inline tags must be ignored. **/
74      private boolean ignoreInlineTags = true;
75  
76      /**
77       * Sets a list of tags to be ignored by check.
78       *
79       * @param tags to be ignored by check.
80       */
81      public void setIgnoredTags(String... tags) {
82          ignoredTags = Arrays.stream(tags).collect(Collectors.toList());
83      }
84  
85      /**
86       * Sets whether inline tags must be ignored.
87       *
88       * @param ignoreInlineTags whether inline tags must be ignored.
89       */
90      public void setIgnoreInlineTags(boolean ignoreInlineTags) {
91          this.ignoreInlineTags = ignoreInlineTags;
92      }
93  
94      @Override
95      public int[] getDefaultJavadocTokens() {
96          return new int[] {
97              JavadocTokenTypes.JAVADOC,
98          };
99      }
100 
101     @Override
102     public int[] getRequiredJavadocTokens() {
103         return getAcceptableJavadocTokens();
104     }
105 
106     @Override
107     public void visitJavadocToken(DetailNode ast) {
108         if (isSingleLineJavadoc(getBlockCommentAst())
109                 && (hasJavadocTags(ast) || !ignoreInlineTags && hasJavadocInlineTags(ast))) {
110             log(ast.getLineNumber(), MSG_KEY);
111         }
112     }
113 
114     /**
115      * Checks if comment is single line comment.
116      *
117      * @param blockCommentStart the AST tree in which a block comment starts
118      * @return true, if comment is single line comment.
119      */
120     private static boolean isSingleLineJavadoc(DetailAST blockCommentStart) {
121         final DetailAST blockCommentEnd = blockCommentStart.getLastChild();
122         return blockCommentStart.getLineNo() == blockCommentEnd.getLineNo();
123     }
124 
125     /**
126      * Checks if comment has javadoc tags which are not ignored. Also works
127      * on custom tags. As block tags can be interpreted only at the beginning of a line,
128      * only the first instance is checked.
129      *
130      * @param javadocRoot javadoc root node.
131      * @return true, if comment has javadoc tags which are not ignored.
132      * @see <a href=
133      * "https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#blockandinlinetags">
134      * Block and inline tags</a>
135      */
136     private boolean hasJavadocTags(DetailNode javadocRoot) {
137         final DetailNode javadocTagSection =
138                 JavadocUtil.findFirstToken(javadocRoot, JavadocTokenTypes.JAVADOC_TAG);
139         return javadocTagSection != null && !isTagIgnored(javadocTagSection);
140     }
141 
142     /**
143      * Checks if comment has in-line tags which are not ignored.
144      *
145      * @param javadocRoot javadoc root node.
146      * @return true, if comment has in-line tags which are not ignored.
147      * @see <a href=
148      * "https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#javadoctags">
149      * JavadocTags</a>
150      */
151     private boolean hasJavadocInlineTags(DetailNode javadocRoot) {
152         DetailNode javadocTagSection =
153                 JavadocUtil.findFirstToken(javadocRoot, JavadocTokenTypes.JAVADOC_INLINE_TAG);
154         boolean foundTag = false;
155         while (javadocTagSection != null) {
156             if (!isTagIgnored(javadocTagSection)) {
157                 foundTag = true;
158                 break;
159             }
160             javadocTagSection = JavadocUtil.getNextSibling(
161                     javadocTagSection, JavadocTokenTypes.JAVADOC_INLINE_TAG);
162         }
163         return foundTag;
164     }
165 
166     /**
167      * Checks if list of ignored tags contains javadocTagSection's javadoc tag.
168      *
169      * @param javadocTagSection to check javadoc tag in.
170      * @return true, if ignoredTags contains javadocTagSection's javadoc tag.
171      */
172     private boolean isTagIgnored(DetailNode javadocTagSection) {
173         return ignoredTags.contains(JavadocUtil.getTagName(javadocTagSection));
174     }
175 
176 }