Coverage Report - com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
NeedBracesCheck
100%
9/9
100%
8/8
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.blocks;
 21  
 
 22  
 
 23  
 import com.puppycrawl.tools.checkstyle.api.Check;
 24  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 25  
 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
 26  
 
 27  
 /**
 28  
  * <p>
 29  
  * Checks for braces around code blocks.
 30  
  * </p>
 31  
  * <p> By default the check will check the following blocks:
 32  
  *  {@link TokenTypes#LITERAL_DO LITERAL_DO},
 33  
  *  {@link TokenTypes#LITERAL_ELSE LITERAL_ELSE},
 34  
  *  {@link TokenTypes#LITERAL_FOR LITERAL_FOR},
 35  
  *  {@link TokenTypes#LITERAL_IF LITERAL_IF},
 36  
  *  {@link TokenTypes#LITERAL_WHILE LITERAL_WHILE}.
 37  
  * </p>
 38  
  * <p>
 39  
  * An example of how to configure the check is:
 40  
  * </p>
 41  
  * <pre>
 42  
  * &lt;module name="NeedBraces"/&gt;
 43  
  * </pre>
 44  
  * <p> An example of how to configure the check for <code>if</code> and
 45  
  * <code>else</code> blocks is:
 46  
  * </p>
 47  
  * <pre>
 48  
  * &lt;module name="NeedBraces"&gt;
 49  
  *     &lt;property name="tokens" value="LITERAL_IF, LITERAL_ELSE"/&gt;
 50  
  * &lt;/module&gt;
 51  
  * </pre>
 52  
  *
 53  
  * @author Rick Giles
 54  
  * @version 1.0
 55  
  */
 56  1
 public class NeedBracesCheck extends Check
 57  
 {
 58  
     @Override
 59  
     public int[] getDefaultTokens()
 60  
     {
 61  1
         return new int[] {
 62  
             TokenTypes.LITERAL_DO,
 63  
             TokenTypes.LITERAL_ELSE,
 64  
             TokenTypes.LITERAL_FOR,
 65  
             TokenTypes.LITERAL_IF,
 66  
             TokenTypes.LITERAL_WHILE,
 67  
         };
 68  
     }
 69  
 
 70  
     @Override
 71  
     public void visitToken(DetailAST aAST)
 72  
     {
 73  27
         final DetailAST slistAST = aAST.findFirstToken(TokenTypes.SLIST);
 74  27
         boolean isElseIf = false;
 75  27
         if ((aAST.getType() == TokenTypes.LITERAL_ELSE)
 76  
             && (aAST.findFirstToken(TokenTypes.LITERAL_IF) != null))
 77  
         {
 78  1
             isElseIf = true;
 79  
         }
 80  27
         if ((slistAST == null) && !isElseIf) {
 81  17
             log(aAST.getLineNo(), "needBraces", aAST.getText());
 82  
         }
 83  27
     }
 84  
 }