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  org.apache.bcel.classfile.Code;
 import  org.apache.bcel.generic.Type;
 
 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 methods that use arrays for items in the keyset of a map, or as an element of a set, or in a list when using the contains method. Since arrays do not, and cannot define an equals method, reference equality is used for these collections, which is probably not desired.
 
 public class ArrayBasedCollections extends BytecodeScanningDetector
 {	
 	private BugReporter bugReporter;
 	private OpcodeStack stack;
 	private List<BugInstance> mapBugs;
 	private List<BugInstance> setBugs;
 	private boolean hasMapComparator;
 	private boolean hasSetComparator;

    
constructs a ABC detector given the reporter to report bugs on

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

implement the visitor to report bugs if no Tree comparators were found

Parameters:
classContext the context object for the class currently being parsed
 
 	public void visitClassContext(ClassContext classContext) {
 		try {
 			 = new OpcodeStack();
 			 = new ArrayList<BugInstance>();
 			 = new ArrayList<BugInstance>();
 			 = false;
 			 = false;
 			super.visitClassContext(classContext);
 			if (!) {
 				for (BugInstance bi : ) {
 					.reportBug(bi);
 				}
 			}
 			
 			if (!) {
 				for (BugInstance bi : ) {
 					.reportBug(bi);
 				}
 			}
 		} finally {
 			 = null;
 			 = null;
 			 = null;
 		}
 	}

implements the visitor to reset the stack of opcodes

Parameters:
obj the context object for the currently parsed code block
 
 	public void visitCode(Code obj) {
 		.resetForMethodEntry(this);
 		super.visitCode(obj);
 	}

implements the visitor to find accesses to maps, sets and lists using arrays

Parameters:
seen the currently visitor opcode
	public void sawOpcode(int seen) {
		try {
			.mergeJumps(this);
			if (seen == INVOKEINTERFACE) {
				String methodName = getNameConstantOperand();
				String methodSig = getSigConstantOperand();
				boolean found = false;
				List<BugInstance> bugList = null;
				if ("java/util/Map".equals(className)
				 && "put".equals(methodName)
				 && "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;".equals(methodSig)) {
					if (.getStackDepth() > 1) {
						OpcodeStack.Item itm = .getStackItem(1);
						String pushedSig = itm.getSignature();
						if ((pushedSig.length() > 0) && (pushedSig.charAt(0) == '[')) {
							bugList = ;
							found = true;
						}
					}
else if ("java/util/Set".equals(className)
				        && "add".equals(methodName)
					    && "(Ljava/lang/Object;)Z".equals(methodSig)) {
					if (.getStackDepth() > 0) {
						OpcodeStack.Item itm = .getStackItem(0);
						String pushedSig = itm.getSignature();
						if (pushedSig.charAt(0) == '[') {
							bugList = ;
							found = true;
						}
					}
else if ("java/util/List".equals(className)
						&& "contains".equals(methodName)
						&& "(Ljava/lang/Object;)Z".equals(methodSig)) {
					if (.getStackDepth() > 0) {
						OpcodeStack.Item itm = .getStackItem(0);
						String pushedSig = itm.getSignature();
						if (pushedSig.charAt(0) == '[')
							found = true;
					}
				}
				if (found) {
					BugInstance bi = new BugInstance(this"ABC_ARRAY_BASED_COLLECTIONS", NORMAL_PRIORITY)
											.addClass(this)
											.addMethod(this)
											.addSourceLine(this);
					if (bugList != null)
						bugList.add(bi);
					else
						.reportBug(bi);
				}
else if (seen == INVOKESPECIAL) {
				String methodName = getNameConstantOperand();
				if ("<init>".equals(methodName)) {
					if (! && "java/util/TreeMap".equals(className)) {
						Type[] parms = Type.getArgumentTypes(sig);
						if ((parms.length == 1) && "Ljava/util/Comparator;".equals(parms[0].getSignature()))
else if (! && "java/util/TreeSet".equals(className)) {
						Type[] parms = Type.getArgumentTypes(sig);
						if ((parms.length == 1) && "Ljava/util/Comparator;".equals(parms[0].getSignature()))
					}
				}
			}
finally {
			.sawOpcode(thisseen);
		}
	}
New to GrepCode? Check out our FAQ X