[Fb-contrib-commit] SF.net SVN: fb-contrib: [525] trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/det
Brought to you by:
dbrosius
From: <dbr...@us...> - 2006-05-10 03:07:25
|
Revision: 525 Author: dbrosius Date: 2006-05-09 20:07:18 -0700 (Tue, 09 May 2006) ViewCVS: http://svn.sourceforge.net/fb-contrib/?rev=525&view=rev Log Message: ----------- start reporting bugs... it is now way to liberal and reports many false positives. No aliases checking yet. Modified Paths: -------------- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/NeedlessMemberCollectionSynchronization.java Modified: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/NeedlessMemberCollectionSynchronization.java =================================================================== --- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/NeedlessMemberCollectionSynchronization.java 2006-05-09 07:41:02 UTC (rev 524) +++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/NeedlessMemberCollectionSynchronization.java 2006-05-10 03:07:18 UTC (rev 525) @@ -27,7 +27,9 @@ 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; @@ -54,6 +56,29 @@ syncCollections.add("java/util/Vector"); syncCollections.add("java/util/Hashtable"); } + private static Set<String> modifyingMethods = new HashSet<String>(); + static { + modifyingMethods.add("add"); + modifyingMethods.add("addAll"); + modifyingMethods.add("addFirst"); + modifyingMethods.add("addElement"); + modifyingMethods.add("addLast"); + modifyingMethods.add("clear"); + modifyingMethods.add("insertElementAt"); + modifyingMethods.add("put"); + modifyingMethods.add("remove"); + modifyingMethods.add("removeAll"); + modifyingMethods.add("removeAllElements"); + modifyingMethods.add("removeElement"); + modifyingMethods.add("removeElementAt"); + modifyingMethods.add("removeFirst"); + modifyingMethods.add("removeLast"); + modifyingMethods.add("removeRange"); + modifyingMethods.add("retainAll"); + modifyingMethods.add("set"); + modifyingMethods.add("setElementAt"); + modifyingMethods.add("setSize"); + } private static final int IN_METHOD = 0; private static final int IN_CLINIT = 1; private static final int IN_INIT = 2; @@ -87,6 +112,17 @@ JavaClass cls = classContext.getJavaClass(); className = cls.getClassName(); super.visitClassContext(classContext); + for (FieldAnnotation fa : staticCollectionFields.values()) { + bugReporter.reportBug(new BugInstance(this, "NMCS_NEEDLESS_MEMBER_COLLECTION_SYNCHRONIZATION", NORMAL_PRIORITY) + .addClass(this) + .addField(fa)); + } + for (FieldAnnotation fa : instanceCollectionFields.values()) { + bugReporter.reportBug(new BugInstance(this, "NMCS_NEEDLESS_MEMBER_COLLECTION_SYNCHRONIZATION", NORMAL_PRIORITY) + .addClass(this) + .addField(fa)); + } + } } finally { staticCollectionFields = null; @@ -189,6 +225,56 @@ try { stack.mergeJumps(this); + switch (seen) { + case INVOKEVIRTUAL: + case INVOKEINTERFACE: + String methodName = getNameConstantOperand(); + if (modifyingMethods.contains(methodName)) { + String signature = getSigConstantOperand(); + int parmCount = Type.getArgumentTypes(signature).length; + if (stack.getStackDepth() > parmCount) { + OpcodeStack.Item item = stack.getStackItem(parmCount); + FieldAnnotation fa = item.getField(); + if (fa != null) { + if (fa.isStatic()) + staticCollectionFields.remove(fa.getFieldName()); + else + instanceCollectionFields.remove(fa.getFieldName()); + } else { + int reg = item.getRegisterNumber(); + if (reg >= 0) { + Integer register = new Integer(reg); + String fName = staticAliases.get(register); + if (fName != null) { + staticCollectionFields.remove(fName); + staticAliases.remove(register); + } else { + fName = instanceAliases.get(register); + if (fName != null) { + instanceCollectionFields.remove(fName); + instanceAliases.remove(register); + } + } + } + } + } + } + break; + + case ARETURN: + if (stack.getStackDepth() > 0) { + OpcodeStack.Item item = stack.getStackItem(0); + FieldAnnotation fa = item.getField(); + if (fa != null) { + if (fa.isStatic()) + staticCollectionFields.remove(fa.getFieldName()); + else + instanceCollectionFields.remove(fa.getFieldName()); + } + } + break; + } + //look for code that alters the collection, passes it to a method, returns it, etc //watch out for aliases } finally { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |