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