[Fb-contrib-commit] SF.net SVN: fb-contrib:[1507] trunk/fb-contrib
Brought to you by:
dbrosius
|
From: <dbr...@us...> - 2010-01-24 00:42:58
|
Revision: 1507
http://fb-contrib.svn.sourceforge.net/fb-contrib/?rev=1507&view=rev
Author: dbrosius
Date: 2010-01-24 00:31:40 +0000 (Sun, 24 Jan 2010)
Log Message:
-----------
new Detector: IPU - Improper Properties use
Modified Paths:
--------------
trunk/fb-contrib/etc/findbugs.xml
trunk/fb-contrib/etc/messages.xml
trunk/fb-contrib/htdocs/index.shtml
Added Paths:
-----------
trunk/fb-contrib/samples/IPU_Sample.java
trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/ImproperPropertiesUse.java
Modified: trunk/fb-contrib/etc/findbugs.xml
===================================================================
--- trunk/fb-contrib/etc/findbugs.xml 2010-01-23 23:50:15 UTC (rev 1506)
+++ trunk/fb-contrib/etc/findbugs.xml 2010-01-24 00:31:40 UTC (rev 1507)
@@ -324,6 +324,10 @@
speed="fast"
reports="ROOM_REFLECTION_ON_OBJECT_METHODS" />
+ <Detector class="com.mebigfatguy.fbcontrib.detect.ImproperPropertiesUse"
+ speed="fast"
+ reports="IPU_IMPROPER_PROPERTIES_USE,IPU_IMPROPER_PROPERTIES_USE_SETPROPERTY" />
+
<!-- BugPattern -->
<BugPattern abbrev="ISB" type="ISB_INEFFICIENT_STRING_BUFFERING"
@@ -610,4 +614,8 @@
category="CORRECTNESS" />
<BugPattern abbrev="ROOM" type="ROOM_REFLECTION_ON_OBJECT_METHODS"
category="CORRECTNESS" />
+ <BugPattern abbrev="IPU" type="IPU_IMPROPER_PROPERTIES_USE"
+ category="CORRECTNESS" experimental="true" />
+ <BugPattern abbrev="IPU" type="IPU_IMPROPER_PROPERTIES_USE_SETPROPERTY"
+ 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-01-23 23:50:15 UTC (rev 1506)
+++ trunk/fb-contrib/etc/messages.xml 2010-01-24 00:31:40 UTC (rev 1507)
@@ -1110,6 +1110,19 @@
]]>
</Details>
</Detector>
+
+ <Detector class="com.mebigfatguy.fbcontrib.detect.ImproperPropertiesUse">
+ <Details>
+ <![CDATA[
+ <p>This detector looks for java.util.Properties use where values other than String
+ are placed in the properties object. As the Properties object was intended to be a
+ String to String only collection, putting other types in the Properties object is
+ incorrect, and takes advantage of a poor design decision by the original Properties class
+ designers to derive from Hashtable, rather than using aggregation.</p>
+ <p>It is a fast detector</p>
+ ]]>
+ </Details>
+ </Detector>
<!-- BugPattern -->
@@ -3019,6 +3032,32 @@
]]>
</Details>
</BugPattern>
+
+ <BugPattern type="IPU_IMPROPER_PROPERTIES_USE">
+ <ShortDescription>Method puts non-String values into a Properties object</ShortDescription>
+ <LongDescription>Method {1} puts non-String values into a Properties object</LongDescription>
+ <Details>
+ <![CDATA[
+ <p>This method places non-String objects into a Properties object. As the Properties object
+ is intented to be a String to String map, putting non String objects is wrong, and takes advantage
+ of a design flaw in the Properties class by deriving from Hashtable instead of using aggregation.
+ If you want a collection that holds other types of objects, use a Hashtable, or better still newer collections
+ like HashMap or TreeMap.
+ ]]>
+ </Details>
+ </BugPattern>
+
+ <BugPattern type="IPU_IMPROPER_PROPERTIES_USE_SETPROPERTY">
+ <ShortDescription>Method uses Properties.put instead of Properties.setProperty</ShortDescription>
+ <LongDescription>Method {1} uses Properties.put instead of Properties.setProperty</LongDescription>
+ <Details>
+ <![CDATA[
+ <p>This method uses the inherited method from Hashtable put(String key, Object value) in
+ a Properties object. Since the Properties object was intended to be only a String to String
+ map, use of the derived put method is discouraged. Use the Properties.setProperty method instead.
+ ]]>
+ </Details>
+ </BugPattern>
<!-- BugCode -->
@@ -3113,4 +3152,5 @@
<BugCode abbrev="SNG">Suspicious Null Guard</BugCode>
<BugCode abbrev="MDM">More Dumb Methods</BugCode>
<BugCode abbrev="ROOM">Reflection on Object Methods</BugCode>
+ <BugCode abbrev="IPU">Improper Properties use</BugCode>
</MessageCollection>
Modified: trunk/fb-contrib/htdocs/index.shtml
===================================================================
--- trunk/fb-contrib/htdocs/index.shtml 2010-01-23 23:50:15 UTC (rev 1506)
+++ trunk/fb-contrib/htdocs/index.shtml 2010-01-24 00:31:40 UTC (rev 1507)
@@ -69,6 +69,12 @@
<li><b>[ROOM] Reflection on Object Methods</b><br/>
Looks for method calls through reflection on methods found in java.lang.Object.
As these methods are always available, there's no reason to do this.</li>
+ <li><b>[IPU] Improper Properties Use</b><br/>
+ Looks for java.util.Properties use where values other than String are placed in
+ the properties object. As the Properties object was intended to be a String to
+ String only collection, putting other types in the Properties object is incorrect,
+ and takes advantage of a poor design decision by the original Properties class
+ designers to derive from Hashtable, rather than using aggregation.</li>
</ul>
</div>
<hr/>
Added: trunk/fb-contrib/samples/IPU_Sample.java
===================================================================
--- trunk/fb-contrib/samples/IPU_Sample.java (rev 0)
+++ trunk/fb-contrib/samples/IPU_Sample.java 2010-01-24 00:31:40 UTC (rev 1507)
@@ -0,0 +1,24 @@
+import java.util.Properties;
+
+
+public class IPU_Sample
+{
+
+ public void testIPUSimple()
+ {
+ Properties p = new Properties();
+ p.put("Key", new Integer(0));
+ }
+
+ public void testIPUUseSetProperty(Object o)
+ {
+ Properties p = new Properties();
+ p.put("Key", o);
+ }
+
+ public void testIPUUseMinorSetProperty()
+ {
+ Properties p = new Properties();
+ p.put("Key", "Hello");
+ }
+}
Property changes on: trunk/fb-contrib/samples/IPU_Sample.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:eol-style
+ native
Added: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/ImproperPropertiesUse.java
===================================================================
--- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/ImproperPropertiesUse.java (rev 0)
+++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/ImproperPropertiesUse.java 2010-01-24 00:31:40 UTC (rev 1507)
@@ -0,0 +1,100 @@
+/*
+ * 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;
+import edu.umd.cs.findbugs.OpcodeStack;
+import edu.umd.cs.findbugs.ba.ClassContext;
+
+/**
+ * looks for java.util.Properties use where values other than String
+ * are placed in the properties object. As the Properties object was intended to be a
+ * String to String only collection, putting other types in the Properties object is
+ * incorrect, and takes advantage of a poor design decision by the original Properties class
+ * designers to derive from Hashtable, rather than using aggregation.
+ */
+public class ImproperPropertiesUse extends BytecodeScanningDetector {
+
+ private BugReporter bugReporter;
+ private OpcodeStack stack;
+
+ /**
+ * constructs a IPU detector given the reporter to report bugs on
+ * @param bugReporter the sync of bug reports
+ */
+ public ImproperPropertiesUse(BugReporter bugReporter) {
+ this.bugReporter = bugReporter;
+ }
+
+ public void visitClassContext(ClassContext classContext) {
+ try {
+ stack = new OpcodeStack();
+ super.visitClassContext(classContext);
+ } finally {
+ stack = null;
+ }
+ }
+
+ public void visitCode(Code obj) {
+ stack.resetForMethodEntry(this);
+ super.visitCode(obj);
+ }
+
+ public void sawOpcode(int seen) {
+ try {
+ if (seen == INVOKEVIRTUAL) {
+ String clsName = getClassConstantOperand();
+ if ("java/util/Properties".equals(clsName)) {
+ String methodName = getNameConstantOperand();
+ if ("put".equals(methodName)) {
+ String sig = getSigConstantOperand();
+ if ("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;".equals(sig)) {
+ if (stack.getStackDepth() >= 3) {
+ OpcodeStack.Item valueItem = stack.getStackItem(0);
+ String valueSig = valueItem.getSignature();
+ if ("Ljava/lang/String;".equals(valueSig)) {
+ bugReporter.reportBug(new BugInstance(this, "IPU_IMPROPER_PROPERTIES_USE_SETPROPERTY", LOW_PRIORITY)
+ .addClass(this)
+ .addMethod(this)
+ .addSourceLine(this));
+ } else if (!"Ljava/lang/Object;".equals(valueSig)) {
+ bugReporter.reportBug(new BugInstance(this, "IPU_IMPROPER_PROPERTIES_USE", NORMAL_PRIORITY)
+ .addClass(this)
+ .addMethod(this)
+ .addSourceLine(this));
+ } else {
+ bugReporter.reportBug(new BugInstance(this, "IPU_IMPROPER_PROPERTIES_USE_SETPROPERTY", NORMAL_PRIORITY)
+ .addClass(this)
+ .addMethod(this)
+ .addSourceLine(this));
+ }
+ }
+ }
+ }
+ }
+ }
+ } finally {
+ stack.sawOpcode(this, seen);
+ }
+ }
+}
Property changes on: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/ImproperPropertiesUse.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.
|