KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > workflow > DefectDensity


1
2 /*
3  * FindBugs - Find bugs in Java programs
4  * Copyright (C) 2003-2005 William Pugh
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 edu.umd.cs.findbugs.workflow;
20
21 import edu.umd.cs.findbugs.BugCollection;
22 import edu.umd.cs.findbugs.PackageStats;
23 import edu.umd.cs.findbugs.Project;
24 import edu.umd.cs.findbugs.ProjectStats;
25 import edu.umd.cs.findbugs.SortedBugCollection;
26 import edu.umd.cs.findbugs.PackageStats.ClassStats;
27
28 /**
29  * Java main application to compute defect density for a bug collection
30  * (stored as an XML collection)
31  *
32  * @author William Pugh
33  */

34 public class DefectDensity {
35     private static void printRow(Object JavaDoc... values) {
36         for (Object JavaDoc s : values) {
37             System.out.print(s);
38             System.out.print("\t");
39         }
40         System.out.println();
41
42     }
43
44     public static double density(int bugs, int ncss) {
45         if (ncss == 0) return Double.NaN;
46         int bugsPer10KNCSS = 10000*bugs/ncss;
47         return bugsPer10KNCSS/10.0;
48     }
49     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
50
51         if (args.length>1 || (args.length>0 && "-help".equals(args[0]))) {
52             System.err.println("Usage: " + DefectDensity.class.getName() + " [<infile>]");
53             System.exit(1);
54         }
55
56         Project project = new Project();
57         BugCollection origCollection = new SortedBugCollection();
58         int argCount = 0;
59         if (argCount == args.length)
60             origCollection.readXML(System.in, project);
61         else
62             origCollection.readXML(args[argCount], project);
63         ProjectStats stats = origCollection.getProjectStats();
64         printRow("kind", "name", "density/KNCSS", "bugs", "NCSS");
65         double projectDensity = density(stats.getTotalBugs(), stats.getCodeSize());
66         printRow("project", origCollection.getCurrentAppVersion().getReleaseName(),
67                 projectDensity, stats.getTotalBugs(),
68                 stats.getCodeSize());
69         for (PackageStats p : stats.getPackageStats()) if (p.getTotalBugs() > 4) {
70             
71             
72                 double packageDensity = density( p.getTotalBugs(),p.size());
73                 if (Double.isNaN(packageDensity) || packageDensity < projectDensity) continue;
74                 printRow("package", p.getPackageName(),
75                         packageDensity, p.getTotalBugs(), p.size());
76             for (ClassStats c : p.getClassStats()) if (c.getTotalBugs() > 4) {
77                 double density = density( c.getTotalBugs(), c.size());
78                 if (Double.isNaN(density) || density < packageDensity) continue;
79                 printRow("class", c.getName(),
80                         density, c.getTotalBugs(), c.size());
81             }
82         }
83
84     }
85     
86
87 }
88
Popular Tags