| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| CyclomaticComplexityCheck |
|
| 1.0;1 |
| 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.metrics; | |
| 20 | ||
| 21 | import java.math.BigInteger; | |
| 22 | ||
| 23 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
| 24 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
| 25 | ||
| 26 | /** | |
| 27 | * Checks cyclomatic complexity against a specified limit. The complexity is | |
| 28 | * measured by the number of "if", "while", "do", "for", "?:", "catch", | |
| 29 | * "switch", "case", "&&" and "||" statements (plus one) in the body of | |
| 30 | * the member. It is a measure of the minimum number of possible paths through | |
| 31 | * the source and therefore the number of required tests. Generally 1-4 is | |
| 32 | * considered good, 5-7 ok, 8-10 consider re-factoring, and 11+ re-factor now! | |
| 33 | * | |
| 34 | * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a> | |
| 35 | * @author Oliver Burn | |
| 36 | */ | |
| 37 | public class CyclomaticComplexityCheck | |
| 38 | extends AbstractComplexityCheck | |
| 39 | { | |
| 40 | /** default allowed complexity */ | |
| 41 | private static final int DEFAULT_VALUE = 10; | |
| 42 | ||
| 43 | /** Create an instance. */ | |
| 44 | public CyclomaticComplexityCheck() | |
| 45 | { | |
| 46 | 1 | super(DEFAULT_VALUE); |
| 47 | 1 | } |
| 48 | ||
| 49 | @Override | |
| 50 | public int[] getDefaultTokens() | |
| 51 | { | |
| 52 | 1 | return new int[] { |
| 53 | TokenTypes.CTOR_DEF, | |
| 54 | TokenTypes.METHOD_DEF, | |
| 55 | TokenTypes.INSTANCE_INIT, | |
| 56 | TokenTypes.STATIC_INIT, | |
| 57 | TokenTypes.LITERAL_WHILE, | |
| 58 | TokenTypes.LITERAL_DO, | |
| 59 | TokenTypes.LITERAL_FOR, | |
| 60 | TokenTypes.LITERAL_IF, | |
| 61 | TokenTypes.LITERAL_CASE, | |
| 62 | TokenTypes.LITERAL_CATCH, | |
| 63 | TokenTypes.QUESTION, | |
| 64 | TokenTypes.LAND, | |
| 65 | TokenTypes.LOR, | |
| 66 | }; | |
| 67 | } | |
| 68 | ||
| 69 | @Override | |
| 70 | protected final void visitTokenHook(DetailAST aAST) | |
| 71 | { | |
| 72 | 20 | incrementCurrentValue(BigInteger.ONE); |
| 73 | 20 | } |
| 74 | ||
| 75 | @Override | |
| 76 | protected final String getMessageID() | |
| 77 | { | |
| 78 | 10 | return "cyclomaticComplexity"; |
| 79 | } | |
| 80 | } |