KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > detect > FindBugsSummaryStats


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2003-2005, 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.detect;
21
22
23 import edu.umd.cs.findbugs.*;
24 import edu.umd.cs.findbugs.ba.ClassContext;
25 import edu.umd.cs.findbugs.visitclass.PreorderVisitor;
26 import java.io.PrintStream JavaDoc;
27 import java.util.BitSet JavaDoc;
28 import org.apache.bcel.classfile.*;
29
30 public class FindBugsSummaryStats extends PreorderVisitor
31         implements Detector, BugReporterObserver {
32     private ProjectStats stats;
33
34        BitSet JavaDoc lines = new BitSet JavaDoc(500);
35        int methods = 0;
36        int fields = 0;
37        int classCodeSize;
38        int totalNCSS = 0;
39        int totalCodeSize = 0;
40        int totalMethods = 0;
41        int totalFields = 0;
42        boolean sawLineNumbers;
43
44
45        @Override JavaDoc
46             public void visit(JavaClass obj) {
47         lines.clear();
48         methods = 0;
49         fields = 0;
50         classCodeSize = 0;
51         sawLineNumbers = false;
52         }
53        @Override JavaDoc
54             public void visit(Method obj) {
55         methods++;
56         }
57        @Override JavaDoc
58             public void visit(Field obj) {
59         fields++;
60         }
61        @Override JavaDoc
62             public void visit(Code obj) {
63         classCodeSize += obj.getCode().length;
64         }
65
66        @Override JavaDoc
67             public void visitAfter(JavaClass obj) {
68            int linesNCSS = 1 + methods + fields;
69            if (sawLineNumbers)
70                linesNCSS += lines.cardinality();
71            else
72                linesNCSS += classCodeSize / 10;
73            if (stats != null)
74             stats.addClass(getDottedClassName(), obj.isInterface(),
75                     linesNCSS);
76             totalCodeSize += classCodeSize;
77             totalNCSS += linesNCSS;
78             totalMethods += methods;
79             totalFields += fields;
80
81         }
82
83        @Override JavaDoc
84             public void visit(LineNumber obj) {
85         sawLineNumbers = true;
86         int line = obj.getLineNumber();
87         lines.set(line);
88             }
89     
90
91     public FindBugsSummaryStats(BugReporter bugReporter) {
92         this.stats = bugReporter.getProjectStats();
93         bugReporter.addObserver(this);
94     }
95     public FindBugsSummaryStats() {
96         this.stats =null;
97     }
98
99     public void visitClassContext(ClassContext classContext) {
100         classContext.getJavaClass().accept(this);
101     }
102
103     public void report() {
104     }
105     @Override JavaDoc
106          public void report(PrintStream JavaDoc out) {
107         out.println("NCSS\t" + totalNCSS);
108         out.println("codeSz\t" + totalCodeSize);
109         out.println("methods\t" + totalMethods);
110         out.println("fields\t" + totalFields);
111     }
112     
113     public void reportBug(BugInstance bug) {
114         stats.addBug(bug);
115     }
116
117
118
119 }
120
Popular Tags