Package edu.umd.cs.findbugs
Class FindBugs2
- java.lang.Object
-
- edu.umd.cs.findbugs.FindBugs2
-
- All Implemented Interfaces:
IFindBugsEngine,java.lang.AutoCloseable
public class FindBugs2 extends java.lang.Object implements IFindBugsEngine, java.lang.AutoCloseable
FindBugs driver class. Orchestrates the analysis of a project, collection of results, etc.
-
-
Field Summary
-
Constructor Summary
Constructors Constructor Description FindBugs2()Constructor that usesCurrentThreadExecutorServiceto keep backward compatibility with SpotBugs 3.1.FindBugs2(java.util.concurrent.ExecutorService service)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddClassObserver(IClassObserver classObserver)Add an IClassObserver.voidaddFilter(java.lang.String filterFileName, boolean include)Set filter of bug instances to include or exclude.private voidanalyzeApplication()Analyze the classes in the application codebase.private voidbuildClassPath()Build the classpath from project codebases and system codebases.private voidbuildReferencedClassSet()static voidclearAnalysisContext()protected voidclearCaches()Protected to allow Eclipse plugin remember some cache data for later reusevoidclose()private voidconfigureAnalysisFeatures()Configure analysis feature settings.protected voidconfigureFilters(UserPreferences userPreferences)protected IAnalysisCachecreateAnalysisCache()Create the analysis cache object and register it for current execution thread.static voidcreateAnalysisContext(Project project, java.util.List<ClassDescriptor> appClassList, java.lang.String sourceInfoFileName)Create the AnalysisContext that will serve as the BCEL-compatibility layer over the AnalysisCache.private voidcreateClassPath()Create the classpath object.private voidcreateExecutionPlan()Create an execution plan.voiddispose()To avoid cyclic cross-references and allow GC after engine is no longer needed.booleanemitTrainingOutput()Return whether or not training output should be emitted after analysis completes.voidenableTrainingInput(java.lang.String trainingInputDir)Set whether or not training input should be used to make the analysis more precise.voidenableTrainingOutput(java.lang.String trainingOutputDir)Set whether or not training output should be emitted.voidexcludeBaselineBugs(java.lang.String baselineBugs)Provide baseline of bugs not to reportvoidexecute()Execute the analysis.voidfinishSettings()intgetBugCount()Get the number of bug instances that were reported during analysis.BugReportergetBugReporter()Get the BugReporter.java.lang.StringgetCurrentClass()Get the name of the most recent class to be analyzed.intgetErrorCount()Get the number of errors that occurred during analysis.intgetMissingClassCount()Get the number of time missing classes were reported during analysis.ProjectgetProject()Get the Project.java.lang.StringgetProjectName()java.lang.StringgetReleaseName()java.lang.StringgetTrainingInputDir()Get the training input database directory.java.lang.StringgetTrainingOutputDir()Get the training output directory.UserPreferencesgetUserPreferences()Get the UserPreferences.private voidlogRecoverableException(ClassDescriptor classDescriptor, Detector2 detector, java.lang.Throwable e)Report an exception that occurred while analyzing a class with a detector.static voidmain(java.lang.String[] args)private voidnotifyClassObservers(ClassDescriptor classDescriptor)Notify all IClassObservers that we are visiting given class.static voidregisterBuiltInAnalysisEngines(IAnalysisCache analysisCache)Register the "built-in" analysis engines with given IAnalysisCache.static voidregisterPluginAnalysisEngines(DetectorFactoryCollection detectorFactoryCollection, IAnalysisCache analysisCache)Register all of the analysis engines defined in the plugins contained in a DetectorFactoryCollection with an IAnalysisCache.voidsetAbridgedMessages(boolean xmlWithAbridgedMessages)voidsetAnalysisFeatureSettings(AnalysisFeatureSetting[] settingList)Set analysis feature settings.static voidsetAppClassList(java.util.List<ClassDescriptor> appClassList)voidsetApplySuppression(boolean applySuppression)voidsetBugReporter(BugReporter bugReporter)Set the BugReporter.voidsetBugReporterDecorators(java.util.Set<java.lang.String> explicitlyEnabled, java.util.Set<java.lang.String> explicitlyDisabled)voidsetClassScreener(IClassScreener classScreener)Set the ClassScreener.voidsetDetectorFactoryCollection(DetectorFactoryCollection detectorFactoryCollection)Set the detector factory collection to be used by this FindBugs2 engine.voidsetMergeSimilarWarnings(boolean mergeSimilarWarnings)voidsetNoClassOk(boolean noClassOk)Set whether or not to generate an empty output file if there were no class files specified.voidsetProgressCallback(FindBugsProgress progressCallback)Set the progress callback that will be used to keep track of the progress of the analysis.voidsetProject(Project project)Set the Project.voidsetProjectName(java.lang.String name)voidsetRankThreshold(int rankThreshold)voidsetRelaxedReportingMode(boolean relaxedReportingMode)Set relaxed reporting mode.voidsetReleaseName(java.lang.String releaseName)voidsetScanNestedArchives(boolean scanNestedArchives)Set whether or not nested archives should be scanned.voidsetSourceInfoFile(java.lang.String sourceInfoFile)Set the filename of the source info file containing line numbers for fields and classes.voidsetUserPreferences(UserPreferences userPreferences)Set the UserPreferences representing which Detectors should be used.java.util.List<ClassDescriptor>sortByCallGraph(java.util.Collection<ClassDescriptor> classList, TopologicalSort.OutEdges<ClassDescriptor> outEdges)booleanuseTrainingInput()Return whether or not we should make use of training data.
-
-
-
Field Detail
-
LOG
private static final org.slf4j.Logger LOG
-
LIST_ORDER
private static final boolean LIST_ORDER
-
VERBOSE
private static final boolean VERBOSE
-
DEBUG
public static final boolean DEBUG
-
PROGRESS
public static final boolean PROGRESS
-
SCREEN_FIRST_PASS_CLASSES
private static final boolean SCREEN_FIRST_PASS_CLASSES
-
MULTI_THREAD
public static final boolean MULTI_THREAD
-
PROP_FINDBUGS_HOST_APP
public static final java.lang.String PROP_FINDBUGS_HOST_APP
- See Also:
- Constant Field Values
-
PROP_FINDBUGS_HOST_APP_VERSION
public static final java.lang.String PROP_FINDBUGS_HOST_APP_VERSION
- See Also:
- Constant Field Values
-
rankThreshold
private int rankThreshold
-
classObserverList
private java.util.List<IClassObserver> classObserverList
-
bugReporter
private BugReporter bugReporter
-
errorCountingBugReporter
private ErrorCountingBugReporter errorCountingBugReporter
-
project
private Project project
-
classFactory
private IClassFactory classFactory
-
classPath
private IClassPath classPath
-
appClassList
private java.util.List<ClassDescriptor> appClassList
-
referencedClassSet
private java.util.Collection<ClassDescriptor> referencedClassSet
-
detectorFactoryCollection
private DetectorFactoryCollection detectorFactoryCollection
-
executionPlan
private ExecutionPlan executionPlan
-
currentClassName
private java.lang.String currentClassName
-
progressReporter
private FindBugsProgress progressReporter
-
classScreener
private IClassScreener classScreener
-
analysisOptions
private final AnalysisOptions analysisOptions
-
service
private final java.util.concurrent.ExecutorService service
-
explicitlyEnabledBugReporterDecorators
@Nonnull java.util.Set<java.lang.String> explicitlyEnabledBugReporterDecorators
-
explicitlyDisabledBugReporterDecorators
@Nonnull java.util.Set<java.lang.String> explicitlyDisabledBugReporterDecorators
-
-
Constructor Detail
-
FindBugs2
public FindBugs2()
Constructor that usesCurrentThreadExecutorServiceto keep backward compatibility with SpotBugs 3.1.- Since:
- 3.1
-
FindBugs2
public FindBugs2(@NonNull java.util.concurrent.ExecutorService service)
- Parameters:
service- The non-nullExecutorServiceinstance to execute analysis. Caller is responsible for shutting it down.- Since:
- 4.0
-
-
Method Detail
-
setDetectorFactoryCollection
public void setDetectorFactoryCollection(DetectorFactoryCollection detectorFactoryCollection)
Set the detector factory collection to be used by this FindBugs2 engine. This method should be called before the execute() method is called.- Specified by:
setDetectorFactoryCollectionin interfaceIFindBugsEngine- Parameters:
detectorFactoryCollection- The detectorFactoryCollection to set.
-
execute
public void execute() throws java.io.IOException, java.lang.InterruptedExceptionExecute the analysis. For obscure reasons, CheckedAnalysisExceptions are re-thrown as IOExceptions. However, these can only happen during the setup phase where we scan codebases for classes.- Specified by:
executein interfaceIFindBugsEngine- Throws:
java.io.IOExceptionjava.lang.InterruptedException
-
clearCaches
protected void clearCaches()
Protected to allow Eclipse plugin remember some cache data for later reuse
-
dispose
public void dispose()
To avoid cyclic cross-references and allow GC after engine is no longer needed. (used by Eclipse plugin)
Caller probably need to shutdown the
ExecutorServiceinstance provided at constructor.
-
getBugReporter
public BugReporter getBugReporter()
Description copied from interface:IFindBugsEngineGet the BugReporter.- Specified by:
getBugReporterin interfaceIFindBugsEngine- Returns:
- the BugReporter
-
getProject
public Project getProject()
Description copied from interface:IFindBugsEngineGet the Project.- Specified by:
getProjectin interfaceIFindBugsEngine- Returns:
- the Project
-
addClassObserver
public void addClassObserver(IClassObserver classObserver)
Description copied from interface:IFindBugsEngineAdd an IClassObserver.- Specified by:
addClassObserverin interfaceIFindBugsEngine- Parameters:
classObserver- the IClassObserver
-
addFilter
public void addFilter(java.lang.String filterFileName, boolean include) throws java.io.IOException, FilterExceptionDescription copied from interface:IFindBugsEngineSet filter of bug instances to include or exclude.- Specified by:
addFilterin interfaceIFindBugsEngine- Parameters:
filterFileName- the name of the filter fileinclude- true if the filter specifies bug instances to include, false if it specifies bug instances to exclude- Throws:
java.io.IOExceptionFilterException
-
excludeBaselineBugs
public void excludeBaselineBugs(java.lang.String baselineBugs) throws java.io.IOException, org.dom4j.DocumentExceptionDescription copied from interface:IFindBugsEngineProvide baseline of bugs not to report- Specified by:
excludeBaselineBugsin interfaceIFindBugsEngine- Parameters:
baselineBugs- the name of the xml bug baseline file- Throws:
org.dom4j.DocumentExceptionjava.io.IOException
-
enableTrainingInput
public void enableTrainingInput(java.lang.String trainingInputDir)
Description copied from interface:IFindBugsEngineSet whether or not training input should be used to make the analysis more precise.- Specified by:
enableTrainingInputin interfaceIFindBugsEngine- Parameters:
trainingInputDir- directory to load training input from
-
enableTrainingOutput
public void enableTrainingOutput(java.lang.String trainingOutputDir)
Description copied from interface:IFindBugsEngineSet whether or not training output should be emitted.- Specified by:
enableTrainingOutputin interfaceIFindBugsEngine- Parameters:
trainingOutputDir- directory to save training output in
-
getBugCount
public int getBugCount()
Description copied from interface:IFindBugsEngineGet the number of bug instances that were reported during analysis.- Specified by:
getBugCountin interfaceIFindBugsEngine
-
getCurrentClass
public java.lang.String getCurrentClass()
Description copied from interface:IFindBugsEngineGet the name of the most recent class to be analyzed. This is useful for diagnosing an unexpected exception. Returns null if no class has been analyzed.- Specified by:
getCurrentClassin interfaceIFindBugsEngine
-
getErrorCount
public int getErrorCount()
Description copied from interface:IFindBugsEngineGet the number of errors that occurred during analysis.- Specified by:
getErrorCountin interfaceIFindBugsEngine
-
getMissingClassCount
public int getMissingClassCount()
Description copied from interface:IFindBugsEngineGet the number of time missing classes were reported during analysis.- Specified by:
getMissingClassCountin interfaceIFindBugsEngine
-
getReleaseName
public java.lang.String getReleaseName()
- Specified by:
getReleaseNamein interfaceIFindBugsEngine- Returns:
- Returns the releaseName.
-
getProjectName
public java.lang.String getProjectName()
- Specified by:
getProjectNamein interfaceIFindBugsEngine- Returns:
- Returns the projectName.
-
setProjectName
public void setProjectName(java.lang.String name)
- Specified by:
setProjectNamein interfaceIFindBugsEngine- Parameters:
name- The project name to set.
-
setAnalysisFeatureSettings
public void setAnalysisFeatureSettings(AnalysisFeatureSetting[] settingList)
Description copied from interface:IFindBugsEngineSet analysis feature settings.- Specified by:
setAnalysisFeatureSettingsin interfaceIFindBugsEngine- Parameters:
settingList- list of analysis feature settings
-
setBugReporter
public void setBugReporter(BugReporter bugReporter)
Description copied from interface:IFindBugsEngineSet the BugReporter.- Specified by:
setBugReporterin interfaceIFindBugsEngine- Parameters:
bugReporter- The BugReporter to set
-
setClassScreener
public void setClassScreener(IClassScreener classScreener)
Description copied from interface:IFindBugsEngineSet the ClassScreener. This object chooses which individual classes to analyze. By default, all classes are analyzed.- Specified by:
setClassScreenerin interfaceIFindBugsEngine- Parameters:
classScreener- the ClassScreener to use
-
setProgressCallback
public void setProgressCallback(FindBugsProgress progressCallback)
Description copied from interface:IFindBugsEngineSet the progress callback that will be used to keep track of the progress of the analysis.- Specified by:
setProgressCallbackin interfaceIFindBugsEngine- Parameters:
progressCallback- the progress callback
-
setProject
public void setProject(Project project)
Description copied from interface:IFindBugsEngineSet the Project.- Specified by:
setProjectin interfaceIFindBugsEngine- Parameters:
project- The Project to set
-
setRelaxedReportingMode
public void setRelaxedReportingMode(boolean relaxedReportingMode)
Description copied from interface:IFindBugsEngineSet relaxed reporting mode.- Specified by:
setRelaxedReportingModein interfaceIFindBugsEngine- Parameters:
relaxedReportingMode- true if relaxed reporting mode should be enabled, false if not
-
setReleaseName
public void setReleaseName(java.lang.String releaseName)
- Specified by:
setReleaseNamein interfaceIFindBugsEngine- Parameters:
releaseName- The releaseName to set.
-
setSourceInfoFile
public void setSourceInfoFile(java.lang.String sourceInfoFile)
Description copied from interface:IFindBugsEngineSet the filename of the source info file containing line numbers for fields and classes.- Specified by:
setSourceInfoFilein interfaceIFindBugsEngine- Parameters:
sourceInfoFile- the source info filename
-
setUserPreferences
public void setUserPreferences(UserPreferences userPreferences)
Description copied from interface:IFindBugsEngineSet the UserPreferences representing which Detectors should be used. If UserPreferences are not set explicitly, the default set of Detectors will be used.- Specified by:
setUserPreferencesin interfaceIFindBugsEngine- Parameters:
userPreferences- the UserPreferences
-
configureFilters
protected void configureFilters(UserPreferences userPreferences)
-
emitTrainingOutput
public boolean emitTrainingOutput()
Description copied from interface:IFindBugsEngineReturn whether or not training output should be emitted after analysis completes.- Specified by:
emitTrainingOutputin interfaceIFindBugsEngine- Returns:
- true if training output should be emitted, false if not
-
getUserPreferences
public UserPreferences getUserPreferences()
Description copied from interface:IFindBugsEngineGet the UserPreferences.- Specified by:
getUserPreferencesin interfaceIFindBugsEngine- Returns:
- the UserPreferences
-
createClassPath
private void createClassPath()
Create the classpath object.
-
getTrainingInputDir
public java.lang.String getTrainingInputDir()
Description copied from interface:IFindBugsEngineGet the training input database directory.- Specified by:
getTrainingInputDirin interfaceIFindBugsEngine- Returns:
- the training input database directory
-
getTrainingOutputDir
public java.lang.String getTrainingOutputDir()
Description copied from interface:IFindBugsEngineGet the training output directory.- Specified by:
getTrainingOutputDirin interfaceIFindBugsEngine- Returns:
- the training output directory
-
useTrainingInput
public boolean useTrainingInput()
Description copied from interface:IFindBugsEngineReturn whether or not we should make use of training data.- Specified by:
useTrainingInputin interfaceIFindBugsEngine- Returns:
- true if training data should be used, false if not
-
setScanNestedArchives
public void setScanNestedArchives(boolean scanNestedArchives)
Description copied from interface:IFindBugsEngineSet whether or not nested archives should be scanned.- Specified by:
setScanNestedArchivesin interfaceIFindBugsEngine- Parameters:
scanNestedArchives- true if nested archives should be scanned, false if not
-
setNoClassOk
public void setNoClassOk(boolean noClassOk)
Description copied from interface:IFindBugsEngineSet whether or not to generate an empty output file if there were no class files specified.- Specified by:
setNoClassOkin interfaceIFindBugsEngine- Parameters:
noClassOk- true if FindBugs should generate empty output file
-
createAnalysisCache
protected IAnalysisCache createAnalysisCache() throws java.io.IOException
Create the analysis cache object and register it for current execution thread.This method is protected to allow clients override it and possibly reuse some previous analysis data (for Eclipse interactive re-build)
- Throws:
java.io.IOException- if error occurs registering analysis engines in a plugin
-
registerBuiltInAnalysisEngines
public static void registerBuiltInAnalysisEngines(IAnalysisCache analysisCache)
Register the "built-in" analysis engines with given IAnalysisCache.- Parameters:
analysisCache- an IAnalysisCache
-
registerPluginAnalysisEngines
public static void registerPluginAnalysisEngines(DetectorFactoryCollection detectorFactoryCollection, IAnalysisCache analysisCache) throws java.io.IOException
Register all of the analysis engines defined in the plugins contained in a DetectorFactoryCollection with an IAnalysisCache.- Parameters:
detectorFactoryCollection- a DetectorFactoryCollectionanalysisCache- an IAnalysisCache- Throws:
java.io.IOException
-
buildClassPath
private void buildClassPath() throws java.lang.InterruptedException, java.io.IOException, CheckedAnalysisExceptionBuild the classpath from project codebases and system codebases.- Throws:
java.lang.InterruptedException- if the analysis thread is interruptedjava.io.IOException- if an I/O error occursCheckedAnalysisException
-
buildReferencedClassSet
private void buildReferencedClassSet() throws java.lang.InterruptedException- Throws:
java.lang.InterruptedException
-
sortByCallGraph
public java.util.List<ClassDescriptor> sortByCallGraph(java.util.Collection<ClassDescriptor> classList, TopologicalSort.OutEdges<ClassDescriptor> outEdges)
-
clearAnalysisContext
public static void clearAnalysisContext()
-
createAnalysisContext
public static void createAnalysisContext(Project project, java.util.List<ClassDescriptor> appClassList, @CheckForNull java.lang.String sourceInfoFileName) throws java.io.IOException
Create the AnalysisContext that will serve as the BCEL-compatibility layer over the AnalysisCache.- Parameters:
project- The projectappClassList- list of ClassDescriptors identifying application classessourceInfoFileName- name of source info file (null if none)- Throws:
java.io.IOException
-
setAppClassList
public static void setAppClassList(java.util.List<ClassDescriptor> appClassList)
-
configureAnalysisFeatures
private void configureAnalysisFeatures()
Configure analysis feature settings.
-
createExecutionPlan
private void createExecutionPlan() throws OrderingConstraintExceptionCreate an execution plan.- Throws:
OrderingConstraintException- if the detector ordering constraints are inconsistent
-
analyzeApplication
private void analyzeApplication() throws java.lang.InterruptedExceptionAnalyze the classes in the application codebase.- Throws:
java.lang.InterruptedException
-
notifyClassObservers
private void notifyClassObservers(ClassDescriptor classDescriptor)
Notify all IClassObservers that we are visiting given class.- Parameters:
classDescriptor- the class being visited
-
logRecoverableException
private void logRecoverableException(ClassDescriptor classDescriptor, Detector2 detector, java.lang.Throwable e)
Report an exception that occurred while analyzing a class with a detector.- Parameters:
classDescriptor- class being analyzeddetector- detector doing the analysise- the exception
-
main
public static void main(java.lang.String[] args) throws java.lang.Exception- Throws:
java.lang.Exception
-
setAbridgedMessages
public void setAbridgedMessages(boolean xmlWithAbridgedMessages)
- Specified by:
setAbridgedMessagesin interfaceIFindBugsEngine
-
setMergeSimilarWarnings
public void setMergeSimilarWarnings(boolean mergeSimilarWarnings)
- Specified by:
setMergeSimilarWarningsin interfaceIFindBugsEngine
-
setApplySuppression
public void setApplySuppression(boolean applySuppression)
- Specified by:
setApplySuppressionin interfaceIFindBugsEngine
-
setRankThreshold
public void setRankThreshold(int rankThreshold)
- Specified by:
setRankThresholdin interfaceIFindBugsEngine
-
finishSettings
public void finishSettings()
- Specified by:
finishSettingsin interfaceIFindBugsEngine
-
setBugReporterDecorators
public void setBugReporterDecorators(java.util.Set<java.lang.String> explicitlyEnabled, java.util.Set<java.lang.String> explicitlyDisabled)- Specified by:
setBugReporterDecoratorsin interfaceIFindBugsEngine
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable
-
-