Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * fb-contrib - Auxiliary detectors for Java programs
   * Copyright (C) 2005-2013 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.Map;
 import java.util.Set;
 import  org.apache.bcel.Constants;
 import  org.apache.bcel.classfile.Code;
 import  org.apache.bcel.classfile.Field;
 import  org.apache.bcel.classfile.JavaClass;
 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.FieldAnnotation;
 import  edu.umd.cs.findbugs.OpcodeStack;
 import  edu.umd.cs.findbugs.SourceLineAnnotation;

looks for fields that are implementations of java.util.List, but that are used in a set-like fashion. Since lookup type operations are performed using a linear search for Lists, the performance for large Lists will be poor. Consideration should be made as to whether these fields should be sets. In the case that order is important, consider using LinkedHashSet.
 public class DubiousListCollection extends BytecodeScanningDetector
 	private static Set<StringsetMethods = new HashSet<String>(6);
 	private static Set<StringlistMethods = new HashSet<String>(8);
 	static {
 		//Theoretically get(i) and indexOf(Object) are list Methods but are so abused, as to be meaningless
 	private BugReporter bugReporter;
 	private OpcodeStack stack = new OpcodeStack();
 	private Map<StringFieldInfofieldsReported = new HashMap<StringFieldInfo>(10);

constructs a DLC detector given the reporter to report bugs on

bugReporter the sync of bug reports
 	public DubiousListCollection(final BugReporter bugReporter) {
 		this. = bugReporter;

overrides the visitor to accept classes that define List based fields

classContext the context object for the currently parsed class
 	public void visitClassContext(final ClassContext classContext) {
 		JavaClass cls = classContext.getJavaClass();
 		Field[] flds = cls.getFields();
 		for (Field f : flds) {
 			String sig = f.getSignature();
 			if (sig.charAt(0) == 'L') {
 				sig = sig.substring(1, sig.length() - 1);
 				if (sig.startsWith("java/util/") && sig.endsWith("List")) {
 					.put(f.getName(), new FieldInfo());
		if (.size() > 0) {

overrides the visitor to reset the opcode stack object

obj the code object for the currently parse method
	public void visitCode(final Code obj) {

overrides the visitor to record all method calls on List fields. If a method is not a set based method, remove it from further consideration

seen the current opcode parsed.
	public void sawOpcode(final int seen) {
		try {
			if (seen == INVOKEINTERFACE) {
				String className = this.getClassConstantOperand();
				if (className.startsWith("java/util/") && className.endsWith("List")) {
					String signature = getSigConstantOperand();
					XField field = getFieldFromStack(signature);
					if (field != null) {
						String fieldName = field.getName();
						FieldInfo fi = .get(fieldName);
						if (fi != null) {
							String methodName = getNameConstantOperand();
							String methodInfo = methodName + signature;
							if (.contains(methodInfo))
							else if (.contains(methodInfo))
else if (seen == INVOKEVIRTUAL) {
				if (className.startsWith("java/util/") && className.endsWith("List")) {
					if (field != null) {
						String fieldName = field.getName();
else if (seen == ARETURN) {
				if (.getStackDepth() > 0) {
					OpcodeStack.Item item = .getStackItem(0);
					XField field = item.getXField();
					if (field != null) {
						String fieldName = field.getName();
finally {

return the field object that the current method was called on, by finding the reference down in the stack based on the number of parameters

stk the opcode stack where fields are stored
signature the signature of the called method
the field annotation for the field whose method was executed
	private XField getFieldFromStack(final OpcodeStack stkfinal String signature) {
		int parmCount = Type.getArgumentTypes(signature).length;
		if (stk.getStackDepth() > parmCount) {
			OpcodeStack.Item itm = stk.getStackItem(parmCount);
			return itm.getXField();
		return null;

implements the detector, by reporting all remaining fields that only have set based access
	private void reportBugs() {
		int major = getClassContext().getJavaClass().getMajor();
		for (Map.Entry<StringFieldInfoentry : .entrySet()) {
			String field = entry.getKey();
			FieldInfo fi = entry.getValue();
			int cnt = fi.getSetCount();
			if (cnt > 0) {
				FieldAnnotation fa = getFieldAnnotation(field);
				if (fa != null) {
					//can't use LinkedHashSet in 1.3 so report at LOW
					.reportBug(new BugInstance(this"DLC_DUBIOUS_LIST_COLLECTION", (major >= MAJOR_1_4) ? NORMAL_PRIORITY : LOW_PRIORITY)

builds a field annotation by finding the field in the classes' field list

fieldName the field for which to built the field annotation
the field annotation of the specified field
	private FieldAnnotation getFieldAnnotation(final String fieldName) {
		JavaClass cls = getClassContext().getJavaClass();
		Field[] fields = cls.getFields();
		for (Field f : fields) {
			if (f.getName().equals(fieldName))
				return new FieldAnnotation(cls.getClassName(), fieldNamef.getSignature(), (f.getAccessFlags() & Constants.ACC_STATIC) != 0);
		return null//shouldn't happen
	class FieldInfo
		private int setCnt = 0;
		private SourceLineAnnotation slAnnotation = null;
		public FieldInfo()
		public void addUse(final int pc)
			if ( == null)
				 = SourceLineAnnotation.fromVisitedInstruction(DubiousListCollection.this.getClassContext(), DubiousListCollection.thispc);
		public SourceLineAnnotation getSourceLineAnnotation()
			return ;
		public int getSetCount()
			return ;
New to GrepCode? Check out our FAQ X