Coverage Report - com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyForIteratorPadCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
EmptyForIteratorPadCheck
100%
13/13
100%
12/12
3
 
 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  
 
 20  
 package com.puppycrawl.tools.checkstyle.checks.whitespace;
 21  
 
 22  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 23  
 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
 24  
 import com.puppycrawl.tools.checkstyle.checks.AbstractOptionCheck;
 25  
 
 26  
 /**
 27  
  * <p>Checks the padding of an empty for iterator; that is whether a
 28  
  * space is required at an empty for iterator, or such spaces are
 29  
  * forbidden. No check occurs if there is a line wrap at the iterator, as in
 30  
  * </p>
 31  
  * <pre class="body">
 32  
 for (Iterator foo = very.long.line.iterator();
 33  
       foo.hasNext();
 34  
      )
 35  
    </pre>
 36  
  * <p>
 37  
  * The policy to verify is specified using the {@link PadOption} class and
 38  
  * defaults to {@link PadOption#NOSPACE}.
 39  
  * </p>
 40  
  * <p>
 41  
  * An example of how to configure the check is:
 42  
  * </p>
 43  
  * <pre>
 44  
  * &lt;module name="EmptyForIteratorPad"/&gt;
 45  
  * </pre>
 46  
  *
 47  
  * @author Rick Giles
 48  
  * @version 1.0
 49  
  */
 50  
 public class EmptyForIteratorPadCheck
 51  
     extends AbstractOptionCheck<PadOption>
 52  
 {
 53  
     /**
 54  
      * Sets the paren pad otion to nospace.
 55  
      */
 56  
     public EmptyForIteratorPadCheck()
 57  
     {
 58  2
         super(PadOption.NOSPACE, PadOption.class);
 59  2
     }
 60  
 
 61  
     @Override
 62  
     public int[] getDefaultTokens()
 63  
     {
 64  2
         return new int[] {TokenTypes.FOR_ITERATOR,
 65  
         };
 66  
     }
 67  
 
 68  
     @Override
 69  
     public void visitToken(DetailAST aAST)
 70  
     {
 71  22
         if (aAST.getChildCount() == 0) {
 72  
             //empty for iterator. test pad after semi.
 73  8
             final DetailAST semi = aAST.getPreviousSibling();
 74  8
             final String line = getLines()[semi.getLineNo() - 1];
 75  8
             final int after = semi.getColumnNo() + 1;
 76  
             //don't check if at end of line
 77  8
             if (after < line.length()) {
 78  6
                 if ((PadOption.NOSPACE == getAbstractOption())
 79  
                     && (Character.isWhitespace(line.charAt(after))))
 80  
                 {
 81  2
                     log(semi.getLineNo(), after, "ws.followed", ";");
 82  
                 }
 83  4
                 else if ((PadOption.SPACE == getAbstractOption())
 84  
                          && !Character.isWhitespace(line.charAt(after)))
 85  
                 {
 86  1
                     log(semi.getLineNo(), after, "ws.notFollowed", ";");
 87  
                 }
 88  
             }
 89  
         }
 90  22
     }
 91  
 }