| 1 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 35 36 public class NoteCheckReturnValue extends AnnotationVisitor 37 implements Detector, NonReportingDetector { 38 39 private static final String LOAD_TRAINING = SystemProperties.getProperty("findbugs.checkreturn.loadtraining"); 41 private static final String 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(); 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  65 public void visitAnnotation(String annotationClass, Map<String , Object > 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 line; 91 while ((line = reader.readLine()) != null) { 92 String [] 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 } 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 } 135 } 136 } 137 } 138 139 } 140 | Popular Tags |