[Fb-contrib-commit] SF.net SVN: fb-contrib:[1559] trunk/fb-contrib
Brought to you by:
dbrosius
|
From: <dbr...@us...> - 2010-05-19 05:31:20
|
Revision: 1559
http://fb-contrib.svn.sourceforge.net/fb-contrib/?rev=1559&view=rev
Author: dbrosius
Date: 2010-05-19 05:31:14 +0000 (Wed, 19 May 2010)
Log Message:
-----------
new detector UVA
Modified Paths:
--------------
trunk/fb-contrib/etc/findbugs.xml
trunk/fb-contrib/etc/messages.xml
Added Paths:
-----------
trunk/fb-contrib/samples/UVA_Sample.java
trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/UseVarArgs.java
Modified: trunk/fb-contrib/etc/findbugs.xml
===================================================================
--- trunk/fb-contrib/etc/findbugs.xml 2010-05-11 03:50:30 UTC (rev 1558)
+++ trunk/fb-contrib/etc/findbugs.xml 2010-05-19 05:31:14 UTC (rev 1559)
@@ -335,6 +335,10 @@
<Detector class="com.mebigfatguy.fbcontrib.detect.WriteOnlyCollection"
speed="fast"
reports="WOC_WRITE_ONLY_COLLECTION_LOCAL,WOC_WRITE_ONLY_COLLECTION_FIELD" />
+
+ <Detector class="com.mebigfatguy.fbcontrib.detect.UseVarArgs"
+ speed="fast"
+ reports="UVA_USE_VAR_ARGS" />
<!-- BugPattern -->
@@ -635,4 +639,6 @@
category="CORRECTNESS" experimental="true" />
<BugPattern abbrev="WOC" type="WOC_WRITE_ONLY_COLLECTION_FIELD"
category="CORRECTNESS" experimental="true" />
+ <BugPattern abbrev="UVA" type="UVA_USE_VAR_ARGS"
+ category="STYLE" experimental="true" />
</FindbugsPlugin>
\ No newline at end of file
Modified: trunk/fb-contrib/etc/messages.xml
===================================================================
--- trunk/fb-contrib/etc/messages.xml 2010-05-11 03:50:30 UTC (rev 1558)
+++ trunk/fb-contrib/etc/messages.xml 2010-05-19 05:31:14 UTC (rev 1559)
@@ -1145,6 +1145,17 @@
]]>
</Details>
</Detector>
+
+ <Detector class="com.mebigfatguy.fbcontrib.detect.UseVarArgs">
+ <Details>
+ <![CDATA[
+ <p>This detector looks for definitions of methods that have an array as the last parameter.
+ Since this class is compiled with java 1.5 or better, it would be more flexible for clients of this
+ method to define this parameter as a vararg parameter</p>
+ <p>It is a fast detector</p>
+ ]]>
+ </Details>
+ </Detector>
<!-- BugPattern -->
@@ -3142,6 +3153,18 @@
]]>
</Details>
</BugPattern>
+
+ <BugPattern type="UVA_USE_VAR_ARGS">
+ <ShortDescription>Method defines parameter list with array as last argument, rather than vararg</ShortDescription>
+ <LongDescription>Method {1} defines parameter list with array as last argument, rather than vararg</LongDescription>
+ <Details>
+ <![CDATA[
+ <p>This method defines a parameter list that ends with an array. As this class is compiled with
+ Java 1.5 or better, this parameter could be defined as a vararg parameter instead, which can be
+ more convienent for client developers to use. This is not a bug, per se, just an improvement.
+ ]]>
+ </Details>
+ </BugPattern>
<!-- BugCode -->
@@ -3239,4 +3262,5 @@
<BugCode abbrev="IPU">Improper Properties use</BugCode>
<BugCode abbrev="PCAIL">Possible Constant Allocation In Loop</BugCode>
<BugCode abbrev="WOC">Write Only Collection</BugCode>
+ <BugCode abbrev="UVA">Use Var Args</BugCode>
</MessageCollection>
Added: trunk/fb-contrib/samples/UVA_Sample.java
===================================================================
--- trunk/fb-contrib/samples/UVA_Sample.java (rev 0)
+++ trunk/fb-contrib/samples/UVA_Sample.java 2010-05-19 05:31:14 UTC (rev 1559)
@@ -0,0 +1,36 @@
+import java.util.ArrayList;
+import java.util.Date;
+
+
+public class UVA_Sample<T> extends ArrayList<T>
+{
+ public void testNormalUVA(String[] foo)
+ {}
+
+ public void testLowUVA1(int boo, int[] hoo)
+ {}
+
+ public void testLowUVA2(int boo, String hoo, Date[] woo)
+ {}
+
+ public static void testStaticUVA(Date[] d)
+ {}
+
+ public void fpNoParms()
+ {}
+
+ public void fpHasOtherArrayUVA1(String[] one, int[] two)
+ {}
+
+ public void fpTooManyArgs(int i, char j, long k, String[] moo)
+ {}
+
+ public void fpNotAtEnd(String[] foo, int bar)
+ {}
+
+ @Override
+ public <T> T[] toArray(T[] a)
+ {
+ return null;
+ }
+}
Property changes on: trunk/fb-contrib/samples/UVA_Sample.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:eol-style
+ native
Added: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/UseVarArgs.java
===================================================================
--- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/UseVarArgs.java (rev 0)
+++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/UseVarArgs.java 2010-05-19 05:31:14 UTC (rev 1559)
@@ -0,0 +1,118 @@
+/*
+ * 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.Constants;
+import org.apache.bcel.classfile.JavaClass;
+import org.apache.bcel.classfile.Method;
+import org.apache.bcel.generic.Type;
+
+import edu.umd.cs.findbugs.BugInstance;
+import edu.umd.cs.findbugs.BugReporter;
+import edu.umd.cs.findbugs.Detector;
+import edu.umd.cs.findbugs.ba.ClassContext;
+import edu.umd.cs.findbugs.visitclass.PreorderVisitor;
+
+public class UseVarArgs extends PreorderVisitor implements Detector
+{
+ private BugReporter bugReporter;
+ private JavaClass javaClass;
+
+ public UseVarArgs(BugReporter bugReporter) {
+ this.bugReporter = bugReporter;
+ }
+
+ public void visitClassContext(ClassContext classContext) {
+ try {
+ javaClass = classContext.getJavaClass();
+ if (javaClass.getMajor() >= Constants.MAJOR_1_5) {
+ javaClass.accept(this);
+ }
+ } finally {
+ javaClass = null;
+ }
+ }
+
+ public void visitMethod(Method obj) {
+ try {
+ if (obj.isSynthetic()) {
+ return;
+ }
+
+ Type[] types = obj.getArgumentTypes();
+ if ((types.length == 0) || (types.length > 3)) {
+ return;
+ }
+
+ String lastParmSig = types[types.length-1].getSignature();
+ if (!lastParmSig.startsWith("[") || lastParmSig.startsWith("[[")) {
+ return;
+ }
+
+ if (obj.isStatic() && "main".equals(obj.getName()) && "([Ljava/lang/String;)V".equals(obj.getSignature())) {
+ return;
+ }
+
+ if (!obj.isPrivate() && !obj.isStatic() && isInherited(obj)) {
+ return;
+ }
+
+ super.visitMethod(obj);
+ bugReporter.reportBug(new BugInstance(this, "UVA_USE_VAR_ARGS", (types.length == 1) ? NORMAL_PRIORITY : LOW_PRIORITY)
+ .addClass(this)
+ .addMethod(this));
+
+
+ } catch (ClassNotFoundException cnfe) {
+ bugReporter.reportMissingClass(cnfe);
+ }
+ }
+
+ public void report() {
+ }
+
+ private boolean isInherited(Method m) throws ClassNotFoundException {
+ JavaClass[] infs = javaClass.getAllInterfaces();
+ for (JavaClass inf : infs) {
+ if (hasMethod(inf, m))
+ return true;
+ }
+
+ JavaClass[] sups = javaClass.getSuperClasses();
+ for (JavaClass sup : sups) {
+ if (hasMethod(sup, m))
+ return true;
+ }
+
+ return false;
+ }
+
+ private boolean hasMethod(JavaClass c, Method candidateMethod) {
+ String name = candidateMethod.getName();
+ String sig = candidateMethod.getSignature();
+
+ for (Method method : c.getMethods()) {
+ if (!method.isStatic() && method.getName().equals(name) && method.getSignature().equals(sig)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+}
Property changes on: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/UseVarArgs.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.
|