edu.umd.cs.findbugs.detect
Class StreamResourceTracker

java.lang.Object
  extended by edu.umd.cs.findbugs.detect.StreamResourceTracker
All Implemented Interfaces:
ResourceTracker<Stream>

public class StreamResourceTracker
extends java.lang.Object
implements ResourceTracker<Stream>

Resource tracker which determines where streams are created, and how they are used within the method.

Author:
David Hovemeyer

Field Summary
private  RepositoryLookupFailureCallback lookupFailureCallback
           
private  ResourceCollection<Stream> resourceCollection
           
private  java.util.Map<Stream,StreamEquivalenceClass> streamEquivalenceMap
          Map of individual streams to equivalence classes.
private  java.util.TreeSet<StreamEscape> streamEscapeSet
          Set of all (potential) stream escapes.
private  StreamFactory[] streamFactoryList
           
private  java.util.Map<Location,Stream> streamOpenLocationMap
          Map of locations where streams are opened to the actual Stream objects.
private  java.util.HashSet<Stream> uninterestingStreamEscapeSet
          Set of all open locations and escapes of uninteresting streams.
 
Constructor Summary
StreamResourceTracker(StreamFactory[] streamFactoryList, RepositoryLookupFailureCallback lookupFailureCallback)
          Constructor.
 
Method Summary
 void addStreamEscape(Stream source, Location target)
          Indicate that a stream escapes at the given target Location.
 void addStreamOpenLocation(Location streamOpenLocation, Stream stream)
          Indicate that a stream is constructed at this Location.
 ResourceValueFrameModelingVisitor createVisitor(Stream resource, org.apache.bcel.generic.ConstantPoolGen cpg)
          Create a ResourceValueFrameModelingVisitor to model the effect of instructions on the state of the resource.
 StreamEquivalenceClass getStreamEquivalenceClass(Stream stream)
          Get the equivalence class for given stream.
 boolean ignoreImplicitExceptions(Stream resource)
          Determine whether the analysis should ignore exception edges on which only implicit exceptions are propagated.
 boolean isParamInstance(Stream resource, int slot)
          Return if the given parameter slot contains the resource instance upon entry to the method.
 boolean isResourceClose(BasicBlock basicBlock, org.apache.bcel.generic.InstructionHandle handle, org.apache.bcel.generic.ConstantPoolGen cpg, Stream resource, ResourceValueFrame frame)
          Determine if the given instruction is the site where a resource is closed.
 Stream isResourceCreation(BasicBlock basicBlock, org.apache.bcel.generic.InstructionHandle handle, org.apache.bcel.generic.ConstantPoolGen cpg)
          Determine if the given instruction is the site where a resource is created.
 boolean isResourceOpen(BasicBlock basicBlock, org.apache.bcel.generic.InstructionHandle handle, org.apache.bcel.generic.ConstantPoolGen cpg, Stream resource, ResourceValueFrame frame)
           
private  boolean isStreamOpenLocation(Location location)
          Determine if given Location is a stream open location point.
 boolean isUninterestingStreamEscape(Stream stream)
          Determine if an uninteresting stream escapes at given location.
 void markTransitiveUninterestingStreamEscapes()
          Transitively mark all streams into which uninteresting streams (such as System.out) escape.
 void setResourceCollection(ResourceCollection<Stream> resourceCollection)
          Set the precomputed ResourceCollection for the method.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

streamFactoryList

private StreamFactory[] streamFactoryList

lookupFailureCallback

private RepositoryLookupFailureCallback lookupFailureCallback

resourceCollection

private ResourceCollection<Stream> resourceCollection

streamOpenLocationMap

private java.util.Map<Location,Stream> streamOpenLocationMap
Map of locations where streams are opened to the actual Stream objects.


uninterestingStreamEscapeSet

private java.util.HashSet<Stream> uninterestingStreamEscapeSet
Set of all open locations and escapes of uninteresting streams.


streamEscapeSet

private java.util.TreeSet<StreamEscape> streamEscapeSet
Set of all (potential) stream escapes.


streamEquivalenceMap

private java.util.Map<Stream,StreamEquivalenceClass> streamEquivalenceMap
Map of individual streams to equivalence classes. Any time a stream "A" is wrapped with a stream "B", "A" and "B" belong to the same equivalence class. If any stream in an equivalence class is closed, then we consider all of the streams in the equivalence class as having been closed.

Constructor Detail

StreamResourceTracker

public StreamResourceTracker(StreamFactory[] streamFactoryList,
                             RepositoryLookupFailureCallback lookupFailureCallback)
Constructor.

Parameters:
streamFactoryList - array of StreamFactory objects which determine where streams are created
lookupFailureCallback - used when class hierarchy lookups fail
Method Detail

