[Fb-contrib-commit] SF.net SVN: fb-contrib:[1603] trunk/fb-contrib
Brought to you by:
dbrosius
|
From: <dbr...@us...> - 2010-09-04 20:38:10
|
Revision: 1603
http://fb-contrib.svn.sourceforge.net/fb-contrib/?rev=1603&view=rev
Author: dbrosius
Date: 2010-09-04 20:38:03 +0000 (Sat, 04 Sep 2010)
Log Message:
-----------
New detector SGSU
Modified Paths:
--------------
trunk/fb-contrib/etc/findbugs.xml
trunk/fb-contrib/etc/messages.xml
Added Paths:
-----------
trunk/fb-contrib/samples/SGSU_Sample.java
trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousGetterSetterUse.java
Modified: trunk/fb-contrib/etc/findbugs.xml
===================================================================
--- trunk/fb-contrib/etc/findbugs.xml 2010-08-31 01:33:46 UTC (rev 1602)
+++ trunk/fb-contrib/etc/findbugs.xml 2010-09-04 20:38:03 UTC (rev 1603)
@@ -213,6 +213,7 @@
<Detector class="com.mebigfatguy.fbcontrib.detect.SideEffectConstructor" speed="fast" reports="SEC_SIDE_EFFECT_CONSTRUCTOR" />
+ <Detector class="com.mebigfatguy.fbcontrib.detect.SuspiciousGetterSetterUse" speed="fast" reports="SGSU_SUSPICIOUS_GETTER_SETTER_USE" />
<!-- BugPattern -->
<BugPattern abbrev="ISB" type="ISB_INEFFICIENT_STRING_BUFFERING" category="PERFORMANCE" />
@@ -373,4 +374,5 @@
<BugPattern abbrev="UVA" type="UVA_USE_VAR_ARGS" category="STYLE" />
<BugPattern abbrev="PUS" type="PUS_POSSIBLE_UNSUSPECTED_SERIALIZATION" category="CORRECTNESS" experimental="true" />
<BugPattern abbrev="SEC" type="SEC_SIDE_EFFECT_CONSTRUCTOR" category="STYLE" experimental="true" />
+ <BugPattern abbrev="SGSU" type="SGSU_SUSPICIOUS_GETTER_SETTER_USE" category="CORRECTNESS" experimental="true" />
</FindbugsPlugin>
\ No newline at end of file
Modified: trunk/fb-contrib/etc/messages.xml
===================================================================
--- trunk/fb-contrib/etc/messages.xml 2010-08-31 01:33:46 UTC (rev 1602)
+++ trunk/fb-contrib/etc/messages.xml 2010-09-04 20:38:03 UTC (rev 1603)
@@ -1178,6 +1178,20 @@
]]>
</Details>
</Detector>
+
+ <Detector class="com.mebigfatguy.fbcontrib.detect.SuspiciousGetterSetterUse">
+ <Details>
+ <![CDATA[
+ <p>This detector looks for java bean getter-setter use where the value of a property is set
+ with the value retrieved from the same bean's correllary getter, like this:</p>
+ <pre>
+ person.setAge(person.getAge());
+ </pre>
+ <p>Typically this is a copy paste typo.</p>
+ <p>It is a fast detector</p>
+ ]]>
+ </Details>
+ </Detector>
<!-- BugPattern -->
@@ -3212,6 +3226,17 @@
]]>
</Details>
</BugPattern>
+
+ <BugPattern type="SGSU_SUSPICIOUS_GETTER_SETTER_USE">
+ <ShortDescription>Method uses same bean's getter value for setter</ShortDescription>
+ <LongDescription>Method {1} uses same bean's getter value for setter</LongDescription>
+ <Details>
+ <![CDATA[
+ <p>This method retrieves the property of a java bean, only to use it in the setter
+ for the same property of the same bean. This is usually a copy/paste typo.
+ ]]>
+ </Details>
+ </BugPattern>
<!-- BugCode -->
@@ -3312,4 +3337,5 @@
<BugCode abbrev="UVA">Use Var Args</BugCode>
<BugCode abbrev="PUS">Possible Unsuspected Serialization</BugCode>
<BugCode abbrev="SEC">Side Effect Constructor</BugCode>
+ <BugCode abbrev="SGSU">Suspicious Getter Setter Use</BugCode>
</MessageCollection>
Added: trunk/fb-contrib/samples/SGSU_Sample.java
===================================================================
--- trunk/fb-contrib/samples/SGSU_Sample.java (rev 0)
+++ trunk/fb-contrib/samples/SGSU_Sample.java 2010-09-04 20:38:03 UTC (rev 1603)
@@ -0,0 +1,20 @@
+
+public class SGSU_Sample
+{
+ private SGSU_Sample foo;
+
+ public void testSGSU(SGSU_Sample s1, SGSU_Sample s2)
+ {
+ s1.setSGSU(s1.getSGSU());
+ }
+
+ public void setSGSU(SGSU_Sample f)
+ {
+ foo = f;
+ }
+
+ public SGSU_Sample getSGSU()
+ {
+ return foo;
+ }
+}
Property changes on: trunk/fb-contrib/samples/SGSU_Sample.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:eol-style
+ native
Added: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousGetterSetterUse.java
===================================================================
--- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousGetterSetterUse.java (rev 0)
+++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousGetterSetterUse.java 2010-09-04 20:38:03 UTC (rev 1603)
@@ -0,0 +1,157 @@
+/*
+ * fb-contrib - Auxiliary detectors for Java programs
+ * Copyright (C) 2005-2010 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 org.apache.bcel.classfile.Code;
+
+import edu.umd.cs.findbugs.BugInstance;
+import edu.umd.cs.findbugs.BugReporter;
+import edu.umd.cs.findbugs.BytecodeScanningDetector;
+
+/**
+ * looks for methods that set a setter with the value obtained from the same bean's
+ * complimentary getter. This is usually a typo.
+ */
+public class SuspiciousGetterSetterUse extends BytecodeScanningDetector {
+
+ private static enum State {SEEN_NOTHING, SEEN_ALOAD, SEEN_GETFIELD, SEEN_DUAL_LOADS, SEEN_INVOKEVIRTUAL};
+ private final BugReporter bugReporter;
+ private State state;
+ private String beanReference;
+ private String propName;
+ private String propType;
+
+ /**
+ * constructs a SGSU detector given the reporter to report bugs on
+ * @param bugReporter the sync of bug reports
+ */
+ public SuspiciousGetterSetterUse(BugReporter bugReporter) {
+ this.bugReporter = bugReporter;
+ }
+
+ /**
+ * overrides the visitor to reset the state to SEEN_NOTHING, and clear the beanReference, propName
+ * and propType
+ *
+ * @param code the context object of the currently parsed code block
+ */
+ @Override
+ public void visitCode(Code obj) {
+ state = State.SEEN_NOTHING;
+ beanReference = null;
+ propName = null;
+ propType = null;
+ super.visitCode(obj);
+ }
+
+ /**
+ * overrides the visitor to look for a setXXX with the value returned from a getXXX
+ * using the same base object.
+ *
+ * @param seen the currently parsed opcode
+ */
+ @Override
+ public void sawOpcode(int seen) {
+ boolean reset = true;
+ switch (state) {
+ case SEEN_NOTHING:
+ switch (seen) {
+ case ALOAD:
+ case ALOAD_0:
+ case ALOAD_1:
+ case ALOAD_2:
+ case ALOAD_3:
+ beanReference = String.valueOf(getRegisterOperand());
+ state = State.SEEN_ALOAD;
+ reset = false;
+ break;
+
+ case GETFIELD:
+ beanReference = getNameConstantOperand();
+ state = State.SEEN_GETFIELD;
+ reset = false;
+ break;
+ }
+ break;
+
+ case SEEN_ALOAD:
+ switch (seen) {
+ case ALOAD:
+ case ALOAD_0:
+ case ALOAD_1:
+ case ALOAD_2:
+ case ALOAD_3:
+ if (beanReference.equals(String.valueOf(getRegisterOperand()))) {
+ state = State.SEEN_DUAL_LOADS;
+ reset = false;
+ }
+ break;
+ }
+ break;
+
+ case SEEN_GETFIELD:
+ if (beanReference.equals(getNameConstantOperand())) {
+ state = State.SEEN_DUAL_LOADS;
+ reset = false;
+ }
+ break;
+
+ case SEEN_DUAL_LOADS:
+ if (seen == INVOKEVIRTUAL) {
+ String sig = getSigConstantOperand();
+ if (sig.startsWith("()")) {
+ propType = sig.substring("()".length());
+ if (!propType.equals("V")) {
+ propName = getNameConstantOperand();
+ if (propName.startsWith("get")) {
+ propName = propName.substring("get".length());
+ state = State.SEEN_INVOKEVIRTUAL;
+ reset = false;
+ }
+ }
+ }
+ }
+ break;
+
+ case SEEN_INVOKEVIRTUAL:
+ if (seen == INVOKEVIRTUAL) {
+ String sig = getSigConstantOperand();
+ if (sig.equals("(" + propType + ")V")) {
+ String name = getNameConstantOperand();
+ if (name.startsWith("set")) {
+ if (propName.equals(name.substring("set".length()))) {
+ bugReporter.reportBug(new BugInstance(this, "SGSU_SUSPICIOUS_GETTER_SETTER_USE", NORMAL_PRIORITY)
+ .addClass(this)
+ .addMethod(this)
+ .addSourceLine(this));
+ }
+ }
+ }
+ }
+ break;
+ }
+
+ if (reset) {
+ beanReference = null;
+ propType = null;
+ propName = null;
+ state = State.SEEN_NOTHING;
+ }
+ }
+}
Property changes on: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousGetterSetterUse.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.
|