[Fb-contrib-commit] SF.net SVN: fb-contrib:[1552] trunk/fb-contrib
Brought to you by:
dbrosius
|
From: <dbr...@us...> - 2010-05-08 21:34:26
|
Revision: 1552
http://fb-contrib.svn.sourceforge.net/fb-contrib/?rev=1552&view=rev
Author: dbrosius
Date: 2010-05-08 21:34:19 +0000 (Sat, 08 May 2010)
Log Message:
-----------
improve WOC handling of collections that are fields
Modified Paths:
--------------
trunk/fb-contrib/etc/findbugs.xml
trunk/fb-contrib/etc/messages.xml
trunk/fb-contrib/samples/WOC_Sample.java
trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/WriteOnlyCollection.java
Modified: trunk/fb-contrib/etc/findbugs.xml
===================================================================
--- trunk/fb-contrib/etc/findbugs.xml 2010-05-08 04:30:13 UTC (rev 1551)
+++ trunk/fb-contrib/etc/findbugs.xml 2010-05-08 21:34:19 UTC (rev 1552)
@@ -334,7 +334,7 @@
<Detector class="com.mebigfatguy.fbcontrib.detect.WriteOnlyCollection"
speed="fast"
- reports="WOC_WRITE_ONLY_COLLECTION" />
+ reports="WOC_WRITE_ONLY_COLLECTION_LOCAL,WOC_WRITE_ONLY_COLLECTION_FIELD" />
<!-- BugPattern -->
@@ -631,6 +631,8 @@
category="CORRECTNESS" experimental="true" />
<BugPattern abbrev="PCAIL" type="PCAIL_POSSIBLE_CONSTANT_ALLOCATION_IN_LOOP"
category="PERFORMANCE" experimental="true" />
- <BugPattern abbrev="WOC" type="WOC_WRITE_ONLY_COLLECTION"
+ <BugPattern abbrev="WOC" type="WOC_WRITE_ONLY_COLLECTION_LOCAL"
category="CORRECTNESS" experimental="true" />
+ <BugPattern abbrev="WOC" type="WOC_WRITE_ONLY_COLLECTION_FIELD"
+ category="CORRECTNESS" experimental="true" />
</FindbugsPlugin>
\ No newline at end of file
Modified: trunk/fb-contrib/etc/messages.xml
===================================================================
--- trunk/fb-contrib/etc/messages.xml 2010-05-08 04:30:13 UTC (rev 1551)
+++ trunk/fb-contrib/etc/messages.xml 2010-05-08 21:34:19 UTC (rev 1552)
@@ -3119,7 +3119,7 @@
</Details>
</BugPattern>
- <BugPattern type="WOC_WRITE_ONLY_COLLECTION">
+ <BugPattern type="WOC_WRITE_ONLY_COLLECTION_LOCAL">
<ShortDescription>Method creates and initializes a collection but never reads or gains information from it</ShortDescription>
<LongDescription>Method {1} creates and initializes a collection but never reads or gains information from it</LongDescription>
<Details>
@@ -3130,6 +3130,18 @@
]]>
</Details>
</BugPattern>
+
+ <BugPattern type="WOC_WRITE_ONLY_COLLECTION_FIELD">
+ <ShortDescription>Class creates and initializes a collection but never reads or gains information from it</ShortDescription>
+ <LongDescription>Class {0} creates and initializes a collection but never reads or gains information from it</LongDescription>
+ <Details>
+ <![CDATA[
+ <p>This class creates and initializes a collection as a field but then never access this collection
+ to gain information, or fetch items from the collection. It is likely that this collection
+ is left over from a past effort, and can be removed.
+ ]]>
+ </Details>
+ </BugPattern>
<!-- BugCode -->
Modified: trunk/fb-contrib/samples/WOC_Sample.java
===================================================================
--- trunk/fb-contrib/samples/WOC_Sample.java 2010-05-08 04:30:13 UTC (rev 1551)
+++ trunk/fb-contrib/samples/WOC_Sample.java 2010-05-08 21:34:19 UTC (rev 1552)
@@ -10,12 +10,16 @@
public class WOC_Sample
{
private Set<String> memberSet = new HashSet<String>();
+ private Set<String> fpSet;
public void testWOCSimple()
{
Set<String> s = new HashSet<String>();
s.add("Foo");
memberSet.add("fee");
+ if (fpSet.retainAll(new HashSet<String>())) {
+ System.out.println("woops");
+ }
}
public Map<String, String> testFPWOCReturn()
@@ -23,6 +27,7 @@
Map<String, String> m = new HashMap<String, String>();
m.put("Foo", "Bar");
memberSet.add("fi");
+ fpSet = new HashSet<String>();
return m;
}
@@ -31,6 +36,7 @@
Map<String, String> m = new HashMap<String, String>();
m.put("Foo", "Bar");
memberSet.add("fo");
+ fpSet.add("boo");
helper(0, m);
}
Modified: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/WriteOnlyCollection.java
===================================================================
--- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/WriteOnlyCollection.java 2010-05-08 04:30:13 UTC (rev 1551)
+++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/WriteOnlyCollection.java 2010-05-08 21:34:19 UTC (rev 1552)
@@ -19,20 +19,24 @@
package com.mebigfatguy.fbcontrib.detect;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
+import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.SortedMap;
+import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
-import org.apache.bcel.Constants;
import org.apache.bcel.classfile.Code;
+import org.apache.bcel.classfile.Field;
import org.apache.bcel.generic.Type;
import com.mebigfatguy.fbcontrib.utils.RegisterUtils;
@@ -41,7 +45,6 @@
import edu.umd.cs.findbugs.BugReporter;
import edu.umd.cs.findbugs.BytecodeScanningDetector;
import edu.umd.cs.findbugs.OpcodeStack;
-import edu.umd.cs.findbugs.OpcodeStack.Item;
import edu.umd.cs.findbugs.ba.ClassContext;
import edu.umd.cs.findbugs.ba.XField;
@@ -50,6 +53,12 @@
private static Set<String> collectionClasses = new HashSet<String>();
static
{
+ collectionClasses.add(Set.class.getName());
+ collectionClasses.add(Map.class.getName());
+ collectionClasses.add(List.class.getName());
+ collectionClasses.add(SortedSet.class.getName());
+ collectionClasses.add(SortedMap.class.getName());
+ collectionClasses.add(Collection.class.getName());
collectionClasses.add(HashSet.class.getName());
collectionClasses.add(TreeSet.class.getName());
collectionClasses.add(LinkedHashSet.class.getName());
@@ -91,8 +100,8 @@
private OpcodeStack stack;
/** register to first allocation PC */
private Map<Integer, Integer> localWOCollections;
- /** field to first allocation PC */
- private Map<String, Integer> fieldWOCollections;
+ /** fieldname to field sig */
+ private Map<String, String> fieldWOCollections;
/**
@@ -112,13 +121,18 @@
try {
stack = new OpcodeStack();
localWOCollections = new HashMap<Integer, Integer>();
- fieldWOCollections = new HashMap<String, Integer>();
+ fieldWOCollections = new HashMap<String, String>();
super.visitClassContext(classContext);
- for (Integer pc : fieldWOCollections.values()) {
- bugReporter.reportBug(new BugInstance(this, "WOC_WRITE_ONLY_COLLECTION", NORMAL_PRIORITY)
- .addClass(this)
- .addSourceLine(this, pc.intValue()));
+ if (fieldWOCollections.size() > 0) {
+ String clsName = classContext.getJavaClass().getClassName();
+ for (Map.Entry<String, String> entry : fieldWOCollections.entrySet()) {
+ String fieldName = entry.getKey();
+ String signature = entry.getValue();
+ bugReporter.reportBug(new BugInstance(this, "WOC_WRITE_ONLY_COLLECTION_FIELD", NORMAL_PRIORITY)
+ .addClass(this)
+ .addField(clsName, fieldName, signature, false));
+ }
}
} finally {
stack = null;
@@ -126,6 +140,19 @@
fieldWOCollections = null;
}
}
+
+ @Override
+ public void visitField(Field obj) {
+ if (obj.isPrivate()) {
+ String sig = obj.getSignature();
+ if (sig.startsWith("L")) {
+ String type = sig.substring(1, sig.length() - 1).replace('/', '.');
+ if (collectionClasses.contains(type)) {
+ fieldWOCollections.put(obj.getName(), obj.getSignature());
+ }
+ }
+ }
+ }
/**
* overrides the visitor reset the stack
@@ -139,8 +166,7 @@
super.visitCode(obj);
for (Integer pc : localWOCollections.values()) {
- bugReporter.reportBug(new BugInstance(this,
-"WOC_WRITE_ONLY_COLLECTION", NORMAL_PRIORITY)
+ bugReporter.reportBug(new BugInstance(this, "WOC_WRITE_ONLY_COLLECTION_LOCAL", NORMAL_PRIORITY)
.addClass(this)
.addMethod(this)
.addSourceLine(this, pc.intValue()));
@@ -246,20 +272,8 @@
if (stack.getStackDepth() > 1) {
OpcodeStack.Item item = stack.getStackItem(0);
Object uo = item.getUserValue();
- if (uo != null) {
- if (uo instanceof Boolean) {
- Item fieldItem = stack.getStackItem(1);
- boolean storedInThis = fieldItem.getRegisterNumber() == 0;
- if (storedInThis) {
- XField field = getXFieldOperand();
- if ((field.getAccessFlags() & Constants.ACC_PRIVATE) != 0) {
- String fieldName = field.getName();
- fieldWOCollections.put(fieldName, getPC());
- }
- }
- } else {
- clearUserValue(item);
- }
+ if (!(uo instanceof Boolean)) {
+ clearUserValue(item);
}
}
break;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|