Thread: [Fb-contrib-commit] SF.net SVN: fb-contrib: [512] trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/det
Brought to you by:
dbrosius
From: <dbr...@us...> - 2006-05-01 02:53:50
|
Revision: 512 Author: dbrosius Date: 2006-04-30 19:53:42 -0700 (Sun, 30 Apr 2006) ViewCVS: http://svn.sourceforge.net/fb-contrib/?rev=512&view=rev Log Message: ----------- stop reporting the same bug on fields more than once Modified Paths: -------------- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/UseEnumCollections.java Modified: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/UseEnumCollections.java =================================================================== --- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/UseEnumCollections.java 2006-05-01 01:17:28 UTC (rev 511) +++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/UseEnumCollections.java 2006-05-01 02:53:42 UTC (rev 512) @@ -1,5 +1,8 @@ package com.mebigfatguy.fbcontrib.detect; +import java.util.HashSet; +import java.util.Set; + import org.apache.bcel.Constants; import org.apache.bcel.classfile.JavaClass; import org.apache.bcel.classfile.Method; @@ -7,6 +10,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.ba.ClassContext; @@ -18,6 +22,7 @@ { private BugReporter bugReporter; private OpcodeStack stack; + private Set<String> checkedFields; private boolean methodReported; /** * constructs a UEC detector given the reporter to report bugs on @@ -37,10 +42,12 @@ JavaClass cls = classContext.getJavaClass(); if (cls.getMajor() >= Constants.MAJOR_1_5) { stack = new OpcodeStack(); + checkedFields = new HashSet<String>(); super.visitClassContext(classContext); } } finally { stack = null; + checkedFields = null; } } @@ -70,11 +77,11 @@ if (("java/util/Map".equals(clsName)) && ("put".equals(methodName)) && ("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;".equals(signature))) { - bug = isEnum(1); + bug = isEnum(1) && alreadyReported(2); } else if (("java/util/Set".equals(clsName)) && ("add".equals(methodName)) && ("(Ljava/lang/Object;)Z".equals(signature))) { - bug = isEnum(0); + bug = isEnum(0) && alreadyReported(1); } if (bug) { @@ -92,6 +99,14 @@ } } + /** + * returns whether the item at the stackPos location on the stack + * + * @param stackPos the position on the opstack to check + * + * @return whether the class is an enum + * @throws ClassNotFoundException if the class can not be loaded + */ private boolean isEnum(int stackPos) throws ClassNotFoundException { if (stack.getStackDepth() > stackPos) { OpcodeStack.Item item = stack.getStackItem(stackPos); @@ -100,4 +115,27 @@ } return false; } + + /** + * returns whether the collection has already been reported on + * + * @param stackPos the position on the opstack to check + * + * @return whether the collection has already been reported. + */ + private boolean alreadyReported(int stackPos) { + if (stack.getStackDepth() > stackPos) { + OpcodeStack.Item item = stack.getStackItem(stackPos); + FieldAnnotation fa = item.getField(); + if (fa == null) + return true; + else { + String fieldName = fa.getFieldName(); + boolean checked = checkedFields.contains(fieldName); + checkedFields.add(fieldName); + return !checked; + } + } + return false; + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dbr...@us...> - 2006-05-05 05:56:08
|
Revision: 518 Author: dbrosius Date: 2006-05-04 22:56:04 -0700 (Thu, 04 May 2006) ViewCVS: http://svn.sourceforge.net/fb-contrib/?rev=518&view=rev Log Message: ----------- copyright Modified Paths: -------------- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/UseEnumCollections.java Modified: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/UseEnumCollections.java =================================================================== --- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/UseEnumCollections.java 2006-05-05 05:55:31 UTC (rev 517) +++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/UseEnumCollections.java 2006-05-05 05:56:04 UTC (rev 518) @@ -1,3 +1,21 @@ +/* + * fb-contrib - Auxilliary detectors for Java programs + * Copyright (C) 2005-2006 Dave Brosius + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ package com.mebigfatguy.fbcontrib.detect; import java.util.HashSet; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |