1 19 20 package edu.umd.cs.findbugs.detect; 21 22 import java.util.Iterator ; 23 import java.util.Set ; 24 25 import org.apache.bcel.classfile.Method; 26 import org.apache.bcel.generic.Instruction; 27 import org.apache.bcel.generic.InvokeInstruction; 28 29 import edu.umd.cs.findbugs.BugReporter; 30 import edu.umd.cs.findbugs.Detector; 31 import edu.umd.cs.findbugs.SystemProperties; 32 import edu.umd.cs.findbugs.ba.CFG; 33 import edu.umd.cs.findbugs.ba.CFGBuilderException; 34 import edu.umd.cs.findbugs.ba.ClassContext; 35 import edu.umd.cs.findbugs.ba.DataflowAnalysisException; 36 import edu.umd.cs.findbugs.ba.Hierarchy; 37 import edu.umd.cs.findbugs.ba.JavaClassAndMethod; 38 import edu.umd.cs.findbugs.ba.Location; 39 import edu.umd.cs.findbugs.ba.SignatureConverter; 40 41 46 public class CheckCalls implements Detector { 47 48 private static final String METHOD = SystemProperties.getProperty("checkcalls.method"); 49 private static final String TARGET_METHOD = SystemProperties.getProperty("checkcalls.targetmethod"); 50 51 BugReporter bugReporter; 52 53 public CheckCalls(BugReporter bugReporter) { 54 this.bugReporter = bugReporter; 55 } 56 57 60 public void visitClassContext(ClassContext classContext) { 61 Method[] methodList = classContext.getJavaClass().getMethods(); 62 for (Method method : methodList) { 63 if (method.getCode() == null) 64 continue; 65 66 if (METHOD != null && !method.getName().equals(METHOD)) 68 continue; 69 70 try { 71 System.out.println("Analyzing " + 72 SignatureConverter.convertMethodSignature(classContext.getJavaClass(), method) 73 ); 74 analyzeMethod(classContext, method); 75 } catch (CFGBuilderException e) { 76 bugReporter.logError("Error", e); 77 } catch (DataflowAnalysisException e) { 78 bugReporter.logError("Error", e); 79 } catch (ClassNotFoundException e) { 80 bugReporter.reportMissingClass(e); 81 } 82 } 83 } 84 85 private void analyzeMethod(ClassContext classContext, Method method) 86 throws CFGBuilderException, ClassNotFoundException , DataflowAnalysisException { 87 CFG cfg = classContext.getCFG(method); 88 for (Iterator <Location> i = cfg.locationIterator(); i.hasNext();) { 89 Location location = i.next(); 90 Instruction ins = location.getHandle().getInstruction(); 91 92 if (ins instanceof InvokeInstruction) { 93 if (TARGET_METHOD != null 94 && !((InvokeInstruction)ins).getMethodName(classContext.getConstantPoolGen()).equals(TARGET_METHOD)) 95 continue; 96 97 System.out.println("\n*******************************************************\n"); 98 99 System.out.println("Method invocation: " + location.getHandle()); 100 System.out.println("\tInvoking: " + 101 SignatureConverter.convertMethodSignature((InvokeInstruction)ins,classContext.getConstantPoolGen())); 102 103 JavaClassAndMethod proto = Hierarchy.findInvocationLeastUpperBound( 104 (InvokeInstruction) ins, 105 classContext.getConstantPoolGen()); 106 if (proto == null) { 107 System.out.println("\tUnknown prototype method"); 108 } else { 109 System.out.println("\tPrototype method: class=" + 110 proto.getJavaClass().getClassName() + ", method=" + 111 proto.getMethod()); 112 } 113 Set <JavaClassAndMethod> calledMethodSet = Hierarchy.resolveMethodCallTargets( 114 (InvokeInstruction) ins, 115 classContext.getTypeDataflow(method).getFactAtLocation(location), 116 classContext.getConstantPoolGen() 117 ); 118 System.out.println("\tTarget method set: " + calledMethodSet); 119 } 120 } 121 } 122 123 126 public void report() { 127 } 128 129 } 130 | Popular Tags |