Coverage Report - com.puppycrawl.tools.checkstyle.checks.coding.OneStatementPerLineCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
OneStatementPerLineCheck
100%
28/28
93%
14/15
2.5
 
 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.coding;
 20  
 
 21  
 import com.puppycrawl.tools.checkstyle.api.Check;
 22  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 23  
 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
 24  
 
 25  
 /**
 26  
  * Restricts the number of statements per line to one.
 27  
  * @author Alexander Jesse
 28  
  * @author Oliver Burn
 29  
  */
 30  1
 public final class OneStatementPerLineCheck extends Check
 31  
 {
 32  
     /** hold the line-number where the last statement ended. */
 33  1
     private int mLastStatementEnd = -1;
 34  
     /** tracks the depth of EXPR tokens. */
 35  
     private int mExprDepth;
 36  
 
 37  
     /**
 38  
      * The for-header usually has 3 statements on one line, but THIS IS OK.
 39  
      */
 40  
     private boolean mInForHeader;
 41  
 
 42  
     @Override
 43  
     public int[] getDefaultTokens()
 44  
     {
 45  1
         return new int[] {
 46  
             TokenTypes.EXPR, TokenTypes.SEMI, TokenTypes.FOR_INIT,
 47  
             TokenTypes.FOR_ITERATOR,
 48  
         };
 49  
     }
 50  
 
 51  
     @Override
 52  
     public void beginTree(DetailAST aRootAST)
 53  
     {
 54  1
         mExprDepth = 0;
 55  1
         mInForHeader = false;
 56  1
         mLastStatementEnd = -1;
 57  1
     }
 58  
 
 59  
     @Override
 60  
     public void visitToken(DetailAST aAst)
 61  
     {
 62  67
         switch (aAst.getType()) {
 63  
         case TokenTypes.EXPR:
 64  36
             visitExpr(aAst);
 65  36
             break;
 66  
         case TokenTypes.SEMI:
 67  29
             visitSemi(aAst);
 68  29
             break;
 69  
         case TokenTypes.FOR_INIT:
 70  1
             mInForHeader = true;
 71  1
             break;
 72  
         default:
 73  
             break;
 74  
         }
 75  67
     }
 76  
 
 77  
     @Override
 78  
     public void leaveToken(DetailAST aAst)
 79  
     {
 80  67
         switch (aAst.getType()) {
 81  
         case TokenTypes.FOR_ITERATOR:
 82  1
             mInForHeader = false;
 83  1
             break;
 84  
         case TokenTypes.EXPR:
 85  36
             mExprDepth--;
 86  36
             break;
 87  
         default:
 88  
             break;
 89  
         }
 90  67
     }
 91  
 
 92  
     /**
 93  
      * Mark the state-change for the statement (entering) and remember the
 94  
      * first line of the last statement. If the first line of the new
 95  
      * statement is the same as the last line of the last statement and we are
 96  
      * not within a for-statement, then the rule is violated.
 97  
      * @param aAst token for the {@link TokenTypes#EXPR}.
 98  
      */
 99  
     private void visitExpr(DetailAST aAst)
 100  
     {
 101  36
         mExprDepth++;
 102  36
         if (mExprDepth == 1
 103  
                 && !mInForHeader
 104  
                 && (mLastStatementEnd == aAst.getLineNo()))
 105  
         {
 106  2
             log(aAst, "multiple.statements.line");
 107  
         }
 108  36
     }
 109  
 
 110  
     /**
 111  
      * Mark the state-change for the statement (leaving) and remember the last
 112  
      * line of the last statement.
 113  
      * @param aAst for the {@link TokenTypes#SEMI}.
 114  
      */
 115  
     private void visitSemi(DetailAST aAst)
 116  
     {
 117  29
         if (mExprDepth == 0) {
 118  29
             mLastStatementEnd = aAst.getLineNo();
 119  
         }
 120  29
     }
 121  
 }