Coverage Report - com.puppycrawl.tools.checkstyle.checks.whitespace.MethodParamPadCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
MethodParamPadCheck
100%
20/20
100%
16/16
3.25
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2014  Oliver Burn
 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  
 package com.puppycrawl.tools.checkstyle.checks.whitespace;
 20  
 
 21  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 22  
 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
 23  
 import com.puppycrawl.tools.checkstyle.api.Utils;
 24  
 import com.puppycrawl.tools.checkstyle.checks.AbstractOptionCheck;
 25  
 
 26  
 /**
 27  
  * <p>
 28  
  * Checks the padding between the identifier of a method definition,
 29  
  * constructor definition, method call, or constructor invocation;
 30  
  * and the left parenthesis of the parameter list.
 31  
  * That is, if the identifier and left parenthesis are on the same line,
 32  
  * checks whether a space is required immediately after the identifier or
 33  
  * such a space is forbidden.
 34  
  * If they are not on the same line, reports an error, unless configured to
 35  
  * allow line breaks.
 36  
  * </p>
 37  
  * <p> By default the check will check the following tokens:
 38  
  *  {@link TokenTypes#CTOR_DEF CTOR_DEF},
 39  
  *  {@link TokenTypes#LITERAL_NEW LITERAL_NEW},
 40  
  *  {@link TokenTypes#METHOD_CALL METHOD_CALL},
 41  
  *  {@link TokenTypes#METHOD_DEF METHOD_DEF},
 42  
  *  {@link TokenTypes#SUPER_CTOR_CALL SUPER_CTOR_CALL}.
 43  
  * </p>
 44  
  * <p>
 45  
  * An example of how to configure the check is:
 46  
  * </p>
 47  
  * <pre>
 48  
  * &lt;module name="MethodParamPad"/&gt;
 49  
  * </pre>
 50  
  * <p> An example of how to configure the check to require a space
 51  
  * after the identifier of a method definition, except if the left
 52  
  * parenthesis occurs on a new line, is:
 53  
  * </p>
 54  
  * <pre>
 55  
  * &lt;module name="MethodParamPad"&gt;
 56  
  *     &lt;property name="tokens" value="METHOD_DEF"/&gt;
 57  
  *     &lt;property name="option" value="space"/&gt;
 58  
  *     &lt;property name="allowLineBreaks" value="true"/&gt;
 59  
  * &lt;/module&gt;
 60  
  * </pre>
 61  
  * @author Rick Giles
 62  
  * @version 1.0
 63  
  */
 64  
 
 65  
 public class MethodParamPadCheck
 66  
     extends AbstractOptionCheck<PadOption>
 67  
 {
 68  
     /**
 69  
      * Sets the pad option to nospace.
 70  
      */
 71  
     public MethodParamPadCheck()
 72  
     {
 73  4
         super(PadOption.NOSPACE, PadOption.class);
 74  4
     }
 75  
 
 76  
     /** Whether whitespace is allowed if the method identifier is at a
 77  
      * linebreak */
 78  
     private boolean mAllowLineBreaks;
 79  
 
 80  
     @Override
 81  
     public int[] getDefaultTokens()
 82  
     {
 83  4
         return new int[] {
 84  
             TokenTypes.CTOR_DEF,
 85  
             TokenTypes.LITERAL_NEW,
 86  
             TokenTypes.METHOD_CALL,
 87  
             TokenTypes.METHOD_DEF,
 88  
             TokenTypes.SUPER_CTOR_CALL,
 89  
         };
 90  
     }
 91  
 
 92  
     @Override
 93  
     public void visitToken(DetailAST aAST)
 94  
     {
 95  
         final DetailAST parenAST;
 96  97
         if ((aAST.getType() == TokenTypes.METHOD_CALL)) {
 97  38
             parenAST = aAST;
 98  
         }
 99  
         else {
 100  59
             parenAST = aAST.findFirstToken(TokenTypes.LPAREN);
 101  
             // array construction => parenAST == null
 102  59
             if (parenAST == null) {
 103  4
                 return;
 104  
             }
 105  
         }
 106  
 
 107  93
         final String line = getLines()[parenAST.getLineNo() - 1];
 108  93
         if (Utils.whitespaceBefore(parenAST.getColumnNo(), line)) {
 109  24
             if (!mAllowLineBreaks) {
 110  16
                 log(parenAST, "line.previous", parenAST.getText());
 111  
             }
 112  
         }
 113  
         else {
 114  69
             final int before = parenAST.getColumnNo() - 1;
 115  69
             if ((PadOption.NOSPACE == getAbstractOption())
 116  
                 && (Character.isWhitespace(line.charAt(before))))
 117  
             {
 118  16
                 log(parenAST , "ws.preceded", parenAST.getText());
 119  
             }
 120  53
             else if ((PadOption.SPACE == getAbstractOption())
 121  
                      && !Character.isWhitespace(line.charAt(before)))
 122  
             {
 123  13
                 log(parenAST, "ws.notPreceded", parenAST.getText());
 124  
             }
 125  
         }
 126  93
     }
 127  
 
 128  
     /**
 129  
      * Control whether whitespace is flagged at linebreaks.
 130  
      * @param aAllowLineBreaks whether whitespace should be
 131  
      * flagged at linebreaks.
 132  
      */
 133  
     public void setAllowLineBreaks(boolean aAllowLineBreaks)
 134  
     {
 135  1
         mAllowLineBreaks = aAllowLineBreaks;
 136  1
     }
 137  
 }