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