⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 resourcetrackingdetector.java

📁 一个查找java程序里bug的程序的源代码,该程序本身也是java写的,对提高java编程水平很有用
💻 JAVA
字号:
/* * FindBugs - Find bugs in Java programs * Copyright (C) 2003,2004 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.util.*;import edu.umd.cs.findbugs.ba.*;import org.apache.bcel.classfile.JavaClass;import org.apache.bcel.classfile.Method;import org.apache.bcel.generic.ConstantPoolGen;import org.apache.bcel.generic.MethodGen;/** * Abstract implementation of a Detector to find methods where a * particular kind of created resource is not cleaned up * or closed properly.  Subclasses should override the * abstract methods to determine what kinds of resources * are tracked by the detector. * * @author David Hovemeyer */public abstract class ResourceTrackingDetector <Resource, ResourceTrackerType extends ResourceTracker<Resource>>        implements Detector {	private static final boolean DEBUG = Boolean.getBoolean("rtd.debug");	private static final String DEBUG_METHOD_NAME = System.getProperty("rtd.method");	protected BugReporter bugReporter;	private AnalysisContext analysisContext;	public ResourceTrackingDetector(BugReporter bugReporter) {		this.bugReporter = bugReporter;	}	public abstract boolean prescreen(ClassContext classContext, Method method);	public abstract ResourceTrackerType getResourceTracker(ClassContext classContext, Method method)	        throws DataflowAnalysisException, CFGBuilderException;	public abstract void inspectResult(JavaClass javaClass, MethodGen methodGen, CFG cfg,	                                   Dataflow<ResourceValueFrame, ResourceValueAnalysis<Resource>> dataflow, Resource resource);	public void setAnalysisContext(AnalysisContext analysisContext) {		this.analysisContext = analysisContext;	}	/**	 * Get the AnalysisContext.	 *	 * @return the AnalysisContext	 */	protected AnalysisContext getAnalysisContext() {		return analysisContext;	}	public void visitClassContext(ClassContext classContext) {		final JavaClass jclass = classContext.getJavaClass();		Method[] methodList = jclass.getMethods();		for (int i = 0; i < methodList.length; ++i) {			Method method = methodList[i];			if (method.isAbstract() || method.isNative())				continue;			MethodGen methodGen = classContext.getMethodGen(method);			if (methodGen == null)				continue;			if (DEBUG_METHOD_NAME != null && !DEBUG_METHOD_NAME.equals(method.getName()))				continue;			if (!prescreen(classContext, method))				continue;			if (DEBUG) {				System.out.println("----------------------------------------------------------------------");				System.out.println("Analyzing " + SignatureConverter.convertMethodSignature(methodGen));				System.out.println("----------------------------------------------------------------------");			}			try {				ResourceTrackerType resourceTracker = getResourceTracker(classContext, method);				ResourceCollection<Resource> resourceCollection =				        buildResourceCollection(classContext, method, resourceTracker);				if (resourceCollection.isEmpty())					continue;				analyzeMethod(classContext, method, resourceTracker, resourceCollection);			} catch (CFGBuilderException e) {				bugReporter.logError(e.toString());			} catch (DataflowAnalysisException e) {				bugReporter.logError(e.toString());			}		}	}	private ResourceCollection<Resource> buildResourceCollection(ClassContext classContext,	                                                             Method method, ResourceTrackerType resourceTracker)	        throws CFGBuilderException, DataflowAnalysisException {		ResourceCollection<Resource> resourceCollection = new ResourceCollection<Resource>();		CFG cfg = classContext.getCFG(method);		ConstantPoolGen cpg = classContext.getConstantPoolGen();		for (Iterator<Location> i = cfg.locationIterator(); i.hasNext();) {			Location location = i.next();			Resource resource = resourceTracker.isResourceCreation(location.getBasicBlock(),			        location.getHandle(), cpg);			if (resource != null)				resourceCollection.addCreatedResource(location, resource);		}		return resourceCollection;	}	public void analyzeMethod(ClassContext classContext, Method method,	                          ResourceTrackerType resourceTracker, ResourceCollection<Resource> resourceCollection)	        throws CFGBuilderException, DataflowAnalysisException {		MethodGen methodGen = classContext.getMethodGen(method);		CFG cfg = classContext.getCFG(method);		DepthFirstSearch dfs = classContext.getDepthFirstSearch(method);		if (DEBUG) System.out.println(SignatureConverter.convertMethodSignature(methodGen));		for (Iterator<Resource> i = resourceCollection.resourceIterator(); i.hasNext();) {			Resource resource = i.next();			ResourceValueAnalysis<Resource> analysis =			        new ResourceValueAnalysis<Resource>(methodGen, cfg, dfs, resourceTracker, resource);			Dataflow<ResourceValueFrame, ResourceValueAnalysis<Resource>> dataflow =			        new Dataflow<ResourceValueFrame, ResourceValueAnalysis<Resource>>(cfg, analysis);			dataflow.execute();			inspectResult(classContext.getJavaClass(), methodGen, cfg, dataflow, resource);		}	}	public void report() {	}}// vim:ts=3

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -