KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > metrics > CyclomaticComplexityCheck


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.metrics;
20
21 import com.puppycrawl.tools.checkstyle.api.DetailAST;
22 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
23
24 /**
25  * Checks cyclomatic complexity against a specified limit. The complexity is
26  * measured by the number of "if", "while", "do", "for", "?:", "catch",
27  * "switch", "case", "&&" and "||" statements (plus one) in the body of
28  * the member. It is a measure of the minimum number of possible paths through
29  * the source and therefore the number of required tests. Generally 1-4 is
30  * considered good, 5-7 ok, 8-10 consider re-factoring, and 11+ re-factor now!
31  *
32  * @author <a HREF="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
33  * @author Oliver Burn
34  */

35 public class CyclomaticComplexityCheck
36     extends AbstractComplexityCheck
37 {
38     /** default allowed complexity */
39     private static final int DEFAULT_VALUE = 10;
40
41     /** Create an instance. */
42     public CyclomaticComplexityCheck()
43     {
44         super(DEFAULT_VALUE);
45     }
46
47     /** {@inheritDoc} */
48     public int[] getDefaultTokens()
49     {
50         return new int[] {
51             TokenTypes.CTOR_DEF,
52             TokenTypes.METHOD_DEF,
53             TokenTypes.INSTANCE_INIT,
54             TokenTypes.STATIC_INIT,
55             TokenTypes.LITERAL_WHILE,
56             TokenTypes.LITERAL_DO,
57             TokenTypes.LITERAL_FOR,
58             TokenTypes.LITERAL_IF,
59             TokenTypes.LITERAL_CASE,
60             TokenTypes.LITERAL_CATCH,
61             TokenTypes.QUESTION,
62             TokenTypes.LAND,
63             TokenTypes.LOR,
64         };
65     }
66
67     /** {@inheritDoc} */
68     protected final void visitTokenHook(DetailAST aAST)
69     {
70         incrementCurrentValue(1);
71     }
72
73     /** {@inheritDoc} */
74     protected final String JavaDoc getMessageID()
75     {
76         return "cyclomaticComplexity";
77     }
78 }
79
Popular Tags