KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > PackageStats


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2003, Mike Fagan <mfagan@tde.com>
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
20 package edu.umd.cs.findbugs;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.SortedMap JavaDoc;
26 import java.util.TreeMap JavaDoc;
27
28 import edu.umd.cs.findbugs.xml.XMLOutput;
29 import edu.umd.cs.findbugs.xml.XMLWriteable;
30
31 /**
32  * Class to store package bug statistics.
33  *
34  * @author Mike Fagan
35  * @author Jay Dunning
36  */

37 public class PackageStats implements XMLWriteable {
38
39     public static class ClassStats implements XMLWriteable, Cloneable JavaDoc {
40         private String JavaDoc name;
41         private boolean isInterface;
42         // nBugs[0] is total; nBugs[n] is total for bug priority n
43
private int[] nBugs = new int[] { 0, 0, 0, 0, 0 };
44         private int size;
45
46         public ClassStats(String JavaDoc name) {
47             this.name = name;
48         }
49         
50         @Override JavaDoc
51         public Object JavaDoc clone() {
52             try {
53                 return super.clone();
54             } catch (CloneNotSupportedException JavaDoc e) {
55                 // can't happen
56
throw new AssertionError JavaDoc(e);
57             }
58         }
59
60         public void setInterface(boolean isInterface) {
61             this.isInterface = isInterface;
62         }
63         
64         public void setSize(int size) {
65             this.size = size;
66         }
67         
68         public void addError(BugInstance bug) {
69             ++nBugs[bug.getPriority()];
70             ++nBugs[0];
71         }
72         public int getTotalBugs() {
73             return nBugs[0];
74         }
75         public int getBugsAtPriority(int p) {
76             return nBugs[p];
77         }
78         public int size() {
79             return size;
80         }
81         public String JavaDoc getName() {
82             return name;
83         }
84         public void writeXML(XMLOutput xmlOutput) throws IOException JavaDoc {
85             xmlOutput.startTag("ClassStats");
86
87             xmlOutput.addAttribute("class", name);
88             xmlOutput.addAttribute("interface", String.valueOf(isInterface));
89             xmlOutput.addAttribute("size", String.valueOf(size));
90             xmlOutput.addAttribute("bugs", String.valueOf(nBugs[0]));
91             writeBugPriorities(xmlOutput, nBugs);
92
93             xmlOutput.stopTag(true);
94         }
95
96         /**
97          *
98          */

99         public void clearBugCounts() {
100             for(int i = 0; i < nBugs.length; i++) nBugs[i] = 0;
101             
102         }
103     }
104
105     public static final String JavaDoc ELEMENT_NAME = "PackageStats";
106     public static final int ALL_ERRORS = 0;
107     private final String JavaDoc packageName;
108     // nBugs[0] is total; nBugs[n] is total for bug priority n
109
private int[] nBugs = new int[] { 0, 0, 0, 0, 0 };
110     private int size;
111
112     // list of errors for this package
113
//private LinkedList<BugInstance> packageErrors = new LinkedList<BugInstance>();
114

115     // all classes and interfaces in this package
116
private SortedMap JavaDoc<String JavaDoc, ClassStats> packageMembers =
117         new TreeMap JavaDoc<String JavaDoc, ClassStats>();
118
119     public PackageStats(String JavaDoc packageName) {
120         this.packageName = packageName;
121     }
122
123     public Collection JavaDoc<ClassStats> getClassStats() {
124         return packageMembers.values();
125     }
126     public int getTotalBugs() {
127         return nBugs[0];
128     }
129     public int size() {
130         return size;
131     }
132     public int getBugsAtPriority(int p) {
133         return nBugs[p];
134     }
135     private ClassStats getClassStats(String JavaDoc name) {
136         ClassStats result = packageMembers.get(name);
137         if ( result == null ) {
138             result = new ClassStats(name);
139             packageMembers.put(name, result);
140         }
141
142         return result;
143     }
144     
145     public ClassStats getClassStatsOrNull(String JavaDoc name) {
146         ClassStats result = packageMembers.get(name);
147         return result;
148     }
149     public void addError(BugInstance bug) {
150         if (bug.getPriority() >= nBugs.length) return;
151         ++nBugs[bug.getPriority()];
152         ++nBugs[0];
153
154         getClassStats(bug.getPrimaryClass().getClassName()).addError(bug);
155     }
156
157     public void addClass(String JavaDoc name, boolean isInterface, int size) {
158         ClassStats classStats = getClassStats(name);
159         classStats.setInterface(isInterface);
160         classStats.setSize(size);
161         this.size += size;
162     }
163
164     public String JavaDoc getPackageName() {
165         return packageName;
166     }
167
168     public void writeXML(XMLOutput xmlOutput) throws IOException JavaDoc{
169
170         xmlOutput.startTag(ELEMENT_NAME);
171
172         xmlOutput.addAttribute("package", packageName);
173         xmlOutput.addAttribute("total_bugs", String.valueOf(nBugs[0]));
174         xmlOutput.addAttribute("total_types",
175             String.valueOf(packageMembers.size()));
176         xmlOutput.addAttribute("total_size", String.valueOf(size));
177         writeBugPriorities(xmlOutput, nBugs);
178
179         xmlOutput.stopTag(false);
180
181         for (ClassStats classStats : packageMembers.values()) {
182             classStats.writeXML(xmlOutput);
183         }
184
185         xmlOutput.closeTag(ELEMENT_NAME);
186     }
187
188     /**
189      * Add priority attributes to a started tag.
190      * Each priority at offset n, where n &gt; 0, is output using
191      * attribute priority_n if the value at offset n is greater than
192      * zero.
193      *
194      * @param xmlOutput an output stream for which startTag has been
195      * called but stopTag has not.
196      * @param bugs an array for which the element at offset n is
197      * the number of bugs for priority n.
198      */

199     public static void writeBugPriorities(XMLOutput xmlOutput, int[] bugs) throws IOException JavaDoc {
200         int i = bugs.length;
201         while ( --i > 0 ) {
202             if ( bugs[i] > 0 ) {
203                 xmlOutput.addAttribute("priority_" + i,
204                     String.valueOf(bugs[i]));
205             }
206         }
207     }
208
209     /**
210      *
211      */

212     public void clearBugCounts() {
213         for(int i = 0; i < nBugs.length; i++)
214             nBugs[i] = 0;
215         
216         for(ClassStats classStats : packageMembers.values()) {
217             classStats.clearBugCounts();
218         }
219         
220     }
221 }
222
223 // vim:ts=4
224
Popular Tags