Coverage Report - com.puppycrawl.tools.checkstyle.checks.coding.SimplifyBooleanExpressionCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
SimplifyBooleanExpressionCheck
77%
7/9
33%
2/6
2.25
 
 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.TokenTypes;
 23  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 24  
 
 25  
 /**
 26  
  * <p>
 27  
  * Checks for overly complicated boolean expressions. Currently finds code like
 28  
  * <code>if (b == true)</code>, <code>b || true</code>, <code>!false</code>,
 29  
  * etc.
 30  
  * </p>
 31  
  * <p>
 32  
  * Rationale: Complex boolean logic makes code hard to understand and maintain.
 33  
  * </p>
 34  
  * <p>
 35  
  * An example of how to configure the check is:
 36  
  * </p>
 37  
  * <pre>
 38  
  * &lt;module name="SimplifyBooleanExpression"/&gt;
 39  
  * </pre>
 40  
  * @author lkuehne
 41  
  */
 42  1
 public class SimplifyBooleanExpressionCheck
 43  
         extends Check
 44  
 {
 45  
     @Override
 46  
     public int[] getDefaultTokens()
 47  
     {
 48  1
         return new int[] {TokenTypes.LITERAL_TRUE, TokenTypes.LITERAL_FALSE};
 49  
     }
 50  
 
 51  
     @Override
 52  
     public int[] getAcceptableTokens()
 53  
     {
 54  
         // Return empty list to prevent user changing tokens in the
 55  
         // configuration.
 56  0
         return new int[] {};
 57  
     }
 58  
 
 59  
     @Override
 60  
     public int[] getRequiredTokens()
 61  
     {
 62  0
         return new int[] {TokenTypes.LITERAL_TRUE, TokenTypes.LITERAL_FALSE};
 63  
     }
 64  
 
 65  
     @Override
 66  
     public void visitToken(DetailAST aAST)
 67  
     {
 68  14
         final DetailAST parent = aAST.getParent();
 69  14
         switch (parent.getType()) {
 70  
         case TokenTypes.NOT_EQUAL:
 71  
         case TokenTypes.EQUAL:
 72  
         case TokenTypes.LNOT:
 73  
         case TokenTypes.LOR:
 74  
         case TokenTypes.LAND:
 75  6
             log(parent.getLineNo(), parent.getColumnNo(),
 76  
                 "simplify.expression");
 77  6
             break;
 78  
         default:
 79  
             break;
 80  
         }
 81  14
     }
 82  
 }