4
4
* Copyright (C) 2005 Mark Doliner
5
5
* Copyright (C) 2005 Jeremy Thomerson
6
6
* Copyright (C) 2005 Grzegorz Lukasik
7
* Copyright (C) 2008 Tri Bao Ho
8
* Copyright (C) 2009 John Lewis
8
10
* Cobertura is free software; you can redistribute it and/or modify
9
11
* it under the terms of the GNU General Public License as published
33
34
import net.sourceforge.cobertura.coveragedata.PackageData;
34
35
import net.sourceforge.cobertura.coveragedata.ProjectData;
35
36
import net.sourceforge.cobertura.coveragedata.SourceFileData;
37
import net.sourceforge.cobertura.javancss.FunctionMetric;
36
38
import net.sourceforge.cobertura.javancss.Javancss;
37
39
import net.sourceforge.cobertura.util.FileFinder;
40
import net.sourceforge.cobertura.util.Source;
39
42
import org.apache.log4j.Logger;
52
55
public class ComplexityCalculator {
53
56
private static final Logger logger = Logger.getLogger(ComplexityCalculator.class);
55
public static final Complexity ZERO_COMPLEXITY = new Complexity(0,0);
58
public static final Complexity ZERO_COMPLEXITY = new Complexity();
57
60
// Finder used to map source file names to existing files
58
61
private final FileFinder finder;
83
* Calculates the code complexity number for an input stream.
84
* "CCN" stands for "code complexity number." This is
85
* sometimes referred to as McCabe's number. This method
86
* calculates the average cyclomatic code complexity of all
87
* methods of all classes in a given directory.
89
* @param file The input stream for which you want to calculate
91
* @return average complexity for the specified input stream
93
private Complexity getAccumlatedCCNForSource(String sourceFileName, Source source) {
96
return ZERO_COMPLEXITY;
98
if (!sourceFileName.endsWith(".java"))
100
return ZERO_COMPLEXITY;
102
Javancss javancss = new Javancss(source.getInputStream());
104
if (javancss.getLastErrorMessage() != null)
106
//there is an error while parsing the java file. log it
107
logger.warn("JavaNCSS got an error while parsing the java " + source.getOriginDesc() + "\n"
108
+ javancss.getLastErrorMessage());
111
List methodMetrics = javancss.getFunctionMetrics();
113
for( Iterator method = methodMetrics.iterator(); method.hasNext();)
115
FunctionMetric singleMethodMetrics = (FunctionMetric)method.next();
116
classCcn += singleMethodMetrics.ccn;
119
return new Complexity( classCcn, methodMetrics.size());
80
123
* Calculates the code complexity number for single source file.
81
124
* "CCN" stands for "code complexity number." This is
82
125
* sometimes referred to as McCabe's number. This method
83
126
* calculates the average cyclomatic code complexity of all
84
127
* methods of all classes in a given directory.
128
* @param sourceFileName
86
130
* @param file The source file for which you want to calculate
88
132
* @return average complexity for the specified source file
133
* @throws IOException
90
private Complexity getAccumlatedCCNForSingleFile(File file) {
91
Javancss javancss = new Javancss(file.getAbsolutePath());
93
List methodComplexities = javancss.getMethodComplexities();
94
if (methodComplexities.size() <= 0)
95
return ZERO_COMPLEXITY;
97
int ccnAccumulator = 0;
98
Iterator iter = methodComplexities.iterator();
99
while (iter.hasNext())
101
ccnAccumulator += ((Integer)iter.next()).intValue();
104
return new Complexity( ccnAccumulator, methodComplexities.size());
135
private Complexity getAccumlatedCCNForSingleFile(String sourceFileName) throws IOException {
136
Source source = finder.getSource(sourceFileName);
139
return getAccumlatedCCNForSource(sourceFileName, source);
109
151
* Computes CCN for all sources contained in the project.
110
152
* CCN for whole project is an average CCN for source files.
179
221
// Compute CCN and cache it for further use
180
222
Complexity result = ZERO_COMPLEXITY;
182
result = getAccumlatedCCNForSingleFile( finder.getFileForSource(sourceFileName));
224
result = getAccumlatedCCNForSingleFile( sourceFileName );
183
225
} catch( IOException ex) {
184
226
logger.info( "Cannot find source file during CCN computation, source=["+sourceFileName+"]");