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

📄 findmismatchedwaitornotify.java

📁 A static analysis tool to find bugs in Java programs
💻 JAVA
字号:
/* * FindBugs - Find bugs in Java programs * Copyright (C) 2003-2005 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.*;import edu.umd.cs.findbugs.ba.*;import edu.umd.cs.findbugs.ba.vna.*;import java.util.*;import org.apache.bcel.Constants;import org.apache.bcel.classfile.*;import org.apache.bcel.generic.*;public final class FindMismatchedWaitOrNotify implements Detector, StatelessDetector {	private BugReporter bugReporter;	public FindMismatchedWaitOrNotify(BugReporter bugReporter) {		this.bugReporter = bugReporter;	}	@Override	public Object clone() {		try {			return super.clone();		} catch (CloneNotSupportedException e) {			throw new AssertionError(e);		}	}	public void visitClassContext(ClassContext classContext) {		JavaClass jclass = classContext.getJavaClass();		Method[] methodList = jclass.getMethods();		for (Method method : methodList) {			MethodGen methodGen = classContext.getMethodGen(method);			if (methodGen == null)				continue;			// Don't bother analyzing the method unless there is both locking			// and a method call.			BitSet bytecodeSet = classContext.getBytecodeSet(method);			if (bytecodeSet == null) continue;			if (!(bytecodeSet.get(Constants.MONITORENTER) && bytecodeSet.get(Constants.INVOKEVIRTUAL)))				continue;			try {				analyzeMethod(classContext, method);			} catch (DataflowAnalysisException e) {				bugReporter.logError("FindMismatchedWaitOrNotify: caught exception", e);			} catch (CFGBuilderException e) {				bugReporter.logError("FindMismatchedWaitOrNotify: caught exception", e);			}		}	}	private void analyzeMethod(ClassContext classContext, Method method)	throws CFGBuilderException, DataflowAnalysisException {		MethodGen methodGen = classContext.getMethodGen(method);		if (methodGen == null) return;		ConstantPoolGen cpg = methodGen.getConstantPool();		CFG cfg = classContext.getCFG(method);		ValueNumberDataflow vnaDataflow = classContext.getValueNumberDataflow(method);		LockDataflow dataflow = classContext.getLockDataflow(method);		for (Iterator<Location> i = cfg.locationIterator(); i.hasNext();) {			Location location = i.next();			InstructionHandle handle = location.getHandle();			Instruction ins = handle.getInstruction();			if (!(ins instanceof INVOKEVIRTUAL))				continue;			INVOKEVIRTUAL inv = (INVOKEVIRTUAL) ins;			String methodName = inv.getName(cpg);			String methodSig = inv.getSignature(cpg);			if (Hierarchy.isMonitorWait(methodName, methodSig)					|| Hierarchy.isMonitorNotify(methodName, methodSig)) {				int numConsumed = inv.consumeStack(cpg);				if (numConsumed == Constants.UNPREDICTABLE)					throw new DataflowAnalysisException("Unpredictable stack consumption", methodGen, handle);				ValueNumberFrame frame = vnaDataflow.getFactAtLocation(location);				if (!frame.isValid())					// Probably dead code					continue;				if (frame.getStackDepth() - numConsumed < 0)					throw new DataflowAnalysisException("Stack underflow", methodGen, handle);				ValueNumber ref = frame.getValue(frame.getNumSlots() - numConsumed);				LockSet lockSet = dataflow.getFactAtLocation(location);				int lockCount = lockSet.getLockCount(ref.getNumber());				if (lockCount == 0) {					Collection<ValueNumber> lockedValueNumbers = lockSet.getLockedValueNumbers(frame);					boolean foundMatch = false;					for(ValueNumber v : lockedValueNumbers) 						if (frame.fuzzyMatch(ref, v)){							foundMatch = true;							break;						}					if (!foundMatch) {						String type = methodName.equals("wait")						? "MWN_MISMATCHED_WAIT"								: "MWN_MISMATCHED_NOTIFY";						String sourceFile = classContext.getJavaClass().getSourceFileName();						// Report as medium priority only if the method is public.						// Non-public methods may be properly locked in a calling context.						int priority = method.isPublic() ? NORMAL_PRIORITY : LOW_PRIORITY;						bugReporter.reportBug(new BugInstance(this, type, priority)						.addClassAndMethod(methodGen, sourceFile)						.addSourceLine(classContext, methodGen, sourceFile, handle));					}				}			}		}	}	public void report() {	}}//vim:ts=3

⌨️ 快捷键说明

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