KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ByteCodePatternDetector


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2003,2004 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;
21
22 import java.util.Iterator JavaDoc;
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 /**
37  * A base class for bug detectors that are based on a ByteCodePattern.
38  * ByteCodePatterns provide an easy way to detect patterns of
39  * bytecode instructions, taking into account control flow and
40  * uses of fields and values.
41  *
42  * @see ByteCodePattern
43  */

44 public abstract class ByteCodePatternDetector implements Detector {
45     private static final boolean DEBUG = SystemProperties.getBoolean("bcpd.debug");
46     private static final String JavaDoc 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 JavaDoc<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 JavaDoc<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 JavaDoc getDetectorName() {
103         String JavaDoc 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     /**
115      * Get the ByteCodePattern for this detector.
116      */

117     public abstract ByteCodePattern getPattern();
118
119     /**
120      * Prescreen a method.
121      * It is a valid, but dumb, implementation simply to return true unconditionally.
122      * A better implementation is to call ClassContext.getBytecodeSet() to check
123      * whether the method actually contains the bytecode instructions that
124      * the pattern will look for. The theory is that checking the bytecode
125      * set is very fast, while building the MethodGen, CFG, ValueNumberAnalysis,
126      * etc. objects required to match ByteCodePatterns is slow, and the bytecode
127      * pattern matching algorithm is also not particularly fast.
128      * <p/>
129      * <p> As a datapoint, prescreening speeds up the BCPDoubleCheck detector
130      * <b>by a factor of 5</b> with no loss of generality and only a dozen
131      * or so extra lines of code.
132      *
133      * @param method the method
134      * @param classContext the ClassContext for the method
135      * @return true if the method should be analyzed for instances of the
136      * ByteCodePattern
137      */

138     public abstract boolean prescreen(Method method, ClassContext classContext);
139
140     /**
141      * Called to report an instance of the ByteCodePattern.
142      *
143      * @param classContext the ClassContext for the analyzed class
144      * @param method the method to instance appears in
145      * @param match the ByteCodePatternMatch object representing the match
146      * of the ByteCodePattern against actual instructions in the method
147      */

148     public abstract void reportMatch(ClassContext classContext, Method method, ByteCodePatternMatch match)
149             throws CFGBuilderException, DataflowAnalysisException;
150 }
151
152 // vim:ts=4
153
Popular Tags