Coverage Report - com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyForInitializerPadCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
EmptyForInitializerPadCheck
100%
23/23
100%
12/12
2.6
 
 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.whitespace;
 21  
 
 22  
 import java.util.Locale;
 23  
 
 24  
 import com.puppycrawl.tools.checkstyle.StatelessCheck;
 25  
 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
 26  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 27  
 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
 28  
 import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
 29  
 
 30  
 /**
 31  
  * <p>Checks the padding of an empty for initializer; that is whether a
 32  
  * space is required at an empty for initializer, or such spaces are
 33  
  * forbidden. No check occurs if there is a line wrap at the initializer, as in
 34  
  * </p>
 35  
  * <pre class="body">
 36  
 for (
 37  
       ; i &lt; j; i++, j--)
 38  
    </pre>
 39  
  * <p>
 40  
  * The policy to verify is specified using the {@link PadOption} class and
 41  
  * defaults to {@link PadOption#NOSPACE}.
 42  
  * </p>
 43  
  * <p>
 44  
  * An example of how to configure the check is:
 45  
  * </p>
 46  
  * <pre>
 47  
  * &lt;module name="EmptyForInitializerPad"/&gt;
 48  
  * </pre>
 49  
  *
 50  
  * @author lkuehne
 51  
  */
 52  
 @StatelessCheck
 53  13
 public class EmptyForInitializerPadCheck
 54  
     extends AbstractCheck {
 55  
 
 56  
     /**
 57  
      * A key is pointing to the warning message text in "messages.properties"
 58  
      * file.
 59  
      */
 60  
     public static final String MSG_PRECEDED = "ws.preceded";
 61  
 
 62  
     /**
 63  
      * A key is pointing to the warning message text in "messages.properties"
 64  
      * file.
 65  
      */
 66  
     public static final String MSG_NOT_PRECEDED = "ws.notPreceded";
 67  
 
 68  
     /** Semicolon literal. */
 69  
     private static final String SEMICOLON = ";";
 70  
 
 71  
     /** The policy to enforce. */
 72  13
     private PadOption option = PadOption.NOSPACE;
 73  
 
 74  
     /**
 75  
      * Set the option to enforce.
 76  
      * @param optionStr string to decode option from
 77  
      * @throws IllegalArgumentException if unable to decode
 78  
      */
 79  
     public void setOption(String optionStr) {
 80  
         try {
 81  3
             option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
 82  
         }
 83  1
         catch (IllegalArgumentException iae) {
 84  1
             throw new IllegalArgumentException("unable to parse " + optionStr, iae);
 85  2
         }
 86  2
     }
 87  
 
 88  
     @Override
 89  
     public int[] getDefaultTokens() {
 90  14
         return getRequiredTokens();
 91  
     }
 92  
 
 93  
     @Override
 94  
     public int[] getAcceptableTokens() {
 95  5
         return getRequiredTokens();
 96  
     }
 97  
 
 98  
     @Override
 99  
     public int[] getRequiredTokens() {
 100  34
         return new int[] {TokenTypes.FOR_INIT};
 101  
     }
 102  
 
 103  
     @Override
 104  
     public void visitToken(DetailAST ast) {
 105  24
         if (ast.getChildCount() == 0) {
 106  
             //empty for initializer. test pad before semi.
 107  6
             final DetailAST semi = ast.getNextSibling();
 108  6
             final int semiLineIdx = semi.getLineNo() - 1;
 109  6
             final String line = getLines()[semiLineIdx];
 110  6
             final int before = semi.getColumnNo() - 1;
 111  
             //don't check if semi at beginning of line
 112  6
             if (!CommonUtils.hasWhitespaceBefore(before, line)) {
 113  4
                 if (option == PadOption.NOSPACE
 114  2
                     && Character.isWhitespace(line.charAt(before))) {
 115  1
                     log(semi.getLineNo(), before, MSG_PRECEDED, SEMICOLON);
 116  
                 }
 117  3
                 else if (option == PadOption.SPACE
 118  2
                          && !Character.isWhitespace(line.charAt(before))) {
 119  1
                     log(semi.getLineNo(), before, MSG_NOT_PRECEDED, SEMICOLON);
 120  
                 }
 121  
             }
 122  
         }
 123  24
     }
 124  
 }