[Fb-contrib-commit] SF.net SVN: fb-contrib:[1664] trunk/fb-contrib
Brought to you by:
dbrosius
|
From: <dbr...@us...> - 2011-04-30 19:46:33
|
Revision: 1664
http://fb-contrib.svn.sourceforge.net/fb-contrib/?rev=1664&view=rev
Author: dbrosius
Date: 2011-04-30 19:46:27 +0000 (Sat, 30 Apr 2011)
Log Message:
-----------
add LGO detector and sample
Modified Paths:
--------------
trunk/fb-contrib/etc/findbugs.xml
trunk/fb-contrib/etc/messages.xml
Added Paths:
-----------
trunk/fb-contrib/samples/LGO_Sample.java
trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/LingeringGraphicsObjects.java
Modified: trunk/fb-contrib/etc/findbugs.xml
===================================================================
--- trunk/fb-contrib/etc/findbugs.xml 2011-04-26 01:55:51 UTC (rev 1663)
+++ trunk/fb-contrib/etc/findbugs.xml 2011-04-30 19:46:27 UTC (rev 1664)
@@ -220,6 +220,7 @@
<Detector class="com.mebigfatguy.fbcontrib.detect.SuspiciousGetterSetterUse" speed="fast" reports="SGSU_SUSPICIOUS_GETTER_SETTER_USE" />
+ <Detector class="com.mebigfatguy.fbcontrib.detect.LingeringGraphicsObjects" speed="fast" reports="LGO_LINGERING_GRAPHICS_OBJECT" />
<!-- BugPattern -->
<BugPattern abbrev="ISB" type="ISB_INEFFICIENT_STRING_BUFFERING" category="PERFORMANCE" />
@@ -384,4 +385,5 @@
<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" />
+ <BugPattern abbrev="LGO" type="LGO_LINGERING_GRAPHICS_OBJECT" category="PERFORMANCE" experimental="true" />
</FindbugsPlugin>
Modified: trunk/fb-contrib/etc/messages.xml
===================================================================
--- trunk/fb-contrib/etc/messages.xml 2011-04-26 01:55:51 UTC (rev 1663)
+++ trunk/fb-contrib/etc/messages.xml 2011-04-30 19:46:27 UTC (rev 1664)
@@ -1191,6 +1191,19 @@
]]>
</Details>
</Detector>
+
+ <Detector class="com.mebigfatguy.fbcontrib.detect.LingeringGraphicsObjects">
+ <Details>
+ <![CDATA[
+ <p>This detector looks for creation of java.awt.Graphics object that do not have the
+ .dispose() method called on them when finished. These objects will be cleaned up by
+ the Garbage collector, bug given the likelyhood that large numbers of these objects can
+ be created in a short period of time, it is better to dispose them as soon as possible
+ </p>
+ <p>It is a fast detector</p>
+ ]]>
+ </Details>
+ </Detector>
<!-- BugPattern -->
@@ -3276,6 +3289,19 @@
]]>
</Details>
</BugPattern>
+
+ <BugPattern type="LGO_LINGERING_GRAPHICS_OBJECT">
+ <ShortDescription>Method allocations a java.awt.Graphics object without disposing it</ShortDescription>
+ <LongDescription>Method {1} allocations a java.awt.Graphics object without disposing it</LongDescription>
+ <Details>
+ <![CDATA[
+ <p>This method allocates a java.awt.Graphics object but doesn't dispose of it when done. While
+ the garbage collecter will clean this up, given that a large number of Graphics objects can be
+ created in a short period of time, it is recommended that you explicitly dispose() of them.
+ </p>
+ ]]>
+ </Details>
+ </BugPattern>
<!-- BugCode -->
@@ -3377,4 +3403,5 @@
<BugCode abbrev="PUS">Possible Unsuspected Serialization</BugCode>
<BugCode abbrev="SEC">Side Effect Constructor</BugCode>
<BugCode abbrev="SGSU">Suspicious Getter Setter Use</BugCode>
+ <BugCode abbrev="LGO">Lingering Graphics Object</BugCode>
</MessageCollection>
Added: trunk/fb-contrib/samples/LGO_Sample.java
===================================================================
--- trunk/fb-contrib/samples/LGO_Sample.java (rev 0)
+++ trunk/fb-contrib/samples/LGO_Sample.java 2011-04-30 19:46:27 UTC (rev 1664)
@@ -0,0 +1,27 @@
+import java.awt.Graphics;
+import java.awt.image.BufferedImage;
+
+
+public class LGO_Sample {
+
+ public void testCreate(Graphics g) {
+ Graphics g2 = g.create();
+ }
+
+ public void testBufferedImage(BufferedImage bi) {
+ Graphics g = bi.getGraphics();
+ }
+
+ public void fpWasDisposed(Graphics g) {
+ Graphics g2 = g.create();
+ try {
+
+ } finally {
+ g2.dispose();
+ }
+ }
+
+ public Graphics returnG(Graphics g) {
+ return g.create();
+ }
+}
Property changes on: trunk/fb-contrib/samples/LGO_Sample.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:eol-style
+ native
Added: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/LingeringGraphicsObjects.java
===================================================================
--- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/LingeringGraphicsObjects.java (rev 0)
+++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/LingeringGraphicsObjects.java 2011-04-30 19:46:27 UTC (rev 1664)
@@ -0,0 +1,146 @@
+/*
+ * fb-contrib - Auxiliary detectors for Java programs
+ * Copyright (C) 2005-2011 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.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.bcel.classfile.Code;
+
+import com.mebigfatguy.fbcontrib.utils.RegisterUtils;
+
+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;
+
+public class LingeringGraphicsObjects extends BytecodeScanningDetector {
+
+ private static final Set<String> GRAPHICS_PRODUCERS = new HashSet<String>();
+ static {
+ GRAPHICS_PRODUCERS.add("java/awt/image/BufferedImage#getGraphics()Ljava/awt/Graphics;");
+ GRAPHICS_PRODUCERS.add("java/awt/Graphics#create()Ljava/awt/Graphics;");
+ }
+
+ private final BugReporter bugReporter;
+ private OpcodeStack stack;
+ private Map<Integer, Integer> graphicsRegs; //reg->pc
+
+ public LingeringGraphicsObjects(BugReporter bugReporter) {
+ this.bugReporter = bugReporter;
+ }
+
+ @Override
+ public void visitClassContext(ClassContext classContext) {
+ try {
+ stack = new OpcodeStack();
+ graphicsRegs = new HashMap<Integer, Integer>();
+ super.visitClassContext(classContext);
+ } finally {
+ stack = null;
+ graphicsRegs = null;
+ }
+ }
+
+ @Override
+ public void visitCode(Code obj) {
+ stack.resetForMethodEntry(this);
+ super.visitCode(obj);
+ for (Integer pc : graphicsRegs.values()) {
+ bugReporter.reportBug(new BugInstance(this, "LGO_LINGERING_GRAPHICS_OBJECT", NORMAL_PRIORITY)
+ .addClass(this)
+ .addMethod(this)
+ .addSourceLine(this, pc.intValue()));
+ }
+
+ }
+
+ @Override
+ public void sawOpcode(int seen) {
+ Integer sawNewGraphicsAt = null;
+ try {
+ switch (seen) {
+ case ALOAD:
+ case ALOAD_0:
+ case ALOAD_1:
+ case ALOAD_2:
+ case ALOAD_3: {
+ int reg = RegisterUtils.getALoadReg(this, seen);
+ sawNewGraphicsAt = graphicsRegs.get(Integer.valueOf(reg));
+ }
+ break;
+
+ case ASTORE:
+ case ASTORE_0:
+ case ASTORE_1:
+ case ASTORE_2:
+ case ASTORE_3: {
+ if (stack.getStackDepth() > 0) {
+ OpcodeStack.Item item = stack.getStackItem(0);
+ sawNewGraphicsAt = (Integer)item.getUserValue();
+
+ Integer reg = Integer.valueOf(RegisterUtils.getAStoreReg(this, seen));
+ if (sawNewGraphicsAt != null) {
+ graphicsRegs.put(reg, sawNewGraphicsAt);
+ } else {
+ graphicsRegs.remove(reg);
+ }
+ sawNewGraphicsAt = null;
+ }
+ }
+ break;
+
+ case ARETURN:
+ if (stack.getStackDepth() > 0) {
+ OpcodeStack.Item item = stack.getStackItem(0);
+ graphicsRegs.remove(Integer.valueOf(item.getRegisterNumber()));
+ }
+ break;
+
+ case INVOKEVIRTUAL:
+ String clsName = getClassConstantOperand();
+ String methodName = getNameConstantOperand();
+ String methodSig = getSigConstantOperand();
+ String methodInfo = clsName + "#" + methodName + methodSig;
+ if (GRAPHICS_PRODUCERS.contains(methodInfo)) {
+ sawNewGraphicsAt = Integer.valueOf(getPC());
+ } else {
+ if ("java/awt/Graphics#dispose()V".equals(methodInfo)) {
+ if (stack.getStackDepth() > 0) {
+ OpcodeStack.Item item = stack.getStackItem(0);
+ graphicsRegs.remove(Integer.valueOf(item.getRegisterNumber()));
+ }
+ }
+ }
+ break;
+ }
+ } finally {
+ stack.sawOpcode(this, seen);
+ if (sawNewGraphicsAt != null) {
+ if (stack.getStackDepth() > 0) {
+ OpcodeStack.Item item = stack.getStackItem(0);
+ item.setUserValue(sawNewGraphicsAt);
+ }
+ }
+ }
+ }
+}
Property changes on: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/LingeringGraphicsObjects.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.
|