KickJava   Java API By Example, From Geeks To Geeks.

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


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 the npath complexity against a specified limt (default = 200).
26  * The npath metric computes the number of possible execution paths
27  * through a function. Similar to the cyclomatic complexity but also
28  * takes into account the nesting of conditional statements and
29  * multi-part boolean expressions.
30  *
31  * @author <a HREF="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
32  * @author o_sukhodolsky
33  * TODO: For every or: _value += (_orCount * (nestedValue - 1));
34  * TODO: For every and: ???
35  */

36 public final class NPathComplexityCheck extends AbstractComplexityCheck
37 {
38     /** Default allowed complexity. */
39     private static final int DEFAULT_MAX = 200;
40
41     /** Creates new instance of the check. */
42     public NPathComplexityCheck()
43     {
44         super(DEFAULT_MAX);
45     }
46
47     /** {@inheritDoc} */
48     public int[] getDefaultTokens()
49     {
50         return new int[] {
51             TokenTypes.CTOR_DEF,
52             TokenTypes.METHOD_DEF,
53             TokenTypes.STATIC_INIT,
54             TokenTypes.INSTANCE_INIT,
55             TokenTypes.LITERAL_WHILE,
56             TokenTypes.LITERAL_DO,
57             TokenTypes.LITERAL_FOR,
58             TokenTypes.LITERAL_IF,
59             TokenTypes.LITERAL_ELSE,
60             TokenTypes.LITERAL_SWITCH,
61             TokenTypes.LITERAL_CASE,
62             TokenTypes.LITERAL_TRY,
63             TokenTypes.LITERAL_CATCH,
64             TokenTypes.QUESTION,
65         };
66     }
67
68     /** {@inheritDoc} */
69     public void visitToken(DetailAST aAST)
70     {
71         switch (aAST.getType()) {
72         case TokenTypes.LITERAL_WHILE:
73         case TokenTypes.LITERAL_DO:
74         case TokenTypes.LITERAL_FOR:
75         case TokenTypes.LITERAL_IF:
76         case TokenTypes.QUESTION:
77         case TokenTypes.LITERAL_TRY:
78         case TokenTypes.LITERAL_SWITCH:
79             visitMultiplyingConditional();
80             break;
81         case TokenTypes.LITERAL_ELSE:
82         case TokenTypes.LITERAL_CATCH:
83         case TokenTypes.LITERAL_CASE:
84             visitAddingConditional();
85             break;
86         default:
87             super.visitToken(aAST);
88         }
89     }
90
91     /** {@inheritDoc} */
92     public void leaveToken(DetailAST aAST)
93     {
94         switch (aAST.getType()) {
95         case TokenTypes.LITERAL_WHILE:
96         case TokenTypes.LITERAL_DO:
97         case TokenTypes.LITERAL_FOR:
98         case TokenTypes.LITERAL_IF:
99         case TokenTypes.QUESTION:
100         case TokenTypes.LITERAL_TRY:
101         case TokenTypes.LITERAL_SWITCH:
102             leaveMultiplyingConditional();
103             break;
104         case TokenTypes.LITERAL_ELSE:
105         case TokenTypes.LITERAL_CATCH:
106         case TokenTypes.LITERAL_CASE:
107             leaveAddingConditional();
108             break;
109         default:
110             super.leaveToken(aAST);
111         }
112     }
113
114     /** {@inheritDoc} */
115     protected String JavaDoc getMessageID()
116     {
117         return "npathComplexity";
118     }
119
120     /** Visits else, catch or case. */
121     private void visitAddingConditional()
122     {
123         pushValue();
124     }
125
126     /** Leaves else, catch or case. */
127     private void leaveAddingConditional()
128     {
129         setCurrentValue((getCurrentValue() - 1) + popValue());
130     }
131
132     /** Visits while, do, for, if, try, ? (in ?::) or switch. */
133     private void visitMultiplyingConditional()
134     {
135         pushValue();
136     }
137
138     /** Leaves while, do, for, if, try, ? (in ?::) or switch. */
139     private void leaveMultiplyingConditional()
140     {
141         setCurrentValue((getCurrentValue() + 1) * popValue());
142     }
143 }
144
Popular Tags