[Fb-contrib-commit] fb-contrib/src/com/mebigfatguy/fbcontrib/detect LocalSynchronizedCollection.java
Brought to you by:
dbrosius
From: Dave B. <dbr...@us...> - 2006-03-11 15:15:22
|
Update of /cvsroot/fb-contrib/fb-contrib/src/com/mebigfatguy/fbcontrib/detect In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11819/src/com/mebigfatguy/fbcontrib/detect Modified Files: LocalSynchronizedCollection.java Log Message: mark LSYC bugs where the collection is never passed as a parameter as high, otherwise low. Index: LocalSynchronizedCollection.java =================================================================== RCS file: /cvsroot/fb-contrib/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/LocalSynchronizedCollection.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- LocalSynchronizedCollection.java 11 Mar 2006 06:25:40 -0000 1.2 +++ LocalSynchronizedCollection.java 11 Mar 2006 15:15:19 -0000 1.3 @@ -24,6 +24,7 @@ import java.util.Set; import org.apache.bcel.classfile.Code; +import org.apache.bcel.generic.Type; import edu.umd.cs.findbugs.BugInstance; import edu.umd.cs.findbugs.BugReporter; @@ -55,7 +56,7 @@ private BugReporter bugReporter; private OpcodeStack stack = new OpcodeStack(); - private Map<Integer, SourceLineAnnotation> syncRegs = new HashMap<Integer, SourceLineAnnotation>(); + private Map<Integer, CollectionRegInfo> syncRegs = new HashMap<Integer, CollectionRegInfo>(); /** * constructs a LSYC detector given the reporter to report bugs on @@ -86,11 +87,12 @@ syncRegs.clear(); super.visitCode(obj); - for (Map.Entry<Integer, SourceLineAnnotation> entry : syncRegs.entrySet()) { - bugReporter.reportBug(new BugInstance(this, "LSYC_LOCAL_SYNCHRONIZED_COLLECTION", NORMAL_PRIORITY) + for (Map.Entry<Integer, CollectionRegInfo> entry : syncRegs.entrySet()) { + CollectionRegInfo cri = entry.getValue(); + bugReporter.reportBug(new BugInstance(this, "LSYC_LOCAL_SYNCHRONIZED_COLLECTION", cri.getPriority()) .addClass(this) .addMethod(this) - .addSourceLine(entry.getValue())); + .addSourceLine(cri.getSourceLineAnnotation())); } } @@ -123,8 +125,11 @@ OpcodeStack.Item item = stack.getStackItem(0); if (item.getUserValue() != null) { int reg = getAStoreReg(seen); - if (!syncRegs.containsKey(reg)) - syncRegs.put(new Integer(reg), SourceLineAnnotation.fromVisitedInstruction(this)); + if (!syncRegs.containsKey(reg)) { + CollectionRegInfo cri = new CollectionRegInfo(SourceLineAnnotation.fromVisitedInstruction(this)); + syncRegs.put(new Integer(reg), cri); + + } } } } else if ((seen == ALOAD) || ((seen >= ALOAD_0) && (seen <= ALOAD_3))) { @@ -136,7 +141,25 @@ OpcodeStack.Item item = stack.getStackItem(0); syncRegs.remove((Integer)item.getUserValue()); } - } + } + + if (syncRegs.size() > 0) { + if ((seen == INVOKESPECIAL) + || (seen == INVOKEINTERFACE) + || (seen == INVOKEVIRTUAL) + || (seen == INVOKESTATIC)) { + String sig = getSigConstantOperand(); + int argCount = Type.getArgumentTypes(sig).length; + if (stack.getStackDepth() >= argCount) { + for (int i = 0; i < argCount; i++) { + OpcodeStack.Item item = stack.getStackItem(i); + CollectionRegInfo cri = syncRegs.get(item.getUserValue()); + if (cri != null) + cri.setPriority(LOW_PRIORITY); + } + } + } + } } finally { stack.sawOpcode(this, seen); if (tosIsSyncColReg != null) { @@ -171,4 +194,26 @@ return getRegisterOperand(); return seen - ALOAD_0; } + + static class CollectionRegInfo + { + private SourceLineAnnotation slAnnotation; + private int priority = HIGH_PRIORITY; + + public CollectionRegInfo(SourceLineAnnotation sla) { + slAnnotation = sla; + } + public SourceLineAnnotation getSourceLineAnnotation() { + return slAnnotation; + } + + public void setPriority(int newPriority) { + if (newPriority > priority) + priority = newPriority; + } + + public int getPriority() { + return priority; + } + } } |