~ubuntu-branches/ubuntu/natty/cobertura/natty

« back to all changes in this revision

Viewing changes to src/net/sourceforge/cobertura/reporting/ComplexityCalculator.java

  • Committer: Bazaar Package Importer
  • Author(s): Miguel Landaeta
  • Date: 2010-05-11 19:21:46 UTC
  • mfrom: (0.1.4 sid) (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100511192146-j742v5jsl89ztndu
Tags: 1.9.4.1+dfsg-2
* Now Build-Depends on libservlet2.5-java and add a missing Depends
  on the same package. (Closes: #580842). 
* Simplify list of JRE dependences for cobertura and drop JRE dependences for
  libcobertura-java as Java libraries are no longer required to depend on a
  JVM.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
7
9
 *
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
22
24
 */
23
25
package net.sourceforge.cobertura.reporting;
24
26
 
25
 
import java.io.File;
26
27
import java.io.IOException;
27
28
import java.util.HashMap;
28
29
import java.util.Iterator;
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;
38
41
 
39
42
import org.apache.log4j.Logger;
40
43
 
52
55
public class ComplexityCalculator {
53
56
        private static final Logger logger = Logger.getLogger(ComplexityCalculator.class);
54
57
 
55
 
        public static final Complexity ZERO_COMPLEXITY = new Complexity(0,0);
 
58
        public static final Complexity ZERO_COMPLEXITY = new Complexity();
56
59
        
57
60
        // Finder used to map source file names to existing files
58
61
        private final FileFinder finder;
77
80
        }
78
81
        
79
82
        /**
 
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.  
 
88
         *
 
89
         * @param file The input stream for which you want to calculate
 
90
         *        the complexity
 
91
         * @return average complexity for the specified input stream 
 
92
         */
 
93
        private Complexity getAccumlatedCCNForSource(String sourceFileName, Source source) {
 
94
                if (source == null)
 
95
                {
 
96
                        return ZERO_COMPLEXITY;
 
97
                }
 
98
                if (!sourceFileName.endsWith(".java"))
 
99
                {
 
100
                        return ZERO_COMPLEXITY;
 
101
                }
 
102
                Javancss javancss = new Javancss(source.getInputStream());
 
103
 
 
104
                if (javancss.getLastErrorMessage() != null)
 
105
                {
 
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());
 
109
                }
 
110
 
 
111
                List methodMetrics = javancss.getFunctionMetrics();
 
112
                int classCcn = 0;
 
113
        for( Iterator method = methodMetrics.iterator(); method.hasNext();)
 
114
        {
 
115
                FunctionMetric singleMethodMetrics = (FunctionMetric)method.next();
 
116
                classCcn += singleMethodMetrics.ccn;
 
117
        }
 
118
                
 
119
                return new Complexity( classCcn, methodMetrics.size());
 
120
        }
 
121
 
 
122
        /**
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 
85
129
         *
86
130
         * @param file The source file for which you want to calculate
87
131
         *        the complexity
88
132
         * @return average complexity for the specified source file 
 
133
         * @throws IOException 
89
134
         */
90
 
        private Complexity getAccumlatedCCNForSingleFile(File file) {
91
 
                Javancss javancss = new Javancss(file.getAbsolutePath());
92
 
 
93
 
                List methodComplexities = javancss.getMethodComplexities();
94
 
                if (methodComplexities.size() <= 0)
95
 
                        return ZERO_COMPLEXITY;
96
 
 
97
 
                int ccnAccumulator = 0;
98
 
                Iterator iter = methodComplexities.iterator();
99
 
                while (iter.hasNext())
100
 
                {
101
 
                        ccnAccumulator += ((Integer)iter.next()).intValue();
102
 
                }
103
 
                
104
 
                return new Complexity( ccnAccumulator, methodComplexities.size());
 
135
        private Complexity getAccumlatedCCNForSingleFile(String sourceFileName) throws IOException {
 
136
                Source source = finder.getSource(sourceFileName);
 
137
                try
 
138
                {
 
139
                return getAccumlatedCCNForSource(sourceFileName, source);
 
140
                }
 
141
                finally
 
142
                {
 
143
                        if (source != null)
 
144
                        {
 
145
                                source.close();
 
146
                        }
 
147
                }
105
148
        }
106
149
 
107
 
 
108
150
        /**
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;
181
223
                try {
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+"]");
185
227
                }