[Fb-contrib-commit] SF.net SVN: fb-contrib:[1191] trunk/fb-contrib
Brought to you by:
dbrosius
|
From: <dbr...@us...> - 2009-05-09 20:58:38
|
Revision: 1191
http://fb-contrib.svn.sourceforge.net/fb-contrib/?rev=1191&view=rev
Author: dbrosius
Date: 2009-05-09 20:58:29 +0000 (Sat, 09 May 2009)
Log Message:
-----------
add SMA
Modified Paths:
--------------
trunk/fb-contrib/etc/findbugs.xml
trunk/fb-contrib/etc/messages.xml
trunk/fb-contrib/samples/UNNC_Sample.java
Added Paths:
-----------
trunk/fb-contrib/samples/SMA_Sample.java
trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/StutteredMethodArguments.java
Modified: trunk/fb-contrib/etc/findbugs.xml
===================================================================
--- trunk/fb-contrib/etc/findbugs.xml 2009-05-09 20:21:13 UTC (rev 1190)
+++ trunk/fb-contrib/etc/findbugs.xml 2009-05-09 20:58:29 UTC (rev 1191)
@@ -344,6 +344,10 @@
speed="fast"
reports="DTEP_DEPRECATED_TYPESAFE_ENUM_PATTERN" />
+ <Detector class="com.mebigfatguy.fbcontrib.detect.StutteredMethodArguments"
+ speed="fast"
+ reports="SMA_STUTTERED_METHOD_ARGUMENTS" />
+
<!-- BugPattern -->
<BugPattern abbrev="ISB" type="ISB_INEFFICIENT_STRING_BUFFERING" category="PERFORMANCE" />
@@ -463,4 +467,5 @@
<BugPattern abbrev="BED" type="BED_BOGUS_EXCEPTION_DECLARATION" category="CORRECTNESS" experimental="true" />
<BugPattern abbrev="UNNC" type="UNNC_UNNECESSARY_NEW_NULL_CHECK" category="CORRECTNESS" experimental="true" />
<BugPattern abbrev="DTEP" type="DTEP_DEPRECATED_TYPESAFE_ENUM_PATTERN" category="STYLE" experimental="true" />
+ <BugPattern abbrev="SMA" type="SMA_STUTTERED_METHOD_ARGUMENTS" category="STYLE" experimental="true" />
</FindbugsPlugin>
\ No newline at end of file
Modified: trunk/fb-contrib/etc/messages.xml
===================================================================
--- trunk/fb-contrib/etc/messages.xml 2009-05-09 20:21:13 UTC (rev 1190)
+++ trunk/fb-contrib/etc/messages.xml 2009-05-09 20:58:29 UTC (rev 1191)
@@ -956,6 +956,17 @@
]]>
</Details>
</Detector>
+
+ <Detector class="com.mebigfatguy.fbcontrib.detect.StutteredMethodArguments">
+ <Details>
+ <![CDATA[
+ <p>looks for method calls that pass the same value for two separate parameters, where
+ those arguments are not constants. Often this is a cut/paste mistake, but if not, it is
+ confusing why you would pass the same value for two arguments.</p>
+ <p>It is a fast detector</p>
+ ]]>
+ </Details>
+ </Detector>
<!-- BugPattern -->
@@ -2503,6 +2514,19 @@
</Details>
</BugPattern>
+ <BugPattern type="SMA_STUTTERED_METHOD_ARGUMENTS">
+ <ShortDescription>code calls a method passing the same value to two different arguments</ShortDescription>
+ <LongDescription>code {1} calls a method passing the same value to two different arguments</LongDescription>
+ <Details>
+ <![CDATA[
+ <p>This method calls a method passing the same value for two or more of the parameters.
+ Often this is a cut/paste bug, but if not, it is confusing why you would pass the same value for two
+ different parameters. Perhaps an alternative method that just takes one parameter should be overridden
+ in this case.</p>
+ ]]>
+ </Details>
+ </BugPattern>
+
<!-- BugCode -->
<BugCode abbrev="ISB">Inefficient String Buffering</BugCode>
@@ -2583,4 +2607,5 @@
<BugCode abbrev="BED">Bogus Exception Declaration</BugCode>
<BugCode abbrev="UNNC">Unnecessary New Null Check</BugCode>
<BugCode abbrev="DTEP">Deprecated Typesafe Enum Pattern</BugCode>
+ <BugCode abbrev="SMA">Stuttered Method Arguments</BugCode>
</MessageCollection>
\ No newline at end of file
Added: trunk/fb-contrib/samples/SMA_Sample.java
===================================================================
--- trunk/fb-contrib/samples/SMA_Sample.java (rev 0)
+++ trunk/fb-contrib/samples/SMA_Sample.java 2009-05-09 20:58:29 UTC (rev 1191)
@@ -0,0 +1,14 @@
+
+public class SMA_Sample
+{
+ public void testSMA(SMA_Sample s1, SMA_Sample s2)
+ {
+ if (!s1.equals(s2))
+ testSMA(s1, s1);
+ }
+
+ public void testNonReport(int a, int b)
+ {
+ testNonReport(4, 4);
+ }
+}
Property changes on: trunk/fb-contrib/samples/SMA_Sample.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:eol-style
+ native
Modified: trunk/fb-contrib/samples/UNNC_Sample.java
===================================================================
--- trunk/fb-contrib/samples/UNNC_Sample.java 2009-05-09 20:21:13 UTC (rev 1190)
+++ trunk/fb-contrib/samples/UNNC_Sample.java 2009-05-09 20:58:29 UTC (rev 1191)
@@ -1,5 +1,6 @@
+
public class UNNC_Sample {
public void testPosNEW()
{
@@ -51,5 +52,21 @@
System.out.println("OK");
}
+ public void testFPFinally() throws Exception
+ {
+ StringBuilder sb = null;
+ try
+ {
+ sb = new StringBuilder();
+ sb.append("False Positive");
+ }
+ finally
+ {
+ if (sb != null)
+ sb.setLength(0);
+ }
+
+ }
+
}
Added: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/StutteredMethodArguments.java
===================================================================
--- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/StutteredMethodArguments.java (rev 0)
+++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/StutteredMethodArguments.java 2009-05-09 20:58:29 UTC (rev 1191)
@@ -0,0 +1,148 @@
+/*
+ * fb-contrib - Auxiliary detectors for Java programs
+ * Copyright (C) 2005-2009 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;
+import java.util.Set;
+
+import org.apache.bcel.classfile.Code;
+
+import com.sun.org.apache.bcel.internal.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.OpcodeStack;
+import edu.umd.cs.findbugs.ba.ClassContext;
+import edu.umd.cs.findbugs.ba.XField;
+
+/**
+ * looks for method calls that passes the same argument to two different parameters of the same
+ * method. It doesn't report method calls where the arguments are constants.
+ */
+public class StutteredMethodArguments extends BytecodeScanningDetector {
+ private final BugReporter bugReporter;
+ private OpcodeStack stack;
+
+ /**
+ * constructs a SMA detector given the reporter to report bugs on.
+
+ * @param bugReporter the sync of bug reports
+ */
+ public StutteredMethodArguments(BugReporter bugReporter)
+ {
+ this.bugReporter = bugReporter;
+ }
+
+ /**
+ * overrides the visitor to create the opcode stack
+ *
+ * @param classContext the context object of the currently parsed class
+ */
+ @Override
+ public void visitClassContext(ClassContext classContext)
+ {
+ try {
+ stack = new OpcodeStack();
+ super.visitClassContext(classContext);
+ } finally {
+ stack = null;
+ }
+ }
+
+ /**
+ * overrides the visitor to reset the stack object
+ *
+ * @param obj the context object of the currently parsed code block
+ */
+ @Override
+ public void visitCode(Code obj) {
+ stack.resetForMethodEntry(this);
+ super.visitCode(obj);
+ }
+
+ /**
+ * overrides the visitor to look for method calls that pass the same value
+ * for two different arguments
+ *
+ * @param seen the currently parsed op code
+ */
+ @Override
+ public void sawOpcode(int seen) {
+ try {
+ switch (seen) {
+ case INVOKEVIRTUAL:
+ case INVOKESTATIC:
+ case INVOKEINTERFACE:
+ case INVOKESPECIAL:
+ String signature = getSigConstantOperand();
+ Type[] args = Type.getArgumentTypes(signature);
+ if (args.length > 1) {
+ if (stack.getStackDepth() > args.length) {
+ if (duplicateArguments(stack, args.length)) {
+ bugReporter.reportBug(new BugInstance(this, "SMA_STUTTERED_METHOD_ARGUMENTS", NORMAL_PRIORITY)
+ .addClass(this)
+ .addMethod(this)
+ .addSourceLine(this));
+ }
+ }
+ }
+ break;
+ }
+ } finally {
+ stack.sawOpcode(this, seen);
+ }
+ }
+
+ /**
+ * looks for duplicate arguments that are not constants
+ *
+ * @param stack the stack to look thru
+ * @param length the number of arguments
+ * @return if there are duplicates
+ */
+ private boolean duplicateArguments(OpcodeStack stack, int length)
+ {
+ Set<String> args = new HashSet<String>();
+ for (int i = 0; i < length; i++) {
+ OpcodeStack.Item item = stack.getStackItem(i);
+
+ if (item.getConstant() == null) {
+ String arg = null;
+ int reg = item.getRegisterNumber();
+ if (reg >= 0) {
+ arg = String.valueOf(reg);
+ } else {
+ XField f = item.getXField();
+ if (f != null) {
+ arg = f.getName();
+ }
+ }
+
+ if (arg != null) {
+ if (args.contains(arg))
+ return true;
+ args.add(arg);
+ }
+ }
+ }
+
+ return false;
+ }
+}
Property changes on: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/StutteredMethodArguments.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:eol-style
+ native
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|