Source code: com/puppycrawl/tools/checkstyle/checks/SimplifyBooleanExpressionCheck.java
1 ////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code for adherence to a set of rules.
3 // Copyright (C) 2001-2002 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;
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 * <module name="SimplifyBooleanExpression"/>
39 * </pre>
40 * @author lkuehne
41 */
42 public class SimplifyBooleanExpressionCheck
43 extends Check
44 {
45 /** @see Check */
46 public int[] getDefaultTokens()
47 {
48 return new int[] {TokenTypes.LITERAL_TRUE, TokenTypes.LITERAL_FALSE};
49 }
50
51 /**
52 * Prevent user from changing tokens in the configuration.
53 * @see com.puppycrawl.tools.checkstyle.api.Check
54 */
55 public int[] getAcceptableTokens()
56 {
57 return new int[] {};
58 }
59
60 /** @see com.puppycrawl.tools.checkstyle.api.Check */
61 public int[] getRequiredTokens()
62 {
63 return new int[] {TokenTypes.LITERAL_TRUE, TokenTypes.LITERAL_FALSE};
64 }
65
66 /** @see com.puppycrawl.tools.checkstyle.api.Check */
67 public void visitToken(DetailAST aAST)
68 {
69 final DetailAST parent = aAST.getParent();
70 switch (parent.getType()) {
71 case TokenTypes.NOT_EQUAL:
72 case TokenTypes.EQUAL:
73 case TokenTypes.LNOT:
74 case TokenTypes.LOR:
75 case TokenTypes.LAND:
76 log(parent.getLineNo(), parent.getColumnNo(),
77 "simplify.expression");
78 break;
79 default:
80 break;
81 }
82 }
83 }