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

📄 readreturnshouldbechecked.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.detect;import edu.umd.cs.findbugs.BugInstance;import edu.umd.cs.findbugs.BugReporter;import edu.umd.cs.findbugs.BytecodeScanningDetector;import edu.umd.cs.findbugs.visitclass.Constants2;import org.apache.bcel.Repository;import org.apache.bcel.classfile.Method;public class ReadReturnShouldBeChecked extends BytecodeScanningDetector implements Constants2 {	boolean sawRead = false;	boolean sawSkip = false;	int sawAvailable = 0;	boolean wasBufferedInputStream = false;	private BugReporter bugReporter;	private int readPC, skipPC;	private String lastCallClass = null, lastCallMethod = null, lastCallSig = null;	public ReadReturnShouldBeChecked(BugReporter bugReporter) {		this.bugReporter = bugReporter;	}	public void visit(Method obj) {		sawAvailable = 0;		sawRead = false;		sawSkip = false;		//check =  (obj.getAccessFlags() & (ACC_PUBLIC | ACC_PROTECTED)) != 0;	}	public void sawOpcode(int seen) {		boolean isInputStream = false;		boolean isBufferedInputStream = false;		if (seen == INVOKEVIRTUAL || seen == INVOKEINTERFACE) {			lastCallClass = getDottedClassConstantOperand();			lastCallMethod = getNameConstantOperand();			lastCallSig = getDottedSigConstantOperand();	        if (!lastCallClass.startsWith("[")) 		try {		   isInputStream 			= (Repository.instanceOf(lastCallClass,				"java.io.InputStream")			 || Repository.implementationOf(lastCallClass,				"java.io.DataInput")			 || Repository.instanceOf(lastCallClass,				"java.io.Reader"))			&& !Repository.instanceOf(lastCallClass, 				"java.io.ByteArrayInputStream");		    isBufferedInputStream = Repository.instanceOf(getDottedClassConstantOperand(), "java.io.BufferedInputStream");		} catch (ClassNotFoundException e) {		}		}		if (seen == INVOKEVIRTUAL || seen == INVOKEINTERFACE) 		  if (getNameConstantOperand().equals("available")		        && getSigConstantOperand().equals("()I")		      || getNameConstantOperand().startsWith("get")		        && getNameConstantOperand().endsWith("Length")		        && getSigConstantOperand().equals("()I")		      ||  getClassConstantOperand().equals("java/io/File")		        && getNameConstantOperand().equals("length")		        && getSigConstantOperand().equals("()J")) {			sawAvailable = 70;			return;		}		sawAvailable--;		if ((seen == INVOKEVIRTUAL || seen == INVOKEINTERFACE)		        && isInputStream		        && getNameConstantOperand().equals("read")		        && (				getSigConstantOperand().equals("([B)I")				|| getSigConstantOperand().equals("([BII)I")		            || getSigConstantOperand().equals("([C)I")		            || getSigConstantOperand().equals("([CII)I")				)		        && sawAvailable <= 0) {			sawRead = true;			readPC = getPC();			return;			}		if ((seen == INVOKEVIRTUAL || seen == INVOKEINTERFACE)		        && isInputStream		        && getNameConstantOperand().equals("skip")		        && getSigConstantOperand().equals("(J)J")) {			// if not ByteArrayInput Stream			//  and either no recent calls to length			//        or it is a BufferedInputStream			if (sawAvailable <= 0 || isBufferedInputStream) {				wasBufferedInputStream = isBufferedInputStream;				sawSkip = true;				skipPC = getPC();				return;			}		}		if ((seen == POP) || (seen == POP2)) {			if (sawRead) {				bugReporter.reportBug(new BugInstance(this, "RR_NOT_CHECKED", NORMAL_PRIORITY)				        .addClassAndMethod(this)				        .addCalledMethod(lastCallClass, lastCallMethod, lastCallSig)				        .addSourceLine(this, readPC));			} else if (sawSkip) {				bugReporter.reportBug(new BugInstance(this, "SR_NOT_CHECKED",				        (wasBufferedInputStream ? HIGH_PRIORITY : NORMAL_PRIORITY))				        .addClassAndMethod(this)				        .addCalledMethod(lastCallClass, lastCallMethod, lastCallSig)				        .addSourceLine(this, skipPC));			}		}		sawRead = false;		sawSkip = false;	}}	

⌨️ 快捷键说明

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