KickJava   Java API By Example, From Geeks To Geeks.

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


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.detect;
21
22 import org.apache.bcel.Constants;
23 import org.apache.bcel.generic.ConstantPoolGen;
24 import org.apache.bcel.generic.INVOKEINTERFACE;
25 import org.apache.bcel.generic.INVOKESPECIAL;
26 import org.apache.bcel.generic.INVOKEVIRTUAL;
27 import org.apache.bcel.generic.Instruction;
28 import org.apache.bcel.generic.InstructionHandle;
29 import org.apache.bcel.generic.InvokeInstruction;
30
31 import edu.umd.cs.findbugs.ResourceCreationPoint;
32 import edu.umd.cs.findbugs.ba.BasicBlock;
33 import edu.umd.cs.findbugs.ba.Hierarchy;
34 import edu.umd.cs.findbugs.ba.Location;
35 import edu.umd.cs.findbugs.ba.RepositoryLookupFailureCallback;
36 import edu.umd.cs.findbugs.ba.ResourceValue;
37 import edu.umd.cs.findbugs.ba.ResourceValueFrame;
38
39 /**
40  * A Stream object marks the location in the code where a
41  * stream is created. It also is responsible for determining
42  * some aspects of how the stream state is tracked
43  * by the ResourceValueAnalysis, such as when the stream
44  * is opened or closed, and whether implicit exception
45  * edges are significant.
46  * <p/>
47  * <p> TODO: change streamClass and streamBase to ObjectType
48  * <p/>
49  * <p> TODO: isStreamOpen() and isStreamClose() should
50  * probably be abstract, so we can customize how they work
51  * for different kinds of streams
52  */

