edu.umd.cs.findbugs.detect
Class BCPDoubleCheck

java.lang.Object
  extended by edu.umd.cs.findbugs.ByteCodePatternDetector
      extended by edu.umd.cs.findbugs.detect.BCPDoubleCheck
All Implemented Interfaces:
Detector

public class BCPDoubleCheck
extends ByteCodePatternDetector

A bug detector that uses a ByteCodePattern to find instances of the double check idiom. This class serves as a good example of how ByteCodePatterns can be used to simplify the task of implementing Detectors.

Author:
David Hovemeyer
See Also:
ByteCodePattern

Field Summary
private  BugReporter bugReporter
           
private static int CREATE_OBJ_WILD
          Maximum number of "wildcard" instructions to accept for object creation in the doublecheck.
private static int MAX_WILD
          Default maximum number of "wildcard" instructions to accept between explicit pattern instructions.
private static ByteCodePattern pattern
          The doublecheck pattern.
 
Fields inherited from interface edu.umd.cs.findbugs.Detector
EXP_PRIORITY, HIGH_PRIORITY, LOW_PRIORITY, NORMAL_PRIORITY
 
Constructor Summary
BCPDoubleCheck(BugReporter bugReporter)
          Constructor.
 
Method Summary
 ByteCodePattern getPattern()
          Get the ByteCodePattern for this detector.
 boolean prescreen(org.apache.bcel.classfile.Method method, ClassContext classContext)
          Prescreen a method.
 void reportMatch(ClassContext classContext, org.apache.bcel.classfile.Method method, ByteCodePatternMatch match)
          Called to report an instance of the ByteCodePattern.
 
Methods inherited from class edu.umd.cs.findbugs.ByteCodePatternDetector
getAnalysisContext, report, setAnalysisContext, visitClassContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

bugReporter

private BugReporter bugReporter

MAX_WILD

private static final int MAX_WILD
Default maximum number of "wildcard" instructions to accept between explicit pattern instructions.

See Also:
Constant Field Values

CREATE_OBJ_WILD

private static final int CREATE_OBJ_WILD
Maximum number of "wildcard" instructions to accept for object creation in the doublecheck. This needs to be a lot higher than MAX_WILD.

See Also:
Constant Field Values

pattern

private static final ByteCodePattern pattern
The doublecheck pattern. The variable "h" represents the field. "x" and "y" are local values resulting from loading the field.

Constructor Detail

BCPDoubleCheck

public BCPDoubleCheck(BugReporter bugReporter)
Constructor.

Parameters:
bugReporter -
Method Detail

getPattern

public ByteCodePattern getPattern()
Description copied from class: ByteCodePatternDetector
Get the ByteCodePattern for this detector.

Specified by:
getPattern in class ByteCodePatternDetector

prescreen

public boolean prescreen(org.apache.bcel.classfile.Method method,
                         ClassContext classContext)
Description copied from class: ByteCodePatternDetector
Prescreen a method. It is a valid, but dumb, implementation simply to return true unconditionally. A better implementation is to call ClassContext.getBytecodeSet() to check whether the method actually contains the bytecode instructions that the pattern will look for. The theory is that checking the bytecode set is very fast, while building the MethodGen, CFG, ValueNumberAnalysis, etc. objects required to match ByteCodePatterns is slow, and the bytecode pattern matching algorithm is also not particularly fast.

As a datapoint, prescreening speeds up the BCPDoubleCheck detector by a factor of 5 with no loss of generality and only a dozen or so extra lines of code.

Specified by:
prescreen in class ByteCodePatternDetector
Parameters:
method - the method
classContext - the ClassContext for the method
Returns:
true if the method should be analyzed for instances of the ByteCodePattern

reportMatch

public void reportMatch(ClassContext classContext,
                        org.apache.bcel.classfile.Method method,
                        ByteCodePatternMatch match)
Description copied from class: ByteCodePatternDetector
Called to report an instance of the ByteCodePattern.

Specified by:
reportMatch in class ByteCodePatternDetector
Parameters:
classContext - the ClassContext for the analyzed class
method - the method to instance appears in
match - the ByteCodePatternMatch object representing the match of the ByteCodePattern against actual instructions in the method