KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > inspectors > testcases > violations > CyclomaticComplexityRuleViolationTestCase


1 /*
2  * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Hammurapi Group
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * URL: http://www.hammurapi.org
21  * e-Mail: support@hammurapi.biz
22 */

23 package org.hammurapi.inspectors.testcases.violations;
24
25 /**
26  * CyclomaticComplexityRule
27  * @author Pavel Vlasov
28  * @version $Revision: 1.1 $
29  */

30 public class CyclomaticComplexityRuleViolationTestCase {
31
32     private static org.apache.log4j.Logger logger =
33         org.apache.log4j.Logger.getRootLogger();
34
35     private static final int INT_ZERO = 0;
36     private static final int INT_1 = 1;
37     private static final int NEG_INT_1 = -INT_1;
38     private static final int INT_2 = 2;
39     private static final int NEG_INT_2 = -INT_2;
40     private static final int INT_3 = 3;
41     private static final int NEG_INT_3 = -INT_3;
42
43     private static final String JavaDoc CONST_SPACE = " ";
44     private static final char CONST_A = 'A';
45     private static final char CHAR_B = 'B';
46
47     // --- VIOLATION ---
48
/** Java doc automaticaly generated by Hammurapi */
49     public int complexMethod(final String JavaDoc strToProc, final boolean firstType) {
50         if (firstType) {
51             if (strToProc == null) {
52                 return INT_1;
53             } else if (strToProc.length() == INT_ZERO) {
54                 return INT_2;
55             } else if (strToProc.length() == INT_1 &&
56                 strToProc.equalsIgnoreCase(CONST_SPACE)) {
57                 return INT_3;
58             } else {
59                 int retVal = INT_ZERO;
60                 for (int i = INT_ZERO; i < strToProc.length(); i++) {
61                     if (strToProc.charAt(i) == CONST_A ||
62                         i % INT_3 == INT_2) {
63                         retVal++;
64                     } else {
65                         retVal += INT_2;
66                     }
67                 }
68                 return retVal;
69             }
70         } else {
71             if (strToProc == null) {
72                 return NEG_INT_1;
73             } else if (strToProc.length() == INT_ZERO) {
74                 return NEG_INT_2;
75             } else if (strToProc.length() == INT_1 &&
76                 strToProc.equalsIgnoreCase(CONST_SPACE)) {
77                 return NEG_INT_3;
78             } else {
79                 int retVal = INT_ZERO;
80                 for (int i = INT_ZERO; i < strToProc.length(); i++) {
81                     if (strToProc.charAt(i) == CONST_A ||
82                         i % INT_3 == INT_2) {
83                         retVal--;
84                     } else if (strToProc.charAt(i) == CHAR_B ||
85                             i % INT_3 == INT_2) {
86                         retVal -= INT_2;
87                     } else {
88                         retVal -= INT_3;
89                     }
90                 }
91                 return retVal;
92             }
93         }
94     }
95     //--- END VIOLATION ---
96

97     /** Java doc automaticaly generated by Hammurapi */
98     public int tooComplexClassMethod(final int valToProc) {
99         int retVal = INT_ZERO;
100
101         if (valToProc < INT_2 || valToProc > INT_3) {
102             retVal = INT_1;
103         }
104
105         return retVal;
106     }
107 }
108
109
Popular Tags