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
  * 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.Map;
 import java.util.Set;
 
 
 
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.
 
 {
 	private static Set<StringsetMethods = new HashSet<String>(6);
 	private static Set<StringlistMethods = new HashSet<String>(8);
 	static {
 		.add("contains(Ljava/lang/Object;)Z");
 		.add("containsAll(Ljava/util/Collection;)Z");
 		.add("remove(Ljava/lang/Object;)Ljava/lang/Object;");
 		.add("removeAll(Ljava/util/Collection;)Z");
 		.add("retainAll(Ljava/util/Collection;)Z");
 		
 		.add("add(ILjava/lang/Object;)V");
 		.add("addAll(ILjava/util/Collection;)Z");
 		.add("lastIndexOf(Ljava/lang/Object;)I");
 		.add("remove(I)Ljava/lang/Object;");
 		.add("set(ILjava/lang/Object;)Ljava/lang/Object;");
 		.add("subList(II)Ljava/util/List;");
 		.add("listIterator()Ljava/util/ListIterator;");
 		.add("listIterator(I)Ljava/util/ListIterator;");
 		
 		//Theoretically get(i) and indexOf(Object) are list Methods but are so abused, as to be meaningless
 	}
 	private final BugReporter bugReporter;
 	private final OpcodeStack stack = new OpcodeStack();
 	private final Map<StringFieldInfofieldsReported = new HashMap<StringFieldInfo>(10);

constructs a DLC detector given the reporter to report bugs on

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

overrides the visitor to accept classes that define List based fields

Parameters:
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")) {
 				}
			}
		}
		if (.size() > 0) {
			super.visitClassContext(classContext);
		}
	}

overrides the visitor to reset the opcode stack object

Parameters:
obj the code object for the currently parse method
	public void visitCode(final Code obj) {
		super.visitCode(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

Parameters:
seen the current opcode parsed.
	public void sawOpcode(final int seen) {
		try {
	        .precomputation(this);
			if (seen == ) {
				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 == ) {
				if (className.startsWith("java/util/") && className.endsWith("List")) {
					if (field != null) {
						String fieldName = field.getName();
					}
				}
else if (seen == ) {
				if (.getStackDepth() > 0) {
					XField field = item.getXField();
					if (field != null) {
						String fieldName = field.getName();
					}
				}
			}
finally {
			.sawOpcode(thisseen);
		}
	}

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

Parameters:
stk the opcode stack where fields are stored
signature the signature of the called method
Returns:
the field annotation for the field whose method was executed
	private static 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() {
		for (Map.Entry<StringFieldInfoentry : .entrySet()) {
			String field = entry.getKey();
			FieldInfo fi = entry.getValue();
			int cnt = fi.getSetCount();
			if (cnt > 0) {
				if (fa != null) {
					//can't use LinkedHashSet in 1.3 so report at LOW
							.addClass(this)
				}
			}
		}
	}

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

Parameters:
fieldName the field for which to built the field annotation
Returns:
the field annotation of the specified field
	private FieldAnnotation getFieldAnnotation(final String fieldName) {
		Field[] fields = cls.getFields();
		for (Field f : fields) {
			if (f.getName().equals(fieldName))
				return new FieldAnnotation(cls.getClassName(), fieldNamef.getSignature(), (f.getAccessFlags() & .) != 0);
		}
		return null//shouldn't happen
	}
	class FieldInfo
	{
		private int setCnt = 0;
		public FieldInfo()
		{
		}
		public void addUse(final int pc)
		{
			if ( == null)
		}
		{
			return ;
		}
		public int getSetCount()
		{
			return ;
		}
		public String toString() {
			return ToString.build(this);
		}
	}
New to GrepCode? Check out our FAQ X