1 19 20 package edu.umd.cs.findbugs; 21 22 import java.io.PrintStream ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 26 37 public abstract class TextUIBugReporter extends AbstractBugReporter { 38 private boolean reportStackTrace; 39 private boolean useLongBugCodes = false; 40 private static final String OTHER_CATEGORY_ABBREV = "X"; 41 42 protected PrintStream outputStream = System.out; 43 44 public TextUIBugReporter() { 45 reportStackTrace = true; 46 } 47 48 49 54 public void setOutputStream(PrintStream outputStream) { 55 this.outputStream = outputStream; 56 } 57 58 63 public void setReportStackTrace(boolean reportStackTrace) { 64 this.reportStackTrace = reportStackTrace; 65 } 66 67 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 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 116 public void reportQueuedErrors() { 117 analysisErrors = missingClasses = false; 118 super.reportQueuedErrors(); 119 } 120 121 @Override 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 [] stackTrace = error.getStackTrace(); 132 if (stackTrace != null) { 133 for (String aStackTrace : stackTrace) { 134 emitLine("\t\t\tAt " + aStackTrace); 135 } 136 } 137 } 138 } 139 } 140 141 @Override 142 public void reportMissingClass(String 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 157 protected void emitLine(String 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 175 public BugReporter getRealBugReporter() { 176 return this; 177 } 178 179 185 protected void checkBugInstance(BugInstance bugInstance) { 186 for (Iterator <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 ("Package member " + pkgMember + 192 " reported without source lines!"); 193 } 194 } 195 } 196 } 197 198 } 199 200 | Popular Tags |