Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * fb-contrib - Auxiliary detectors for Java programs
   * Copyright (C) 2005-2012 Dave Brosius
   * 
   * 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 com.mebigfatguy.fbcontrib.detect;
 
 import java.util.Set;
 
 import  org.apache.bcel.Constants;
 import  org.apache.bcel.classfile.JavaClass;
 import  org.apache.bcel.classfile.Method;
 
 import  edu.umd.cs.findbugs.BugInstance;
 import  edu.umd.cs.findbugs.BugReporter;
 import  edu.umd.cs.findbugs.BytecodeScanningDetector;
 import  edu.umd.cs.findbugs.OpcodeStack;
 import  edu.umd.cs.findbugs.ba.ClassContext;

looks for calls to the wait method on mutexes defined in the java.util.concurrent package where it is likely that await was intended.
 
 public class SuspiciousWaitOnConcurrentObject extends BytecodeScanningDetector
 {
 	private static final Set<StringconcurrentAwaitClasses = new HashSet<String>();
 	static {
 		.add("java.util.concurrent.CountDownLatch");
 		.add("java.util.concurrent.CyclicBarrier");
 	}
 	
 	private BugReporter bugReporter;
 	private OpcodeStack stack;

constructs a SWCO detector given the reporter to report bugs on

Parameters:
bugReporter the sync of bug reports
 
 	public SuspiciousWaitOnConcurrentObject(BugReporter bugReporter) {
 		this. = bugReporter;
 	}

implements the visitor to check for class file version 1.5 or better

Parameters:
classContext the context object of the currently parsed class
 
 	public void visitClassContext(ClassContext classContext) {
 		try {
 			JavaClass cls = classContext.getJavaClass();
 			int major = cls.getMajor();
 			if (major >= Constants.MAJOR_1_5) {
 				 = new OpcodeStack();
 				super.visitClassContext(classContext);
 			}
 		} finally {
 			 = null;
 		}
 	}

implements the visitor to reset the opcode stack

Parameters:
obj the context object for the currently parsed method
 
 	public void visitMethod(Method obj) {
 		.resetForMethodEntry(this);
 	}

implements the visitor to look for calls to wait, on java.util.concurrent classes that define await.

Parameters:
seen the opcode of the currently visited instruction
 
 	public void sawOpcode(int seen) {
 		try {
 			.mergeJumps(this);
 			
 			if (seen == INVOKEVIRTUAL) {
 				String methodName = getNameConstantOperand();
 				if ("wait".equals(methodName)) {
					if (.getStackDepth() > 0) {
						OpcodeStack.Item itm = .getStackItem(0);
						JavaClass cls = itm.getJavaClass();
						if (cls != null) {
							String clsName = cls.getClassName();
								.reportBug(new BugInstance(this"SWCO_SUSPICIOUS_WAIT_ON_CONCURRENT_OBJECT", NORMAL_PRIORITY)
											.addClass(this)
											.addMethod(this)
											.addSourceLine(this));
							}
						}
					}
				}
			}
catch (ClassNotFoundException cnfe) {
			.reportMissingClass(cnfe);
finally {
			.sawOpcode(thisseen);
		}
	}
New to GrepCode? Check out our FAQ X