KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > coding > SimplifyBooleanExpressionCheck


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 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 public class SimplifyBooleanExpressionCheck
43         extends Check
44 {
45     /** {@inheritDoc} */
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      * @return an empty array.
55      */

56     public int[] getAcceptableTokens()
57     {
58         return new int[] {};
59     }
60
61     /** {@inheritDoc} */
62     public int[] getRequiredTokens()
63     {
64         return new int[] {TokenTypes.LITERAL_TRUE, TokenTypes.LITERAL_FALSE};
65     }
66
67     /** {@inheritDoc} */
68     public void visitToken(DetailAST aAST)
69     {
70         final DetailAST parent = aAST.getParent();
71         switch (parent.getType()) {
72         case TokenTypes.NOT_EQUAL:
73         case TokenTypes.EQUAL:
74         case TokenTypes.LNOT:
75         case TokenTypes.LOR:
76         case TokenTypes.LAND:
77             log(parent.getLineNo(), parent.getColumnNo(),
78                 "simplify.expression");
79             break;
80         default:
81             break;
82         }
83     }
84 }
85
Popular Tags