53 public class Stream extends ResourceCreationPoint implements Comparable JavaDoc<Stream> {
54     private String JavaDoc streamBase;
55     private boolean isUninteresting;
56     private boolean isOpenOnCreation;
57     private Location openLocation;
58     private boolean ignoreImplicitExceptions;
59     private String JavaDoc bugType;
60     private int instanceParam;
61     private boolean isClosed;
62
63     @Override JavaDoc
64     public String JavaDoc toString() {
65         return streamBase +":" + openLocation;
66     }
67     /**
68      * Constructor.
69      * By default, Stream objects are marked as uninteresting.
70      * setInteresting("BUG_TYPE") must be called explicitly to mark
71      * the Stream as interesting.
72      *
73      * @param location where the stream is created
74      * @param streamClass type of Stream
75      * @param streamBase highest class in the class hierarchy through which
76      * stream's close() method could be called
77      */

78     public Stream(Location location, String JavaDoc streamClass, String JavaDoc streamBase) {
79         super(location, streamClass);
80         this.streamBase = streamBase;
81         isUninteresting = true;
82         instanceParam = -1;
83     }
84
85     /**
86      * Mark this Stream as interesting.
87      *
88      * @param bugType the bug type that should be reported if
89      * the stream is not closed on all paths out of the method
90      */

91     public Stream setInteresting(String JavaDoc bugType) {
92         this.isUninteresting = false;
93         this.bugType = bugType;
94         return this;
95     }
96
97     /**
98      * Mark whether or not implicit exception edges should be
99      * ignored by ResourceValueAnalysis when determining whether or
100      * not stream is closed on all paths out of method.
101      */

102     public Stream setIgnoreImplicitExceptions(boolean enable) {
103         ignoreImplicitExceptions = enable;
104         return this;
105     }
106
107     /**
108      * Mark whether or not Stream is open as soon as it is created,
109      * or whether a later method or constructor must explicitly
110      * open it.
111      */

112     public Stream setIsOpenOnCreation(boolean enable) {
113         isOpenOnCreation = enable;
114         return this;
115     }
116
117     /**
118      * Set the number of the parameter which passes the
119      * stream instance.
120      *
121      * @param instanceParam number of the parameter passing the stream instance
122      */

123     public void setInstanceParam(int instanceParam) {
124         this.instanceParam = instanceParam;
125     }
126
127     /**
128      * Set this Stream has having been closed on all
129      * paths out of the method.
130      */

131     public void setClosed() {
132         isClosed = true;
133     }
134
135     public String JavaDoc getStreamBase() {
136         return streamBase;
137     }
138
139     public boolean isUninteresting() {
140         return isUninteresting;
141     }
142
143     public boolean isOpenOnCreation() {
144         return isOpenOnCreation;
145     }
146
147     public void setOpenLocation(Location openLocation) {
148         this.openLocation = openLocation;
149     }
150
151     public Location getOpenLocation() {
152         return openLocation;
153     }
154
155     public boolean ignoreImplicitExceptions() {
156         return ignoreImplicitExceptions;
157     }
158
159     public int getInstanceParam() {
160         return instanceParam;
161     }
162
163     public String JavaDoc getBugType() {
164         return bugType;
165     }
166
167     /**
168      * Return whether or not the Stream is closed on all paths
169      * out of the method.
170      */

171     public boolean isClosed() {
172         return isClosed;
173     }
174
175     public boolean isStreamOpen(BasicBlock basicBlock, InstructionHandle handle,
176                                 ConstantPoolGen cpg, ResourceValueFrame frame) {
177         if (isOpenOnCreation)
178             return false;
179
180         Instruction ins = handle.getInstruction();
181         if (!(ins instanceof INVOKESPECIAL))
182             return false;
183
184         // Does this instruction open the stream?
185
INVOKESPECIAL inv = (INVOKESPECIAL) ins;
186
187         return frame.isValid()
188                 && getInstanceValue(frame, inv, cpg).isInstance()
189                 && matchMethod(inv, cpg, this.getResourceClass(), "<init>");
190     }
191
192     public boolean isStreamClose(BasicBlock basicBlock, InstructionHandle handle,
193                                  ConstantPoolGen cpg, ResourceValueFrame frame,
194                                  RepositoryLookupFailureCallback lookupFailureCallback) {
195
196         Instruction ins = handle.getInstruction();
197
198         if ((ins instanceof INVOKEVIRTUAL) || (ins instanceof INVOKEINTERFACE)) {
199             // Does this instruction close the stream?
200
InvokeInstruction inv = (InvokeInstruction) ins;
201
202             if (!frame.isValid() ||
203                     !getInstanceValue(frame, inv, cpg).isInstance())
204                 return false;
205
206             // It's a close if the invoked class is any subtype of the stream base class.
207
// (Basically, we may not see the exact original stream class,
208
// even though it's the same instance.)
209
try {
210                 return inv.getName(cpg).equals("close")
211                         && inv.getSignature(cpg).equals("()V")
212                         && Hierarchy.isSubtype(inv.getClassName(cpg), streamBase);
213             } catch (ClassNotFoundException JavaDoc e) {
214                 lookupFailureCallback.reportMissingClass(e);
215                 return false;
216             }
217         }
218
219         return false;
220     }
221
222     private ResourceValue getInstanceValue(ResourceValueFrame frame, InvokeInstruction inv,
223                                            ConstantPoolGen cpg) {
224         int numConsumed = inv.consumeStack(cpg);
225         if (numConsumed == Constants.UNPREDICTABLE)
226             throw new IllegalStateException JavaDoc();
227         return frame.getValue(frame.getNumSlots() - numConsumed);
228     }
229
230     private boolean matchMethod(InvokeInstruction inv, ConstantPoolGen cpg, String JavaDoc className,
231                                 String JavaDoc methodName) {
232         return inv.getClassName(cpg).equals(className)
233                 && inv.getName(cpg).equals(methodName);
234     }
235
236     public int hashCode() {
237         return
238         getLocation().hashCode()
239         + 3*streamBase.hashCode()
240         + 7*getResourceClass().hashCode()
241         + 11*instanceParam;
242     }
243     public boolean equals(Object JavaDoc o) {
244         if (!(o instanceof Stream)) return false;
245         Stream other = (Stream) o;
246         if (!getLocation().equals(other.getLocation())) return false;
247         if (!streamBase.equals(other.streamBase)) return false;
248         if (!getResourceClass().equals(other.getResourceClass())) return false;
249         if (instanceParam != other.instanceParam) return false;
250         return true;
251     }
252     public int compareTo(Stream other) {
253         int cmp;
254
255         // The main idea in comparing streams is that
256
// if they can't be differentiated by location
257
// and base/stream class, then we should try
258
// instanceParam. This allows streams passed in
259
// different parameters to be distinguished.
260

261         cmp = getLocation().compareTo(other.getLocation());
262         if (cmp != 0) return cmp;
263         cmp = streamBase.compareTo(other.streamBase);
264         if (cmp != 0) return cmp;
265         cmp = getResourceClass().compareTo(other.getResourceClass());
266         if (cmp != 0) return cmp;
267         cmp = instanceParam - other.instanceParam;
268         if (cmp != 0) return cmp;
269
270         return 0;
271     }
272 }
273
274 // vim:ts=3
275
Popular Tags