📄 findbugs2.java
字号:
/* * FindBugs - Find Bugs in Java programs * Copyright (C) 2006, University of Maryland * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package edu.umd.cs.findbugs;import java.io.FileInputStream;import java.io.IOException;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.Collections;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.Set;import java.util.TreeSet;import org.apache.bcel.classfile.ClassFormatException;import edu.umd.cs.findbugs.ba.AnalysisCacheToAnalysisContextAdapter;import edu.umd.cs.findbugs.ba.AnalysisContext;import edu.umd.cs.findbugs.ba.AnalysisException;import edu.umd.cs.findbugs.ba.SourceInfoMap;import edu.umd.cs.findbugs.ba.ch.Subtypes;import edu.umd.cs.findbugs.classfile.CheckedAnalysisException;import edu.umd.cs.findbugs.classfile.ClassDescriptor;import edu.umd.cs.findbugs.classfile.Global;import edu.umd.cs.findbugs.classfile.IAnalysisCache;import edu.umd.cs.findbugs.classfile.IClassFactory;import edu.umd.cs.findbugs.classfile.IClassObserver;import edu.umd.cs.findbugs.classfile.IClassPath;import edu.umd.cs.findbugs.classfile.IClassPathBuilder;import edu.umd.cs.findbugs.classfile.ICodeBase;import edu.umd.cs.findbugs.classfile.MissingClassException;import edu.umd.cs.findbugs.classfile.ResourceNotFoundException;import edu.umd.cs.findbugs.classfile.analysis.ClassInfo;import edu.umd.cs.findbugs.classfile.impl.ClassFactory;import edu.umd.cs.findbugs.config.AnalysisFeatureSetting;import edu.umd.cs.findbugs.config.UserPreferences;import edu.umd.cs.findbugs.filter.FilterException;import edu.umd.cs.findbugs.plan.AnalysisPass;import edu.umd.cs.findbugs.plan.ExecutionPlan;import edu.umd.cs.findbugs.plan.OrderingConstraintException;import edu.umd.cs.findbugs.util.ClassName;import edu.umd.cs.findbugs.util.TopologicalSort.OutEdges;/** * FindBugs driver class. * Experimental version to use the new bytecode-framework-neutral * codebase/classpath/classfile infrastructure. * Supports all features of the original FindBugs driver. * * @author David Hovemeyer */public class FindBugs2 implements IFindBugsEngine { private static final boolean VERBOSE = SystemProperties.getBoolean("findbugs.verbose"); public static final boolean DEBUG = VERBOSE || SystemProperties.getBoolean("findbugs.debug"); private List<IClassObserver> classObserverList; private ErrorCountingBugReporter bugReporter; private Project project; private IClassFactory classFactory; private IClassPath classPath; private IAnalysisCache analysisCache; private List<ClassDescriptor> appClassList; private Set<ClassDescriptor> referencedClassSet; private DetectorFactoryCollection detectorFactoryCollection; private ExecutionPlan executionPlan; private UserPreferences userPreferences; private String currentClassName; private String releaseName; private String projectName; private String sourceInfoFileName; private AnalysisFeatureSetting[] analysisFeatureSettingList; private boolean relaxedReportingMode; private boolean abridgedMessages; private String trainingInputDir; private String trainingOutputDir; private FindBugsProgress progress; private IClassScreener classScreener; private boolean scanNestedArchives; /** * Constructor. */ public FindBugs2() { this.classObserverList = new LinkedList<IClassObserver>(); this.analysisFeatureSettingList = FindBugs.DEFAULT_EFFORT; this.progress = new NoOpFindBugsProgress(); // By default, do not exclude any classes via the class screener this.classScreener = new IClassScreener() { /* (non-Javadoc) * @see edu.umd.cs.findbugs.IClassScreener#matches(java.lang.String) */ public boolean matches(String fileName) { return true; } }; // By default, we do not want to scan nested archives this.scanNestedArchives = false; } /** * Set the detector factory collection to be used by this * FindBugs2 engine. This method should be called before * the execute() method is called. * * @param detectorFactoryCollection The detectorFactoryCollection to set. */ public void setDetectorFactoryCollection( DetectorFactoryCollection detectorFactoryCollection) { this.detectorFactoryCollection = detectorFactoryCollection; } /** * Execute 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. * * @throws IOException * @throws InterruptedException */ public void execute() throws IOException, InterruptedException { // Get the class factory for creating classpath/codebase/etc. classFactory = ClassFactory.instance(); // The class path object createClassPath(); // The analysis cache object createAnalysisCache(); progress.reportNumberOfArchives(project.getFileCount()); try { // Discover all codebases in classpath and // enumerate all classes (application and non-application) buildClassPath(); // Build set of classes referenced by application classes buildReferencedClassSet(); // Create BCEL compatibility layer createAnalysisContext(); // Configure the BugCollection (if we are generating one) FindBugs.configureBugCollection(this); // Enable/disabled relaxed reporting mode FindBugsAnalysisFeatures.setRelaxedMode(relaxedReportingMode); FindBugsDisplayFeatures.setAbridgedMessages(abridgedMessages); // Configure training databases FindBugs.configureTrainingDatabases(this); // Configure analysis features configureAnalysisFeatures(); // Create the execution plan (which passes/detectors to execute) createExecutionPlan(); // Analyze the application analyzeApplication(); } catch (CheckedAnalysisException e) { IOException ioe = new IOException("IOException while scanning codebases"); ioe.initCause(e); throw ioe; } finally { // Make sure the codebases on the classpath are closed classPath.close(); } } /* (non-Javadoc) * @see edu.umd.cs.findbugs.IFindBugsEngine#getBugReporter() */ public BugReporter getBugReporter() { return bugReporter; } /* (non-Javadoc) * @see edu.umd.cs.findbugs.IFindBugsEngine#getProject() */ public Project getProject() { return project; } /* (non-Javadoc) * @see edu.umd.cs.findbugs.IFindBugsEngine#addClassObserver(edu.umd.cs.findbugs.classfile.IClassObserver) */ public void addClassObserver(IClassObserver classObserver) { classObserverList.add(classObserver); } /* (non-Javadoc) * @see edu.umd.cs.findbugs.IFindBugsEngine#addFilter(java.lang.String, boolean) */ public void addFilter(String filterFileName, boolean include) throws IOException, FilterException { FindBugs.configureFilter(bugReporter, filterFileName, include); } /* (non-Javadoc) * @see edu.umd.cs.findbugs.IFindBugsEngine#enableTrainingInput(java.lang.String) */ public void enableTrainingInput(String trainingInputDir) { this.trainingInputDir = trainingInputDir; } /* (non-Javadoc) * @see edu.umd.cs.findbugs.IFindBugsEngine#enableTrainingOutput(java.lang.String) */ public void enableTrainingOutput(String trainingOutputDir) { this.trainingOutputDir = trainingOutputDir; } /* (non-Javadoc) * @see edu.umd.cs.findbugs.IFindBugsEngine#getBugCount() */ public int getBugCount() { return bugReporter.getBugCount(); } /* (non-Javadoc) * @see edu.umd.cs.findbugs.IFindBugsEngine#getCurrentClass() */ public String getCurrentClass() { return currentClassName; } /* (non-Javadoc) * @see edu.umd.cs.findbugs.IFindBugsEngine#getErrorCount() */ public int getErrorCount() { return bugReporter.getErrorCount(); } /* (non-Javadoc) * @see edu.umd.cs.findbugs.IFindBugsEngine#getMissingClassCount() */ public int getMissingClassCount() { return bugReporter.getMissingClassCount(); } /* (non-Javadoc) * @see edu.umd.cs.findbugs.IFindBugsEngine#getReleaseName() */ public String getReleaseName() { return releaseName; } public String getProjectName() { return projectName; } public void setProjectName(String name) { projectName = name; } /* (non-Javadoc) * @see edu.umd.cs.findbugs.IFindBugsEngine#setAnalysisFeatureSettings(edu.umd.cs.findbugs.config.AnalysisFeatureSetting[]) */ public void setAnalysisFeatureSettings(AnalysisFeatureSetting[] settingList) { this.analysisFeatureSettingList = settingList; } /* (non-Javadoc) * @see edu.umd.cs.findbugs.IFindBugsEngine#setBugReporter(edu.umd.cs.findbugs.BugReporter) */ public void setBugReporter(BugReporter bugReporter) { this.bugReporter = new ErrorCountingBugReporter(bugReporter); addClassObserver(bugReporter); } /* (non-Javadoc) * @see edu.umd.cs.findbugs.IFindBugsEngine#setClassScreener(edu.umd.cs.findbugs.ClassScreener) */ public void setClassScreener(IClassScreener classScreener) { this.classScreener = classScreener; } /* (non-Javadoc) * @see edu.umd.cs.findbugs.IFindBugsEngine#setProgressCallback(edu.umd.cs.findbugs.FindBugsProgress) */ public void setProgressCallback(FindBugsProgress progressCallback) { this.progress = progressCallback; } /* (non-Javadoc) * @see edu.umd.cs.findbugs.IFindBugsEngine#setProject(edu.umd.cs.findbugs.Project) */ public void setProject(Project project) { this.project = project; } /* (non-Javadoc) * @see edu.umd.cs.findbugs.IFindBugsEngine#setRelaxedReportingMode(boolean) */ public void setRelaxedReportingMode(boolean relaxedReportingMode) { this.relaxedReportingMode = relaxedReportingMode; } /* (non-Javadoc) * @see edu.umd.cs.findbugs.IFindBugsEngine#setReleaseName(java.lang.String) */ public void setReleaseName(String releaseName) { this.releaseName = releaseName; } /* (non-Javadoc) * @see edu.umd.cs.findbugs.IFindBugsEngine#setSourceInfoFile(java.lang.String) */ public void setSourceInfoFile(String sourceInfoFile) { this.sourceInfoFileName = sourceInfoFile; } /* (non-Javadoc) * @see edu.umd.cs.findbugs.IFindBugsEngine#setUserPreferences(edu.umd.cs.findbugs.config.UserPreferences) */ public void setUserPreferences(UserPreferences userPreferences) { this.userPreferences = userPreferences; } /* (non-Javadoc) * @see edu.umd.cs.findbugs.IFindBugsEngine#emitTrainingOutput() */ public boolean emitTrainingOutput() { return trainingOutputDir != null; } /* (non-Javadoc) * @see edu.umd.cs.findbugs.IFindBugsEngine#getUserPreferences() */ public UserPreferences getUserPreferences() { return userPreferences; } /** * Create the classpath object. */ private void createClassPath() { classPath = classFactory.createClassPath(); } /* (non-Javadoc) * @see edu.umd.cs.findbugs.IFindBugsEngine#getTrainingInputDir() */ public String getTrainingInputDir() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -