KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > detect > FindSleepWithLockHeld


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2005, 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 package edu.umd.cs.findbugs.detect;
20
21 import java.util.BitSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import org.apache.bcel.Constants;
25 import org.apache.bcel.classfile.JavaClass;
26 import org.apache.bcel.classfile.Method;
27 import org.apache.bcel.generic.ConstantPoolGen;
28 import org.apache.bcel.generic.INVOKESTATIC;
29 import org.apache.bcel.generic.Instruction;
30 import org.apache.bcel.generic.MethodGen;
31
32 import edu.umd.cs.findbugs.BugInstance;
33 import edu.umd.cs.findbugs.BugReporter;
34 import edu.umd.cs.findbugs.Detector;
35 import edu.umd.cs.findbugs.ba.CFG;
36 import edu.umd.cs.findbugs.ba.CFGBuilderException;
37 import edu.umd.cs.findbugs.ba.ClassContext;
38 import edu.umd.cs.findbugs.ba.DataflowAnalysisException;
39 import edu.umd.cs.findbugs.ba.Location;
40 import edu.umd.cs.findbugs.ba.LockDataflow;
41 import edu.umd.cs.findbugs.ba.LockSet;
42
43 /**
44  * Find calls to Thread.sleep() made with a lock held.
45  *
46  * @author David Hovemeyer
47  */

48 public class FindSleepWithLockHeld implements Detector {
49     
50     private BugReporter bugReporter;
51     
52     public FindSleepWithLockHeld(BugReporter bugReporter) {
53         this.bugReporter = bugReporter;
54     }
55
56     public void visitClassContext(ClassContext classContext) {
57         JavaClass javaClass = classContext.getJavaClass();
58         
59         Method[] methodList = javaClass.getMethods();
60         for (Method method : methodList) {
61             if (method.getCode() == null)
62                 continue;
63
64             if (!prescreen(classContext, method))
65                 continue;
66
67             try {
68                 analyzeMethod(classContext, method);
69             } catch (CFGBuilderException e) {
70                 bugReporter.logError("FindSleepWithLockHeld caught exception", e);
71             } catch (DataflowAnalysisException e) {
72                 bugReporter.logError("FindSleepWithLockHeld caught exception", e);
73             }
74         }
75     }
76
77     private boolean prescreen(ClassContext classContext, Method method) {
78         BitSet JavaDoc bytecodeSet = classContext.getBytecodeSet(method);
79         if (bytecodeSet == null) return false;
80         // method must acquire a lock
81
if (!bytecodeSet.get(Constants.MONITORENTER) && !method.isSynchronized())
82             return false;
83         
84         // and contain a static method invocation
85
if (!bytecodeSet.get(Constants.INVOKESTATIC))
86             return false;
87         
88         return true;
89     }
90
91     private void analyzeMethod(ClassContext classContext, Method method) throws CFGBuilderException, DataflowAnalysisException {
92 // System.out.println("Checking " + method);
93

94         CFG cfg = classContext.getCFG(method);
95         LockDataflow lockDataflow = classContext.getLockDataflow(method);
96
97         for (Iterator JavaDoc<Location> i = cfg.locationIterator(); i.hasNext();) {
98             Location location = i.next();
99             Instruction ins = location.getHandle().getInstruction();
100
101             if (!(ins instanceof INVOKESTATIC))
102                 continue;
103             
104             if (!isSleep((INVOKESTATIC) ins, classContext.getConstantPoolGen()))
105                 continue;
106             
107 // System.out.println("Found sleep at " + location.getHandle());
108

109             LockSet lockSet = lockDataflow.getFactAtLocation(location);
110             if (lockSet.getNumLockedObjects() > 0) {
111                 MethodGen mg = classContext.getMethodGen(method);
112                 String JavaDoc sourceFile = classContext.getJavaClass().getSourceFileName();
113                 bugReporter.reportBug(new BugInstance("SWL_SLEEP_WITH_LOCK_HELD", NORMAL_PRIORITY)
114                         .addClassAndMethod(mg, sourceFile)
115                         .addSourceLine(classContext, mg, sourceFile, location.getHandle()));
116             }
117         }
118     }
119
120     private boolean isSleep(INVOKESTATIC ins, ConstantPoolGen cpg) {
121         String JavaDoc className = ins.getClassName(cpg);
122         if (!className.equals("java.lang.Thread"))
123             return false;
124         String JavaDoc methodName = ins.getMethodName(cpg);
125         String JavaDoc signature = ins.getSignature(cpg);
126         
127         return methodName.equals("sleep")
128                 && (signature.equals("(J)V") || signature.equals("(JI)V"));
129     }
130
131     public void report() {
132     }
133
134 }
135
Popular Tags