[Fb-contrib-commit] fb-contrib/src/com/mebigfatguy/fbcontrib/detect FieldCouldBeLocal.java,1.3,1.4
Brought to you by:
dbrosius
|
From: Dave B. <dbr...@us...> - 2006-03-28 02:19:59
|
Update of /cvsroot/fb-contrib/fb-contrib/src/com/mebigfatguy/fbcontrib/detect In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21568/src/com/mebigfatguy/fbcontrib/detect Modified Files: FieldCouldBeLocal.java Log Message: add sourcelineannotation for FCBL where possible Index: FieldCouldBeLocal.java =================================================================== RCS file: /cvsroot/fb-contrib/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/FieldCouldBeLocal.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- FieldCouldBeLocal.java 27 Mar 2006 06:26:18 -0000 1.3 +++ FieldCouldBeLocal.java 28 Mar 2006 02:19:55 -0000 1.4 @@ -40,6 +40,7 @@ import edu.umd.cs.findbugs.BugReporter; import edu.umd.cs.findbugs.BytecodeScanningDetector; import edu.umd.cs.findbugs.FieldAnnotation; +import edu.umd.cs.findbugs.SourceLineAnnotation; import edu.umd.cs.findbugs.StatelessDetector; import edu.umd.cs.findbugs.ba.BasicBlock; import edu.umd.cs.findbugs.ba.CFG; @@ -55,7 +56,7 @@ private BugReporter bugReporter; private ClassContext clsContext; - private Map<String, FieldAnnotation> localizableFields = new HashMap<String, FieldAnnotation>(); + private Map<String, FieldInfo> localizableFields = new HashMap<String, FieldInfo>(); private CFG cfg; private ConstantPoolGen cpg; private BitSet visitedBlocks = new BitSet(); @@ -96,16 +97,21 @@ for (Field f : fields) { if ((!f.isStatic() && f.getName().indexOf("$") < 0) && (f.isPrivate() || (clsIsFinal && f.isProtected()))) { FieldAnnotation fa = new FieldAnnotation(cls.getClassName(), f.getName(), f.getSignature(), false); - localizableFields.put(f.getName(), fa); + localizableFields.put(f.getName(), new FieldInfo(fa)); } } if (localizableFields.size() > 0) { super.visitClassContext(classContext); - for (FieldAnnotation fa : localizableFields.values()) { - bugReporter.reportBug(new BugInstance(this, "FCBL_FIELD_COULD_BE_LOCAL", NORMAL_PRIORITY) - .addClass(this) - .addField(fa)); + for (FieldInfo fi : localizableFields.values()) { + FieldAnnotation fa = fi.getFieldAnnotation(); + SourceLineAnnotation sla = fi.getSrcLineAnnotation(); + BugInstance bug = new BugInstance(this, "FCBL_FIELD_COULD_BE_LOCAL", NORMAL_PRIORITY) + .addClass(this) + .addField(fa); + if (sla != null) + bug.addSourceLine(sla); + bugReporter.reportBug(bug); } } } @@ -164,10 +170,15 @@ FieldInstruction fi = (FieldInstruction) ins; String fieldName = fi.getFieldName(cpg); uncheckedFields.remove(fieldName); + if (ins instanceof GETFIELD) { localizableFields.remove(fieldName); if (localizableFields.size() == 0) return; + } else { + FieldInfo finfo = localizableFields.get(fieldName); + if (finfo != null) + finfo.setSrcLineAnnotation(SourceLineAnnotation.fromVisitedInstruction(clsContext, this, ih.getPosition())); } if (uncheckedFields.size() == 0) return; @@ -188,4 +199,27 @@ } } } + + private static class FieldInfo { + private FieldAnnotation fieldAnnotation; + private SourceLineAnnotation srcLineAnnotation; + + public FieldInfo(FieldAnnotation fa) { + fieldAnnotation = fa; + srcLineAnnotation = null; + } + + public void setSrcLineAnnotation(SourceLineAnnotation sla) { + if (srcLineAnnotation == null) + srcLineAnnotation = sla; + } + + public FieldAnnotation getFieldAnnotation() { + return fieldAnnotation; + } + + public SourceLineAnnotation getSrcLineAnnotation() { + return srcLineAnnotation; + } + } } |