KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.HashSet JavaDoc;
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 /**
34  * BugReporter to output warnings in Emacs format.
35  *
36  * @author David Li
37  */

38 public class EmacsBugReporter extends TextUIBugReporter {
39
40     private HashSet JavaDoc<BugInstance> seenAlready = new HashSet JavaDoc<BugInstance>();
41
42     private HashMap JavaDoc<String JavaDoc,String JavaDoc> sourceFileNameCache = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
43
44     public void observeClass(ClassDescriptor classDescriptor) {
45         try {
46             JavaClass javaClass = AnalysisContext.currentAnalysisContext().lookupClass(
47                     classDescriptor.toDottedClassName());
48             String JavaDoc sourceFileName = fileNameFor(javaClass.getPackageName(), javaClass.getSourceFileName());
49             sourceFileNameCache.put(javaClass.getClassName(), sourceFileName);
50         } catch (ClassNotFoundException JavaDoc e) {
51             // Ignore - should not happen
52
}
53     }
54     
55     private String JavaDoc fileNameFor(final String JavaDoc packageName, final String JavaDoc sourceName) {
56         String JavaDoc result;
57         SourceFinder sourceFinder = AnalysisContext.currentAnalysisContext().getSourceFinder();
58         try {
59             result = sourceFinder.findSourceFile(packageName, sourceName).getFullFileName();
60         } catch (IOException JavaDoc e) {
61             result = packageName.replace('.', File.separatorChar) + File.separatorChar + sourceName;
62         }
63         return result;
64     }
65
66     @Override JavaDoc
67     protected void printBug(BugInstance bugInstance) {
68         int lineStart = 0;
69         int lineEnd = 0;
70         String JavaDoc 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 JavaDoc pkgName = line.getPackageName();
83             try {
84                 fullPath = sourceFinder.findSourceFile(pkgName, line.getSourceFile()).getFullFileName();
85             } catch (IOException JavaDoc 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 JavaDoc
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     /* (non-Javadoc)
128      * @see edu.umd.cs.findbugs.BugReporter#getRealBugReporter()
129      */

130     @Override JavaDoc
131     public BugReporter getRealBugReporter() {
132         return this;
133     }
134 }
135
136 /*
137  * Local Variables:
138  * eval: (c-set-style "bsd")
139  * End:
140  */

141
Popular Tags