KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2003-2005 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.detect;
21
22
23 import edu.umd.cs.findbugs.*;
24 import edu.umd.cs.findbugs.ba.*;
25 import edu.umd.cs.findbugs.ba.bcp.Invoke;
26 import edu.umd.cs.findbugs.visitclass.AnnotationVisitor;
27 import java.io.*;
28 import java.util.*;
29
30 import org.apache.bcel.classfile.JavaClass;
31
32 /**
33  * @author William Pugh
34  */

35  
36 public class NoteCheckReturnValue extends AnnotationVisitor
37   implements Detector, NonReportingDetector {
38     
39     // XXX: Hack, for now
40
private static final String JavaDoc LOAD_TRAINING = SystemProperties.getProperty("findbugs.checkreturn.loadtraining");
41     private static final String JavaDoc SAVE_TRAINING = SystemProperties.getProperty("findbugs.checkreturn.savetraining");
42
43     private BugReporter bugReporter;
44     private boolean checkLoad;
45     private Set<XMethod> checkReturnValueDatabase;
46
47     public NoteCheckReturnValue(BugReporter bugReporter) {
48         AnalysisContext.currentAnalysisContext().getCheckReturnAnnotationDatabase(); // force creation
49
this.bugReporter = bugReporter;
50         if (SAVE_TRAINING != null) {
51             checkReturnValueDatabase = new HashSet<XMethod>();
52         }
53     }
54
55     public void visitClassContext(ClassContext classContext) {
56         if (LOAD_TRAINING != null && !checkLoad) {
57             loadTraining();
58             checkLoad = true;
59         }
60         JavaClass javaClass = classContext.getJavaClass();
61         if (!FindUnreleasedLock.preTiger(javaClass)) javaClass.accept(this);
62     }
63
64         @Override JavaDoc
65                  public void visitAnnotation(String JavaDoc annotationClass, Map<String JavaDoc, Object JavaDoc> map,
66  boolean runtimeVisible) {
67         if (!annotationClass.endsWith("CheckReturnValue")) return;
68         if (!visitingMethod()) return;
69         BCPMethodReturnCheck.addMethodWhoseReturnMustBeChecked(
70             "+" + getDottedClassName(),
71             getMethodName(),
72             getMethodSig(),
73             getThisClass().isStatic() ? Invoke.STATIC : Invoke.ANY);
74         
75         if (SAVE_TRAINING != null) {
76             checkReturnValueDatabase.add(XFactory.createXMethod(this));
77         }
78         }
79
80     public void report() {
81         if (SAVE_TRAINING != null) {
82             saveTraining();
83         }
84         }
85
86     private void loadTraining() {
87         BufferedReader reader = null;
88         try {
89             reader = new BufferedReader(new FileReader(LOAD_TRAINING));
90             String JavaDoc line;
91             while ((line = reader.readLine()) != null) {
92                 String JavaDoc[] tuple = line.split(",");
93                 if (tuple.length != 4)
94                     continue;
95                 BCPMethodReturnCheck.addMethodWhoseReturnMustBeChecked(
96                         tuple[0], tuple[1], tuple[2],
97                         Boolean.valueOf(tuple[3]).booleanValue() ? Invoke.STATIC : Invoke.ANY);
98             }
99         } catch (IOException e) {
100             bugReporter.logError("Couldn't load check return database");
101         } finally {
102             if (reader != null) {
103                 try {
104                     reader.close();
105                 } catch (IOException e) {
106                     // Ignore
107
}
108             }
109         }
110     }
111
112     private void saveTraining() {
113         BufferedWriter writer = null;
114         try {
115             writer = new BufferedWriter(new FileWriter(SAVE_TRAINING));
116             for (XMethod xmethod : checkReturnValueDatabase) {
117                 writer.write(xmethod.getClassName());
118                 writer.write(",");
119                 writer.write(xmethod.getName());
120                 writer.write(",");
121                 writer.write(xmethod.getSignature());
122                 writer.write(",");
123                 writer.write(String.valueOf(xmethod.getAccessFlags()));
124                 writer.write("\n");
125             }
126         } catch (IOException e) {
127             bugReporter.logError("Couldn't write check return value training data", e);
128         } finally {
129             if (writer != null) {
130                 try {
131                     writer.close();
132                 } catch (IOException e) {
133                     // ignore
134
}
135             }
136         }
137     }
138
139 }
140
Popular Tags