setResourceCollection

public void setResourceCollection(ResourceCollection<Stream> resourceCollection)
Set the precomputed ResourceCollection for the method.


addStreamEscape

public void addStreamEscape(Stream source,
                            Location target)
Indicate that a stream escapes at the given target Location.

Parameters:
source - the Stream that is escaping
target - the target Location (where the stream escapes)

markTransitiveUninterestingStreamEscapes

public void markTransitiveUninterestingStreamEscapes()
Transitively mark all streams into which uninteresting streams (such as System.out) escape. This handles the rule that wrapping an uninteresting stream makes the wrapper uninteresting as well.


isUninterestingStreamEscape

public boolean isUninterestingStreamEscape(Stream stream)
Determine if an uninteresting stream escapes at given location. markTransitiveUninterestingStreamEscapes() should be called first.

Parameters:
stream - the stream
Returns:
true if an uninteresting stream escapes at the location

addStreamOpenLocation

public void addStreamOpenLocation(Location streamOpenLocation,
                                  Stream stream)
Indicate that a stream is constructed at this Location.

Parameters:
streamOpenLocation - the Location
stream - the Stream opened at this Location

getStreamEquivalenceClass

public StreamEquivalenceClass getStreamEquivalenceClass(Stream stream)
Get the equivalence class for given stream. May only be called if markTransitiveUninterestingStreamEscapes() has been called.

Parameters:
stream - the stream
Returns:
the set containing the equivalence class for the given stream

isStreamOpenLocation

private boolean isStreamOpenLocation(Location location)
Determine if given Location is a stream open location point.

Parameters:
location - the Location

isResourceCreation

public Stream isResourceCreation(BasicBlock basicBlock,
                                 org.apache.bcel.generic.InstructionHandle handle,
                                 org.apache.bcel.generic.ConstantPoolGen cpg)
Description copied from interface: ResourceTracker
Determine if the given instruction is the site where a resource is created.

Specified by:
isResourceCreation in interface ResourceTracker<Stream>
Parameters:
basicBlock - basic block containing the instruction
handle - the instruction
cpg - the ConstantPoolGen for the method
Returns:
an opaque Resource object if it is a creation site, or null if it is not a creation site

isResourceOpen

public boolean isResourceOpen(BasicBlock basicBlock,
                              org.apache.bcel.generic.InstructionHandle handle,
                              org.apache.bcel.generic.ConstantPoolGen cpg,
                              Stream resource,
                              ResourceValueFrame frame)

isResourceClose

public boolean isResourceClose(BasicBlock basicBlock,
                               org.apache.bcel.generic.InstructionHandle handle,
                               org.apache.bcel.generic.ConstantPoolGen cpg,
                               Stream resource,
                               ResourceValueFrame frame)
Description copied from interface: ResourceTracker
Determine if the given instruction is the site where a resource is closed.

Specified by:
isResourceClose in interface ResourceTracker<Stream>
Parameters:
basicBlock - basic block containing the instruction
handle - the instruction
cpg - the ConstantPoolGen for the method
resource - the resource, as returned by isResourceCreation()
frame - the ResourceValueFrame representing the stack prior to executing the instruction
Returns:
true if the resource is closed here, false otherwise

createVisitor

public ResourceValueFrameModelingVisitor createVisitor(Stream resource,
                                                       org.apache.bcel.generic.ConstantPoolGen cpg)
Description copied from interface: ResourceTracker
Create a ResourceValueFrameModelingVisitor to model the effect of instructions on the state of the resource.

Specified by:
createVisitor in interface ResourceTracker<Stream>
Parameters:
resource - the resource we are tracking
cpg - the ConstantPoolGen of the method
Returns:
a ResourceValueFrameModelingVisitor

ignoreImplicitExceptions

public boolean ignoreImplicitExceptions(Stream resource)
Description copied from interface: ResourceTracker
Determine whether the analysis should ignore exception edges on which only implicit exceptions are propagated. This allows different resource types to be tracked with varying precision. For example, we might want to ignore implicit exceptions for stream objects, but treat them as significant for database resources.

Specified by:
ignoreImplicitExceptions in interface ResourceTracker<Stream>
Parameters:
resource - the resource being tracked
Returns:
true if implicit exceptions are significant, false if they should be ignore

isParamInstance

public boolean isParamInstance(Stream resource,
                               int slot)
Description copied from interface: ResourceTracker
Return if the given parameter slot contains the resource instance upon entry to the method. This is for resources passed as parameters.

Specified by:
isParamInstance in interface ResourceTracker<Stream>
Parameters:
resource - the resource
slot - the local variable slot
Returns:
true if the slot contains the resource instance, false otherwise