1 19 20 package edu.umd.cs.findbugs.ba; 21 22 import java.io.IOException ; 23 24 import org.apache.bcel.classfile.JavaClass; 25 import org.apache.bcel.classfile.Method; 26 import org.apache.bcel.generic.MethodGen; 27 28 import edu.umd.cs.findbugs.FindBugsAnalysisFeatures; 29 import edu.umd.cs.findbugs.SystemProperties; 30 31 41 public abstract class DataflowTestDriver <Fact, AnalysisType extends BasicAbstractDataflowAnalysis<Fact>> { 42 private boolean overrideIsForwards; 43 44 public void overrideIsForwards() { 45 this.overrideIsForwards = true; 46 } 47 48 53 public void execute(String filename) throws DataflowAnalysisException, CFGBuilderException, IOException { 54 JavaClass jclass = new RepositoryClassParser(filename).parse(); 55 56 final RepositoryLookupFailureCallback lookupFailureCallback = new DebugRepositoryLookupFailureCallback(); 57 58 AnalysisContext analysisContext = AnalysisContext.create(lookupFailureCallback); 59 analysisContext.setBoolProperty(AnalysisFeatures.ACCURATE_EXCEPTIONS, true); 60 61 configureAnalysisContext(analysisContext); 62 63 ClassContext classContext = analysisContext.getClassContext(jclass); 64 String methodName = SystemProperties.getProperty("dataflow.method"); 65 66 Method[] methods = jclass.getMethods(); 67 for (Method method : methods) { 68 if (methodName != null && !method.getName().equals(methodName)) 69 continue; 70 71 MethodGen methodGen = classContext.getMethodGen(method); 72 if (methodGen == null) 73 continue; 74 75 System.out.println("-----------------------------------------------------------------"); 76 System.out.println("Method: " + SignatureConverter.convertMethodSignature(methodGen)); 77 System.out.println("-----------------------------------------------------------------"); 78 79 execute(classContext, method); 80 } 81 } 82 83 static class Knob { 84 String systemPropertyName; 85 int analysisProperty; 86 87 Knob(String systemPropertyName, int analysisProperty) { 88 this.systemPropertyName = systemPropertyName; 89 this.analysisProperty = analysisProperty; 90 } 91 } 92 93 private static final Knob[] KNOB_LIST = { 94 new Knob("ta.instanceof", AnalysisFeatures.MODEL_INSTANCEOF), 95 new Knob("inva.trackvalues", AnalysisFeatures.TRACK_VALUE_NUMBERS_IN_NULL_POINTER_ANALYSIS), 96 new Knob("fnd.derefs", AnalysisFeatures.TRACK_GUARANTEED_VALUE_DEREFS_IN_NULL_POINTER_ANALYSIS), 97 }; 98 99 104 private void configureAnalysisContext(AnalysisContext analysisContext) { 105 boolean max = SystemProperties.getBoolean("dataflow.max"); 106 107 for (Knob knob : KNOB_LIST) { 108 boolean enable = max || SystemProperties.getBoolean(knob.systemPropertyName); 109 System.out.println("Setting " + knob.systemPropertyName + "=" + enable); 110 analysisContext.setBoolProperty(knob.analysisProperty, enable); 111 } 112 } 113 114 117 public void execute(ClassContext classContext, Method method) throws DataflowAnalysisException, CFGBuilderException { 118 119 Dataflow<Fact, AnalysisType> dataflow = createDataflow(classContext, method); 120 System.out.println("Finished in " + dataflow.getNumIterations() + " iterations"); 121 122 CFG cfg = classContext.getCFG(method); 123 examineResults(cfg, dataflow); 124 125 if (SystemProperties.getBoolean("dataflow.printcfg")) { 126 CFGPrinter p = new DataflowCFGPrinter<Fact, AnalysisType>(dataflow); 127 if (overrideIsForwards) { 128 p.setIsForwards(!p.isForwards()); 129 } 130 p.print(System.out); 131 } 132 } 133 134 142 public abstract Dataflow<Fact, AnalysisType> createDataflow(ClassContext classContext, Method method) 143 throws CFGBuilderException, DataflowAnalysisException; 144 145 152 public void examineResults(CFG cfg, Dataflow<Fact, AnalysisType> dataflow) { 153 } 154 } 155 156 | Popular Tags |