1 19 20 package edu.umd.cs.findbugs; 21 22 import java.util.Iterator ; 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.ba.CFGBuilderException; 29 import edu.umd.cs.findbugs.ba.ClassContext; 30 import edu.umd.cs.findbugs.ba.DataflowAnalysisException; 31 import edu.umd.cs.findbugs.ba.bcp.ByteCodePattern; 32 import edu.umd.cs.findbugs.ba.bcp.ByteCodePatternMatch; 33 import edu.umd.cs.findbugs.ba.bcp.PatternElementMatch; 34 import edu.umd.cs.findbugs.ba.bcp.PatternMatcher; 35 36 44 public abstract class ByteCodePatternDetector implements Detector { 45 private static final boolean DEBUG = SystemProperties.getBoolean("bcpd.debug"); 46 private static final String METHOD = SystemProperties.getProperty("bcpd.method"); 47 48 protected abstract BugReporter getBugReporter(); 49 50 public void visitClassContext(ClassContext classContext) { 51 try { 52 ByteCodePattern pattern = getPattern(); 53 JavaClass jclass = classContext.getJavaClass(); 54 Method[] methodList = jclass.getMethods(); 55 56 for (Method method : methodList) { 57 if (method.isAbstract() || method.isNative()) 58 continue; 59 60 if (METHOD != null && !method.getName().equals(METHOD)) 61 continue; 62 63 if (DEBUG) { 64 System.out.print("=====================================================================\n" + 65 "Method " + jclass.getClassName() + "." + method.getName() + "\n" + 66 "=====================================================================\n"); 67 } 68 69 if (!prescreen(method, classContext)) 70 continue; 71 72 MethodGen methodGen = classContext.getMethodGen(method); 73 if (methodGen == null) 74 continue; 75 76 PatternMatcher matcher = new PatternMatcher(pattern, classContext, method); 77 matcher.execute(); 78 79 Iterator <ByteCodePatternMatch> j = matcher.byteCodePatternMatchIterator(); 80 while (j.hasNext()) { 81 ByteCodePatternMatch match = j.next(); 82 83 if (DEBUG) { 84 System.out.println("Pattern match:"); 85 Iterator <PatternElementMatch> pemIter = match.patternElementMatchIterator(); 86 while (pemIter.hasNext()) { 87 PatternElementMatch pem = pemIter.next(); 88 System.out.println("\t" + pem.toString()); 89 } 90 } 91 92 reportMatch(classContext, method, match); 93 } 94 } 95 } catch (DataflowAnalysisException e) { 96 getBugReporter().logError(getDetectorName() + " caught exception", e); 97 } catch (CFGBuilderException e) { 98 getBugReporter().logError(getDetectorName() + " caught exception", e); 99 } 100 } 101 102 private String getDetectorName() { 103 String className = this.getClass().getName(); 104 int lastDot = className.lastIndexOf('.'); 105 if (lastDot >= 0) { 106 className = className.substring(lastDot + 1); 107 } 108 return className; 109 } 110 111 public void report() { 112 } 113 114 117 public abstract ByteCodePattern getPattern(); 118 119 138 public abstract boolean prescreen(Method method, ClassContext classContext); 139 140 148 public abstract void reportMatch(ClassContext classContext, Method method, ByteCodePatternMatch match) 149 throws CFGBuilderException, DataflowAnalysisException; 150 } 151 152 | Popular Tags |