Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * fb-contrib - Auxiliary detectors for Java programs
   * Copyright (C) 2005-2015 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
  * 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;
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 {
 	private static final Set<StringqueryMethods = new HashSet<String>();
 	static {
 	private final BugReporter bugReporter;
constructs a SIL detector given the reporter to report bugs on

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

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

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

obj the context object for the currently parsed code block
 	public void visitCode(Code obj) {
 		for (Integer qLoc : ) {
 			for (LoopLocation lLoc : ) {
 				if (lLoc.isInLoop(qLoc.intValue())) {

implements the visitor to collect positions of queries and loops

seen the opcode of the currently parsed instruction
	public void sawOpcode(int seen) {
		if (seen == ) {
			String methodName = getNameConstantOperand();
			if (.contains(clsName) && .contains(methodName))
else if ((seen == ) || (seen == )) {
			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;
		LoopLocation(int startint end) {
			 = start;
			 = end;
		boolean isInLoop(int pc) {
			return (pc >= ) && (pc <= );
		public String toString() {
New to GrepCode? Check out our FAQ X