001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2019 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.metrics;
021
022import java.util.ArrayDeque;
023import java.util.Deque;
024
025import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
026import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
027import com.puppycrawl.tools.checkstyle.api.DetailAST;
028import com.puppycrawl.tools.checkstyle.api.TokenTypes;
029import com.puppycrawl.tools.checkstyle.utils.CheckUtil;
030
031/**
032 * Restricts nested boolean operators (&&, ||, &, | and ^) to
033 * a specified depth (default = 3).
034 * Note: &, | and ^ are not checked if they are part of constructor or
035 * method call because they can be applied to non boolean variables and
036 * Checkstyle does not know types of methods from different classes.
037 *
038 */
039@FileStatefulCheck
040public final class BooleanExpressionComplexityCheck extends AbstractCheck {
041
042    /**
043     * A key is pointing to the warning message text in "messages.properties"
044     * file.
045     */
046    public static final String MSG_KEY = "booleanExpressionComplexity";
047
048    /** Default allowed complexity. */
049    private static final int DEFAULT_MAX = 3;
050
051    /** Stack of contexts. */
052    private final Deque<Context> contextStack = new ArrayDeque<>();
053    /** Maximum allowed complexity. */
054    private int max;
055    /** Current context. */
056    private Context context = new Context(false);
057
058    /** Creates new instance of the check. */
059    public BooleanExpressionComplexityCheck() {
060        max = DEFAULT_MAX;
061    }
062
063    @Override
064    public int[] getDefaultTokens() {
065        return new int[] {
066            TokenTypes.CTOR_DEF,
067            TokenTypes.METHOD_DEF,
068            TokenTypes.EXPR,
069            TokenTypes.LAND,
070            TokenTypes.BAND,
071            TokenTypes.LOR,
072            TokenTypes.BOR,
073            TokenTypes.BXOR,
074        };
075    }
076
077    @Override
078    public int[] getRequiredTokens() {
079        return new int[] {
080            TokenTypes.CTOR_DEF,
081            TokenTypes.METHOD_DEF,
082            TokenTypes.EXPR,
083        };
084    }
085
086    @Override
087    public int[] getAcceptableTokens() {
088        return new int[] {
089            TokenTypes.CTOR_DEF,
090            TokenTypes.METHOD_DEF,
091            TokenTypes.EXPR,
092            TokenTypes.LAND,
093            TokenTypes.BAND,
094            TokenTypes.LOR,
095            TokenTypes.BOR,
096            TokenTypes.BXOR,
097        };
098    }
099
100    /**
101     * Setter for maximum allowed complexity.
102     * @param max new maximum allowed complexity.
103     */
104    public void setMax(int max) {
105        this.max = max;
106    }
107
108    @Override
109    public void visitToken(DetailAST ast) {
110        switch (ast.getType()) {
111            case TokenTypes.CTOR_DEF:
112            case TokenTypes.METHOD_DEF:
113                visitMethodDef(ast);
114                break;
115            case TokenTypes.EXPR:
116                visitExpr();
117                break;
118            case TokenTypes.BOR:
119                if (!isPipeOperator(ast) && !isPassedInParameter(ast)) {
120                    context.visitBooleanOperator();
121                }
122                break;
123            case TokenTypes.BAND:
124            case TokenTypes.BXOR:
125                if (!isPassedInParameter(ast)) {
126                    context.visitBooleanOperator();
127                }
128                break;
129            case TokenTypes.LAND:
130            case TokenTypes.LOR:
131                context.visitBooleanOperator();
132                break;
133            default:
134                throw new IllegalArgumentException("Unknown type: " + ast);
135        }
136    }
137
138    /**
139     * Checks if logical operator is part of constructor or method call.
140     * @param logicalOperator logical operator
141     * @return true if logical operator is part of constructor or method call
142     */
143    private static boolean isPassedInParameter(DetailAST logicalOperator) {
144        return logicalOperator.getParent().getParent().getType() == TokenTypes.ELIST;
145    }
146
147    /**
148     * Checks if {@link TokenTypes#BOR binary OR} is applied to exceptions
149     * in
150     * <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.20">
151     * multi-catch</a> (pipe-syntax).
152     * @param binaryOr {@link TokenTypes#BOR binary or}
153     * @return true if binary or is applied to exceptions in multi-catch.
154     */
155    private static boolean isPipeOperator(DetailAST binaryOr) {
156        return binaryOr.getParent().getType() == TokenTypes.TYPE;
157    }
158
159    @Override
160    public void leaveToken(DetailAST ast) {
161        switch (ast.getType()) {
162            case TokenTypes.CTOR_DEF:
163            case TokenTypes.METHOD_DEF:
164                leaveMethodDef();
165                break;
166            case TokenTypes.EXPR:
167                leaveExpr(ast);
168                break;
169            default:
170                // Do nothing
171        }
172    }
173
174    /**
175     * Creates new context for a given method.
176     * @param ast a method we start to check.
177     */
178    private void visitMethodDef(DetailAST ast) {
179        contextStack.push(context);
180        final boolean check = !CheckUtil.isEqualsMethod(ast);
181        context = new Context(check);
182    }
183
184    /** Removes old context. */
185    private void leaveMethodDef() {
186        context = contextStack.pop();
187    }
188
189    /** Creates and pushes new context. */
190    private void visitExpr() {
191        contextStack.push(context);
192        context = new Context(context.isChecking());
193    }
194
195    /**
196     * Restores previous context.
197     * @param ast expression we leave.
198     */
199    private void leaveExpr(DetailAST ast) {
200        context.checkCount(ast);
201        context = contextStack.pop();
202    }
203
204    /**
205     * Represents context (method/expression) in which we check complexity.
206     *
207     */
208    private class Context {
209
210        /**
211         * Should we perform check in current context or not.
212         * Usually false if we are inside equals() method.
213         */
214        private final boolean checking;
215        /** Count of boolean operators. */
216        private int count;
217
218        /**
219         * Creates new instance.
220         * @param checking should we check in current context or not.
221         */
222        Context(boolean checking) {
223            this.checking = checking;
224            count = 0;
225        }
226
227        /**
228         * Getter for checking property.
229         * @return should we check in current context or not.
230         */
231        public boolean isChecking() {
232            return checking;
233        }
234
235        /** Increases operator counter. */
236        public void visitBooleanOperator() {
237            ++count;
238        }
239
240        /**
241         * Checks if we violates maximum allowed complexity.
242         * @param ast a node we check now.
243         */
244        public void checkCount(DetailAST ast) {
245            if (checking && count > max) {
246                final DetailAST parentAST = ast.getParent();
247
248                log(parentAST, MSG_KEY, count, max);
249            }
250        }
251
252    }
253
254}