KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2003-2006, University of Maryland
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.PrintStream JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 /**
27  * Base class for BugReporters which provides convenient formatting
28  * and reporting of warnings and analysis errors.
29  *
30  * <p>
31  * "TextUIBugReporter" is a bit of a misnomer, since this class
32  * is useful in GUIs, too.
33  * </p>
34  *
35  * @author David Hovemeyer
36  */

37 public abstract class TextUIBugReporter extends AbstractBugReporter {
38     private boolean reportStackTrace;
39     private boolean useLongBugCodes = false;
40     private static final String JavaDoc OTHER_CATEGORY_ABBREV = "X";
41
42     protected PrintStream JavaDoc outputStream = System.out;
43     
44     public TextUIBugReporter() {
45         reportStackTrace = true;
46     }
47
48     
49     /**
50      * Set the PrintStream to write bug output to.
51      *
52      * @param outputStream the PrintStream to write bug output to
53      */

54     public void setOutputStream(PrintStream JavaDoc outputStream) {
55         this.outputStream = outputStream;
56     }
57     
58     /**
59      * Set whether or not stack traces should be reported in error output.
60      *
61      * @param reportStackTrace true if stack traces should be reported, false if not
62      */

63     public void setReportStackTrace(boolean reportStackTrace) {
64         this.reportStackTrace = reportStackTrace;
65     }
66
67     /**
68      * Print bug in one-line format.
69      *
70      * @param bugInstance the bug to print
71      */

72     protected void printBug(BugInstance bugInstance) {
73         switch (bugInstance.getPriority()) {
74         case Detector.EXP_PRIORITY:
75             outputStream.print("E ");
76             break;
77         case Detector.LOW_PRIORITY:
78             outputStream.print("L ");
79             break;
80         case Detector.NORMAL_PRIORITY:
81             outputStream.print("M ");
82             break;
83         case Detector.HIGH_PRIORITY:
84             outputStream.print("H ");
85             break;
86         }
87
88         BugPattern pattern = bugInstance.getBugPattern();
89         if (pattern != null) {
90             String JavaDoc categoryAbbrev = null;
91             BugCategory bcat = I18N.instance().getBugCategory(pattern.getCategory());
92             if (bcat != null) categoryAbbrev = bcat.getAbbrev();
93             if (categoryAbbrev == null) categoryAbbrev = OTHER_CATEGORY_ABBREV;
94             outputStream.print(categoryAbbrev);
95             outputStream.print(" ");
96         }
97
98         if (useLongBugCodes) {
99         outputStream.print(bugInstance.getType());
100         outputStream.print(" ");
101         }
102         SourceLineAnnotation line =
103                 bugInstance.getPrimarySourceLineAnnotation();
104         if (line == null)
105             outputStream.println(bugInstance.getMessage());
106         else
107             outputStream.println(bugInstance.getMessage()
108                     + " " + line.toString());
109     }
110
111     private boolean analysisErrors;
112     private boolean missingClasses;
113     
114     //@Override
115
@Override JavaDoc
116     public void reportQueuedErrors() {
117         analysisErrors = missingClasses = false;
118         super.reportQueuedErrors();
119     }
120     
121     @Override JavaDoc
122     public void reportAnalysisError(AnalysisError error) {
123         if (!analysisErrors) {
124             emitLine("The following errors occurred during analysis:");
125             analysisErrors = true;
126         }
127         emitLine("\t" + error.getMessage());
128         if (error.getExceptionMessage() != null) {
129             emitLine("\t\t" + error.getExceptionMessage());
130             if (reportStackTrace) {
131                 String JavaDoc[] stackTrace = error.getStackTrace();
132                 if (stackTrace != null) {
133                     for (String JavaDoc aStackTrace : stackTrace) {
134                         emitLine("\t\t\tAt " + aStackTrace);
135                     }
136                 }
137             }
138         }
139     }
140
141     @Override JavaDoc
142     public void reportMissingClass(String JavaDoc message) {
143         if (!missingClasses) {
144             emitLine("The following classes needed for analysis were missing:");
145             missingClasses = true;
146         }
147         emitLine("\t" + message);
148     }
149     
150     /**
151      * Emit one line of the error message report.
152      * By default, error messages are printed to System.err.
153      * Subclasses may override.
154      *
155      * @param line one line of the error report
156      */

157     protected void emitLine(String JavaDoc line) {
158         line = line.replaceAll("\t", " ");
159         System.err.println(line);
160     }
161
162
163     public boolean getUseLongBugCodes() {
164         return useLongBugCodes;
165     }
166
167
168     public void setUseLongBugCodes(boolean useLongBugCodes) {
169         this.useLongBugCodes = useLongBugCodes;
170     }
171     
172     /* (non-Javadoc)
173      * @see edu.umd.cs.findbugs.BugReporter#getRealBugReporter()
174      */

175     public BugReporter getRealBugReporter() {
176         return this;
177     }
178     
179     /**
180      * For debugging: check a BugInstance to make sure it
181      * is valid.
182      *
183      * @param bugInstance the BugInstance to check
184      */

185     protected void checkBugInstance(BugInstance bugInstance) {
186         for (Iterator JavaDoc<BugAnnotation> i = bugInstance.annotationIterator(); i.hasNext();) {
187             BugAnnotation bugAnnotation = i.next();
188             if (bugAnnotation instanceof PackageMemberAnnotation) {
189                 PackageMemberAnnotation pkgMember = (PackageMemberAnnotation) bugAnnotation;
190                 if (pkgMember.getSourceLines() == null) {
191                     throw new IllegalStateException JavaDoc("Package member " + pkgMember +
192                             " reported without source lines!");
193                 }
194             }
195         }
196     }
197
198 }
199
200 // vim:ts=4
201
Popular Tags