KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > LockChecker


1 /*
2  * Bytecode Analysis Framework
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
20 package edu.umd.cs.findbugs.ba;
21
22 import java.util.BitSet JavaDoc;
23 import java.util.HashMap JavaDoc;
24
25 import org.apache.bcel.Constants;
26 import org.apache.bcel.classfile.Method;
27
28 import edu.umd.cs.findbugs.ba.vna.ValueNumber;
29 import edu.umd.cs.findbugs.ba.vna.ValueNumberDataflow;
30
31 /**
32  * Front-end for LockDataflow that can avoid doing unnecessary work
33  * (e.g., actually performing the lock dataflow)
34  * if the method analyzed does not contain explicit
35  * monitorenter/monitorexit instructions.
36  *
37  * <p>Note that because LockSets use value numbers, ValueNumberAnalysis
38  * must be performed for all methods that are synchronized or contain
39  * explicit monitorenter/monitorexit instructions.</p>
40  *
41  * @see LockSet
42  * @see LockDataflow
43  * @see LockAnalysis
44  * @author David Hovemeyer
45  */

46 public class LockChecker {
47     private ClassContext classContext;
48     private Method method;
49     private LockDataflow lockDataflow;
50     private ValueNumberDataflow vnaDataflow;
51     private HashMap JavaDoc<Location, LockSet> cache;
52     
53     /**
54      * Constructor.
55      *
56      * @param classContext ClassContext for the class
57      * @param method Method we want LockSets for
58      */

59     public LockChecker(ClassContext classContext, Method method) {
60         this.classContext = classContext;
61         this.method = method;
62         this.cache = new HashMap JavaDoc<Location, LockSet>();
63     }
64     
65     /**
66      * Execute dataflow analyses (only if required).
67      *
68      * @throws DataflowAnalysisException
69      * @throws CFGBuilderException
70      */

71     public void execute() throws DataflowAnalysisException, CFGBuilderException {
72         BitSet JavaDoc bytecodeSet = classContext.getBytecodeSet(method);
73         if (bytecodeSet == null) return;
74         if (bytecodeSet.get(Constants.MONITORENTER) || bytecodeSet.get(Constants.MONITOREXIT)) {
75             this.lockDataflow = classContext.getLockDataflow(method);
76         } else if (method.isSynchronized()) {
77             this.vnaDataflow = classContext.getValueNumberDataflow(method); // will need this later
78
}
79     }
80     
81     /**
82      * Get LockSet at given Location.
83      *
84      * @param location the Location
85      * @return the LockSet at that Location
86      * @throws DataflowAnalysisException
87      */

88     public LockSet getFactAtLocation(Location location) throws DataflowAnalysisException {
89         if (lockDataflow != null)
90             return lockDataflow.getFactAtLocation(location);
91         else {
92             LockSet lockSet = cache.get(location);
93             if (lockSet == null) {
94                 lockSet = new LockSet();
95                 lockSet.setDefaultLockCount(0);
96                 if (method.isSynchronized() && !method.isStatic()) {
97                     // LockSet contains just the "this" reference
98
ValueNumber instance = vnaDataflow.getAnalysis().getThisValue();
99                     lockSet.setLockCount(instance.getNumber(), 1);
100                 } else {
101                     // LockSet is completely empty - nothing to do
102
}
103                 cache.put(location, lockSet);
104             }
105             return lockSet;
106         }
107     }
108 }
109
Popular Tags