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.List;
 import java.util.Set;
 
 import  org.apache.bcel.classfile.Code;
 
 import  edu.umd.cs.findbugs.BugInstance;
 import  edu.umd.cs.findbugs.BugReporter;
 import  edu.umd.cs.findbugs.BytecodeScanningDetector;
 import  edu.umd.cs.findbugs.ba.ClassContext;

looks for the execution of sql queries inside a loop. This pattern tends to be inefficient, and often can be improved upon, by collecting all the keys needed for the query and issuing just one query using an in clause with all the keys for all the queries previously needed in the loop.
 
 public class SQLInLoop extends BytecodeScanningDetector 
 {
 	private static final Set<StringqueryClasses = new HashSet<String>();
 	static {
 		.add("java/sql/Statement");
 		.add("java/sql/PreparedStatement");
 		.add("java/sql/CallableStatement");
 	}
 	private static final Set<StringqueryMethods = new HashSet<String>();
 	static {
 		.add("execute");
 		.add("executeQuery");
 	}
 	
 	private final BugReporter bugReporter;
constructs a SIL detector given the reporter to report bugs on

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

implements the visitor to create and clear the query locations and loops collections

Parameters:
classContext the context object for the currently parsed java class
 
 	public void visitClassContext(ClassContext classContext) {
 		try {
 			super.visitClassContext(classContext);
 		} finally {
 			 = null;
 			 = null;
 		}
 	}

implements the visitor to clear the collections, and report the query locations that are in loops

Parameters:
obj the context object for the currently parsed code block
 
 	public void visitCode(Code obj) {
 		super.visitCode(obj);
 		for (Integer qLoc : ) {
 			for (LoopLocation lLoc : ) {
 				if (lLoc.isInLoop(qLoc.intValue())) {
 					.reportBug(new BugInstance(this"SIL_SQL_IN_LOOP", NORMAL_PRIORITY)
 							   .addClass(this)
 							   .addMethod(this)
 							   .addSourceLine(thisqLoc.intValue()));
 					break;
 				}
			}
		}
	}

implements the visitor to collect positions of queries and loops

Parameters:
seen the opcode of the currently parsed instruction
	public void sawOpcode(int seen) {
		if (seen == INVOKEINTERFACE) {
			String methodName = getNameConstantOperand();
			if (.contains(clsName) && .contains(methodName))
else if ((seen == GOTO) || (seen == GOTO_W)) {
			int branchTarget = getBranchTarget();
			int pc = getPC();
			if (branchTarget < pc) {
				.add(new LoopLocation(branchTargetpc));
			}
		}
	}

holds the start and end position of a loop
	private static class LoopLocation {
		private final int startPC;
		private final int endPC;
		public LoopLocation(int startint end) {
			 = start;
			 = end;
		}
		public boolean isInLoop(int pc) {
			return (pc >= ) && (pc <= );
		}
	}
New to GrepCode? Check out our FAQ X