Coverage Report - com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyForIteratorPadCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
EmptyForIteratorPadCheck
100%
22/22
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  
 
 29  
 /**
 30  
  * <p>Checks the padding of an empty for iterator; that is whether a
 31  
  * space is required at an empty for iterator, or such spaces are
 32  
  * forbidden. No check occurs if there is a line wrap at the iterator, as in
 33  
  * </p>
 34  
  * <pre class="body">
 35  
 for (Iterator foo = very.long.line.iterator();
 36  
       foo.hasNext();
 37  
      )
 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="EmptyForIteratorPad"/&gt;
 48  
  * </pre>
 49  
  *
 50  
  * @author Rick Giles
 51  
  */
 52  
 @StatelessCheck
 53  13
 public class EmptyForIteratorPadCheck
 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_WS_FOLLOWED = "ws.followed";
 61  
 
 62  
     /**
 63  
      * A key is pointing to the warning message text in "messages.properties"
 64  
      * file.
 65  
      */
 66  
     public static final String MSG_WS_NOT_FOLLOWED = "ws.notFollowed";
 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_ITERATOR};
 101  
     }
 102  
 
 103  
     @Override
 104  
     public void visitToken(DetailAST ast) {
 105  24
         if (ast.getChildCount() == 0) {
 106  
             //empty for iterator. test pad after semi.
 107  10
             final DetailAST semi = ast.getPreviousSibling();
 108  10
             final String line = getLines()[semi.getLineNo() - 1];
 109  10
             final int after = semi.getColumnNo() + 1;
 110  
             //don't check if at end of line
 111  10
             if (after < line.length()) {
 112  8
                 if (option == PadOption.NOSPACE
 113  4
                     && Character.isWhitespace(line.charAt(after))) {
 114  3
                     log(semi.getLineNo(), after, MSG_WS_FOLLOWED, SEMICOLON);
 115  
                 }
 116  5
                 else if (option == PadOption.SPACE
 117  4
                          && !Character.isWhitespace(line.charAt(after))) {
 118  1
                     log(semi.getLineNo(), after, MSG_WS_NOT_FOLLOWED, SEMICOLON);
 119  
                 }
 120  
             }
 121  
         }
 122  24
     }
 123  
 }