[Fb-contrib-commit] fb-contrib/src/com/mebigfatguy/fbcontrib/detect ArrayBasedCollections.java,NONE,
Brought to you by:
dbrosius
|
From: Dave B. <dbr...@us...> - 2005-12-17 08:27:09
|
Update of /cvsroot/fb-contrib/fb-contrib/src/com/mebigfatguy/fbcontrib/detect In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14091/src/com/mebigfatguy/fbcontrib/detect Added Files: ArrayBasedCollections.java Log Message: initial checkin - new ABC detector --- NEW FILE: ArrayBasedCollections.java --- /* * fb-contrib - Auxilliary detectors for Java programs * Copyright (C) 2005 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.StatelessDetector; public class ArrayBasedCollections extends BytecodeScanningDetector implements StatelessDetector { private BugReporter bugReporter; private OpcodeStack stack = new OpcodeStack(); public ArrayBasedCollections(BugReporter bugReporter) { this.bugReporter = bugReporter; } @Override public Object clone() throws CloneNotSupportedException { return super.clone(); } @Override public void visitCode(Code obj) { stack.resetForMethodEntry(this); super.visitCode(obj); } @Override public void sawOpcode(int seen) { try { if (seen == INVOKEINTERFACE) { String className = getClassConstantOperand(); String methodName = getNameConstantOperand(); String methodSig = getSigConstantOperand(); boolean found = false; if ("java/util/Map".equals(className) && "put".equals(methodName) && "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;".equals(methodSig)) { if (stack.getStackDepth() > 1) { OpcodeStack.Item itm = stack.getStackItem(1); String pushedSig = itm.getSignature(); if (pushedSig.charAt(0) == '[') found = true; } } else if ("java/util/Set".equals(className) && "add".equals(methodName) && "(Ljava/lang/Object;)Z".equals(methodSig)) { if (stack.getStackDepth() > 0) { OpcodeStack.Item itm = stack.getStackItem(0); String pushedSig = itm.getSignature(); if (pushedSig.charAt(0) == '[') found = true; } } else if ("java/util/List".equals(className) && "contains".equals(methodName) && "(Ljava/lang/Object;)Z".equals(methodSig)) { if (stack.getStackDepth() > 0) { OpcodeStack.Item itm = stack.getStackItem(0); String pushedSig = itm.getSignature(); if (pushedSig.charAt(0) == '[') found = true; } } if (found) { bugReporter.reportBug(new BugInstance(this, "ABC_ARRAY_BASED_COLLECTIONS", NORMAL_PRIORITY) .addClass(this) .addMethod(this) .addSourceLine(this)); } } } finally { stack.sawOpcode(this, seen); } } } |