| 1 19 20 package edu.umd.cs.findbugs; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 27 import org.apache.bcel.classfile.JavaClass; 28 29 import edu.umd.cs.findbugs.ba.AnalysisContext; 30 import edu.umd.cs.findbugs.ba.SourceFinder; 31 import edu.umd.cs.findbugs.classfile.ClassDescriptor; 32 33 38 public class EmacsBugReporter extends TextUIBugReporter { 39 40 private HashSet <BugInstance> seenAlready = new HashSet <BugInstance>(); 41 42 private HashMap <String ,String > sourceFileNameCache = new HashMap <String ,String >(); 43 44 public void observeClass(ClassDescriptor classDescriptor) { 45 try { 46 JavaClass javaClass = AnalysisContext.currentAnalysisContext().lookupClass( 47 classDescriptor.toDottedClassName()); 48 String sourceFileName = fileNameFor(javaClass.getPackageName(), javaClass.getSourceFileName()); 49 sourceFileNameCache.put(javaClass.getClassName(), sourceFileName); 50 } catch (ClassNotFoundException e) { 51 } 53 } 54 55 private String fileNameFor(final String packageName, final String sourceName) { 56 String result; 57 SourceFinder sourceFinder = AnalysisContext.currentAnalysisContext().getSourceFinder(); 58 try { 59 result = sourceFinder.findSourceFile(packageName, sourceName).getFullFileName(); 60 } catch (IOException e) { 61 result = packageName.replace('.', File.separatorChar) + File.separatorChar + sourceName; 62 } 63 return result; 64 } 65 66 @Override  67 protected void printBug(BugInstance bugInstance) { 68 int lineStart = 0; 69 int lineEnd = 0; 70 String fullPath = "???"; 71 72 SourceLineAnnotation line = bugInstance.getPrimarySourceLineAnnotation(); 73 if (line == null) { 74 ClassAnnotation classInfo = bugInstance.getPrimaryClass(); 75 if (classInfo != null) { 76 fullPath = sourceFileNameCache.get(classInfo.getClassName()); 77 } 78 } else { 79 lineStart = line.getStartLine(); 80 lineEnd = line.getEndLine(); 81 SourceFinder sourceFinder = AnalysisContext.currentAnalysisContext().getSourceFinder(); 82 String pkgName = line.getPackageName(); 83 try { 84 fullPath = sourceFinder.findSourceFile(pkgName, line.getSourceFile()).getFullFileName(); 85 } catch (IOException e) { 86 if (pkgName.equals("")) 87 fullPath = line.getSourceFile(); 88 else 89 fullPath = pkgName.replace('.', '/') + "/" + line.getSourceFile(); 90 } 91 } 92 outputStream.print(fullPath + ":" 93 + lineStart + ":" 94 + lineEnd + " " 95 + bugInstance.getMessage()); 96 97 switch (bugInstance.getPriority()) { 98 case Detector.EXP_PRIORITY: 99 outputStream.print(" (E) "); 100 break; 101 case Detector.LOW_PRIORITY: 102 outputStream.print(" (L) "); 103 break; 104 case Detector.NORMAL_PRIORITY: 105 outputStream.print(" (M) "); 106 break; 107 case Detector.HIGH_PRIORITY: 108 outputStream.print(" (H) "); 109 break; 110 } 111 112 outputStream.println(); 113 } 114 115 @Override  116 protected void doReportBug(BugInstance bugInstance) { 117 if (seenAlready.add(bugInstance)) { 118 printBug(bugInstance); 119 notifyObservers(bugInstance); 120 } 121 } 122 123 public void finish() { 124 outputStream.close(); 125 } 126 127 130 @Override  131 public BugReporter getRealBugReporter() { 132 return this; 133 } 134 } 135 136 141 | Popular Tags |