[Fb-contrib-commit] fb-contrib/src/com/mebigfatguy/fbcontrib/detect BloatedSynchronizedBlock.java,1.
Brought to you by:
dbrosius
|
From: Dave B. <dbr...@us...> - 2006-01-07 05:29:53
|
Update of /cvsroot/fb-contrib/fb-contrib/src/com/mebigfatguy/fbcontrib/detect In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23245/src/com/mebigfatguy/fbcontrib/detect Modified Files: BloatedSynchronizedBlock.java Log Message: accessing a static field that is synchronized on, is also unsafe Index: BloatedSynchronizedBlock.java =================================================================== RCS file: /cvsroot/fb-contrib/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/BloatedSynchronizedBlock.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- BloatedSynchronizedBlock.java 4 Jan 2006 02:40:42 -0000 1.7 +++ BloatedSynchronizedBlock.java 7 Jan 2006 05:29:43 -0000 1.8 @@ -27,6 +27,7 @@ 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.StatelessDetector; @@ -36,6 +37,7 @@ private static final String BSB_MIN_SAFE_CODE_SIZE = "fb-contrib.bsb.minsize"; private OpcodeStack stack = new OpcodeStack(); private Set<Integer> unsafeAliases = new HashSet<Integer>(); + private Set<String> unsafeFields = new HashSet<String>(); private int syncPC; private int lastPC; private boolean isStatic; @@ -75,6 +77,7 @@ lastPC = -1; isStatic = obj.isStatic(); unsafeAliases.clear(); + unsafeFields.clear(); thisCallOccurred = false; stack.resetForMethodEntry(this); } @@ -119,7 +122,15 @@ int monitorReg = itm.getRegisterNumber(); if (monitorReg >= 0) { unsafeAliases.add(new Integer(monitorReg)); - } + } else { + FieldAnnotation fa = itm.getField(); + if (fa != null) { + String name = fa.getFieldName(); + if (name != null) + unsafeFields.add(name); + } + } + } } } @@ -130,6 +141,8 @@ unsafe |= (!isStatic) && ((seen == ALOAD_0) || (seen == ASTORE_0)); int aloadReg = aloadReg(seen); unsafe |= (aloadReg >= 0) && unsafeAliases.contains(new Integer(aloadReg)); + String getStaticName = getStaticName(seen); + unsafe |= (getStaticName != null) && unsafeFields.contains(getStaticName); if (unsafe) { if ((getPC() - syncPC) > minSafeCodeLength) { bugReporter.reportBug(new BugInstance(this, "BSB_BLOATED_SYNCHRONIZED_BLOCK", NORMAL_PRIORITY) @@ -175,4 +188,21 @@ return seen - ASTORE_0; return -1; } + + /** + * returns the name of the field that is loaded with GETSTATIC, or null if not the getstatic instruction + * + * @param seen the currently visited opcode + * + * @return the fieldname used if it's a getstatic instruction, or null otherwise + */ + public String getStaticName(int seen) { + if (seen == GETSTATIC) { + FieldAnnotation fa = FieldAnnotation.fromReferencedField(this); + if (fa != null) + return fa.getFieldName(); + } + return null; + } + } |